script.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import os
  2. import shutil
  3. import sys
  4. from pathlib import Path
  5. class PlatformioBuilder(object):
  6. """
  7. This is the platformio env builder
  8. """
  9. def __init__(self):
  10. dir_path = ""
  11. if getattr(sys, 'frozen', False):
  12. dir_path = os.path.dirname(sys.executable)
  13. elif __file__:
  14. dir_path = os.path.dirname(__file__)
  15. self.current_folder = Path(dir_path)
  16. self.home_path = Path.home()
  17. self.platformio_path = self.home_path.joinpath(".platformio")
  18. self.python_path = self.current_folder.joinpath("python377x64/python.exe")
  19. self.get_platformio_script_path = self.current_folder.joinpath("python377x64/get-platformio.py")
  20. self.is_pip_config_exists = False
  21. self.pip_config_dir_path = self.home_path.joinpath("pip")
  22. if self.home_path.joinpath("pip/pip.ini").exists():
  23. self.is_pip_config_exists = True
  24. def modify_pip_source(self):
  25. if self.is_pip_config_exists:
  26. shutil.move(str(self.home_path.joinpath("pip/pip.ini")), str(self.home_path.joinpath("pip/pip.ini.backup")))
  27. self.cp_fr_list(["pip.ini"], self.current_folder, self.pip_config_dir_path)
  28. def recover_pip_source(self):
  29. os.remove(self.pip_config_dir_path.joinpath("pip.ini"))
  30. if self.is_pip_config_exists:
  31. shutil.move(str(self.home_path.joinpath("pip/pip.ini.backup")), str(self.home_path.joinpath("pip/pip.ini")))
  32. def rm_fr_list(self, item_list, dst_path):
  33. for item_path in item_list:
  34. dst_item_path = str(dst_path.joinpath(item_path))
  35. dst_item_path = dst_item_path.replace('\\', '/')
  36. dst_item_path = Path(dst_item_path)
  37. if dst_item_path.exists():
  38. if Path.is_dir(dst_item_path):
  39. shutil.rmtree(self.long_path_enable(dst_item_path))
  40. else:
  41. os.remove(self.long_path_enable(dst_item_path))
  42. @staticmethod
  43. def long_path_enable(path_in):
  44. if os.name == "nt":
  45. return str('\\\\?\\' + str(path_in))
  46. else:
  47. return path_in
  48. def cp_fr_list(self, item_list, src_path, dst_path):
  49. item_list_temp = []
  50. for item_path in item_list:
  51. if str(item_path) == "*":
  52. item_list_temp.extend(os.listdir(src_path))
  53. else:
  54. item_list_temp.append(item_path)
  55. item_list_all = list(set(item_list_temp))
  56. for item_path in item_list_all:
  57. src_item_path = str(src_path.joinpath(item_path))
  58. dst_item_path = str(dst_path.joinpath(item_path))
  59. src_item_path = src_item_path.replace('\\', '/')
  60. dst_item_path = dst_item_path.replace('\\', '/')
  61. src_item_path = Path(src_item_path)
  62. dst_item_path = Path(dst_item_path)
  63. if dst_item_path.exists():
  64. if Path.is_dir(dst_item_path):
  65. shutil.rmtree(self.long_path_enable(dst_item_path))
  66. else:
  67. os.remove(self.long_path_enable(dst_item_path))
  68. if not dst_item_path.parent.exists():
  69. os.makedirs(self.long_path_enable(dst_item_path.parent))
  70. if src_item_path.is_dir():
  71. shutil.copytree(self.long_path_enable(src_item_path), self.long_path_enable(dst_item_path))
  72. else:
  73. shutil.copy(self.long_path_enable(src_item_path), self.long_path_enable(dst_item_path))
  74. def copy_platformio_packages(self):
  75. print("************* Copy and install platformio packages (Step 1/2) ***************")
  76. if not self.platformio_path.exists():
  77. os.makedirs(self.long_path_enable(self.platformio_path))
  78. self.cp_fr_list([".platformio"], self.current_folder, self.home_path)
  79. else:
  80. self.cp_fr_list(os.listdir(self.current_folder.joinpath(".platformio/packages")),
  81. self.current_folder.joinpath(".platformio/packages"),
  82. self.platformio_path.joinpath("packages"))
  83. self.cp_fr_list(os.listdir(self.current_folder.joinpath(".platformio/platforms")),
  84. self.current_folder.joinpath(".platformio/platforms"),
  85. self.platformio_path.joinpath("platforms"))
  86. print(self.current_folder.joinpath(".platformio"))
  87. other_file_and_folders = os.listdir(self.current_folder.joinpath(".platformio"))
  88. other_file_and_folders.remove("platforms")
  89. other_file_and_folders.remove("packages")
  90. self.cp_fr_list(other_file_and_folders, self.current_folder.joinpath(".platformio"), self.platformio_path)
  91. if self.platformio_path.joinpath("penv").exists():
  92. if Path.is_dir(self.platformio_path.joinpath("penv")):
  93. shutil.rmtree(self.long_path_enable(self.platformio_path.joinpath("penv")))
  94. else:
  95. os.remove(self.long_path_enable(self.platformio_path.joinpath("penv")))
  96. def install_platformio(self):
  97. print("************* Create platformio environment (Step 2/2) ***************")
  98. os.system(str(self.python_path.as_posix()) + " " + str(self.get_platformio_script_path.as_posix()))
  99. print("************* Done ***************")
  100. def make_platformio(self):
  101. self.copy_platformio_packages()
  102. self.install_platformio()
  103. if len(sys.argv) > 2:
  104. rt_studio_version = sys.argv[1]
  105. if "global" in str(sys.argv[2]).lower():
  106. is_studio_global_version = True
  107. else:
  108. is_studio_global_version = False
  109. print("rt_studio_version: " + str(rt_studio_version))
  110. if rt_studio_version >= "2.0.0":
  111. if is_studio_global_version:
  112. builder = PlatformioBuilder()
  113. builder.make_platformio()
  114. else:
  115. builder = PlatformioBuilder()
  116. builder.modify_pip_source()
  117. builder.make_platformio()
  118. builder.recover_pip_source()
  119. else:
  120. print("RT-Thread Studio version is not match")
  121. else:
  122. print("Info:Please add RT-Thread Studio version number as parameter")