script.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import os
  2. import platform
  3. import shutil
  4. import sys
  5. from pathlib import Path
  6. class PlatformioBuilder(object):
  7. """
  8. This is the platformio env builder
  9. """
  10. def __init__(self):
  11. dir_path = ""
  12. if getattr(sys, 'frozen', False):
  13. dir_path = os.path.dirname(sys.executable)
  14. elif __file__:
  15. dir_path = os.path.dirname(__file__)
  16. self.current_folder = Path(dir_path)
  17. self.home_path = Path.home()
  18. self.platformio_path = self.home_path.joinpath(".platformio")
  19. self.python_path = self.platformio_path.joinpath(".rt_studio/python377x64/python.exe")
  20. self.get_platformio_script_path = self.platformio_path.joinpath(".rt_studio/python377x64/get-platformio.py")
  21. self.is_pip_config_exists = False
  22. self.pip_config_dir_path = self.home_path.joinpath("pip")
  23. if self.home_path.joinpath("pip/pip.ini").exists():
  24. self.is_pip_config_exists = True
  25. def modify_pip_source(self):
  26. if self.is_pip_config_exists:
  27. shutil.move(str(self.home_path.joinpath("pip/pip.ini")), str(self.home_path.joinpath("pip/pip.ini.backup")))
  28. self.cp_fr_list(["pip.ini"], self.current_folder, self.pip_config_dir_path)
  29. def recover_pip_source(self):
  30. os.remove(self.pip_config_dir_path.joinpath("pip.ini"))
  31. if self.is_pip_config_exists:
  32. shutil.move(str(self.home_path.joinpath("pip/pip.ini.backup")), str(self.home_path.joinpath("pip/pip.ini")))
  33. def rm_fr_list(self, item_list, dst_path):
  34. for item_path in item_list:
  35. dst_item_path = str(dst_path.joinpath(item_path))
  36. dst_item_path = dst_item_path.replace('\\', '/')
  37. dst_item_path = Path(dst_item_path)
  38. if dst_item_path.exists():
  39. if Path.is_dir(dst_item_path):
  40. shutil.rmtree(self.long_path_enable(dst_item_path))
  41. else:
  42. os.remove(self.long_path_enable(dst_item_path))
  43. @staticmethod
  44. def long_path_enable(path_in):
  45. if os.name == "nt":
  46. return str('\\\\?\\' + str(path_in))
  47. else:
  48. return path_in
  49. def cp_fr_list(self, item_list, src_path, dst_path):
  50. item_list_temp = []
  51. for item_path in item_list:
  52. if str(item_path) == "*":
  53. item_list_temp.extend(os.listdir(src_path))
  54. else:
  55. item_list_temp.append(item_path)
  56. item_list_all = list(set(item_list_temp))
  57. for item_path in item_list_all:
  58. src_item_path = str(src_path.joinpath(item_path))
  59. dst_item_path = str(dst_path.joinpath(item_path))
  60. src_item_path = src_item_path.replace('\\', '/')
  61. dst_item_path = dst_item_path.replace('\\', '/')
  62. src_item_path = Path(src_item_path)
  63. dst_item_path = Path(dst_item_path)
  64. if dst_item_path.exists():
  65. if Path.is_dir(dst_item_path):
  66. shutil.rmtree(self.long_path_enable(dst_item_path))
  67. else:
  68. os.remove(self.long_path_enable(dst_item_path))
  69. if not dst_item_path.parent.exists():
  70. os.makedirs(self.long_path_enable(dst_item_path.parent))
  71. if src_item_path.is_dir():
  72. shutil.copytree(self.long_path_enable(src_item_path), self.long_path_enable(dst_item_path))
  73. else:
  74. shutil.copy(self.long_path_enable(src_item_path), self.long_path_enable(dst_item_path))
  75. def copy_platformio_packages(self):
  76. print("************* Copy and install platformio packages (Step 1/2) ***************", flush=True)
  77. if not self.platformio_path.exists():
  78. os.makedirs(self.long_path_enable(self.platformio_path))
  79. print("Copying .platformio folder...", flush=True)
  80. self.cp_fr_list([".platformio"], self.current_folder, self.home_path)
  81. else:
  82. print("Copying packages...", flush=True)
  83. self.cp_fr_list(os.listdir(self.current_folder.joinpath(".platformio/packages")),
  84. self.current_folder.joinpath(".platformio/packages"),
  85. self.platformio_path.joinpath("packages"))
  86. print("Copying platforms...", flush=True)
  87. self.cp_fr_list(os.listdir(self.current_folder.joinpath(".platformio/platforms")),
  88. self.current_folder.joinpath(".platformio/platforms"),
  89. self.platformio_path.joinpath("platforms"))
  90. other_file_and_folders = os.listdir(self.current_folder.joinpath(".platformio"))
  91. other_file_and_folders.remove("platforms")
  92. other_file_and_folders.remove("packages")
  93. print("Copying other files...", flush=True)
  94. self.cp_fr_list(other_file_and_folders, self.current_folder.joinpath(".platformio"), self.platformio_path)
  95. if self.platformio_path.joinpath("penv").exists():
  96. if Path.is_dir(self.platformio_path.joinpath("penv")):
  97. shutil.rmtree(self.long_path_enable(self.platformio_path.joinpath("penv")))
  98. else:
  99. os.remove(self.long_path_enable(self.platformio_path.joinpath("penv")))
  100. def copy_portble_python(self):
  101. print("Copying python environment...", flush=True)
  102. self.cp_fr_list(['python377x64'], self.current_folder, self.platformio_path.joinpath(".rt_studio"))
  103. def install_platformio(self):
  104. print("************* Create platformio environment (Step 2/2) ***************", flush=True)
  105. os.system(str(self.python_path.as_posix()) + " " + "-m pioinstaller")
  106. print("************* Done ***************", flush=True)
  107. def make_platformio(self):
  108. self.copy_platformio_packages()
  109. self.copy_portble_python()
  110. self.install_platformio()
  111. if len(sys.argv) > 2:
  112. builder = PlatformioBuilder()
  113. rt_studio_version = sys.argv[1]
  114. if "global" in str(sys.argv[2]).lower():
  115. is_studio_global_version = True
  116. else:
  117. is_studio_global_version = False
  118. print("rt_studio_version: " + str(rt_studio_version), flush=True)
  119. if rt_studio_version >= "2.0.0":
  120. if int(platform.release()) >= int("10"):
  121. result = os.popen(
  122. """reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\FileSystem /v LongPathsEnabled""")
  123. if "1" in result.read().strip().split(" ")[-1]:
  124. print("Long path support has enabled", flush=True)
  125. else:
  126. print("Long path support has not enabled", flush=True)
  127. print("Enable windows long path support...", flush=True)
  128. bat_path = builder.current_folder.joinpath("longpathenable.bat").as_posix()
  129. os.system(str(bat_path))
  130. result = os.popen(
  131. """reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\FileSystem /v LongPathsEnabled""")
  132. if "1" in result.read().strip().split(" ")[-1]:
  133. print("Long path support has enabled", flush=True)
  134. else:
  135. print("Enable long path support fail.", flush=True)
  136. sys.exit(1)
  137. else:
  138. print("Current windows os version is lower than windows 10, skip enable long path support", flush=True)
  139. if is_studio_global_version:
  140. if not builder.current_folder.joinpath("python377x64/.rt_global").exists():
  141. os.makedirs(builder.current_folder.joinpath("python377x64/.rt_global"))
  142. builder.make_platformio()
  143. else:
  144. if builder.current_folder.joinpath("python377x64/.rt_global").exists():
  145. shutil.rmtree(builder.current_folder.joinpath("python377x64/.rt_global"))
  146. builder.modify_pip_source()
  147. builder.make_platformio()
  148. builder.recover_pip_source()
  149. else:
  150. print("RT-Thread Studio version is not match", flush=True)
  151. else:
  152. print("Info:Please add RT-Thread Studio version number as parameter", flush=True)