script.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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) ***************")
  77. if not self.platformio_path.exists():
  78. os.makedirs(self.long_path_enable(self.platformio_path))
  79. self.cp_fr_list([".platformio"], self.current_folder, self.home_path)
  80. else:
  81. self.cp_fr_list(os.listdir(self.current_folder.joinpath(".platformio/packages")),
  82. self.current_folder.joinpath(".platformio/packages"),
  83. self.platformio_path.joinpath("packages"))
  84. self.cp_fr_list(os.listdir(self.current_folder.joinpath(".platformio/platforms")),
  85. self.current_folder.joinpath(".platformio/platforms"),
  86. self.platformio_path.joinpath("platforms"))
  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 copy_portble_python(self):
  97. self.cp_fr_list(['python377x64'], self.current_folder, self.platformio_path.joinpath(".rt_studio"))
  98. def install_platformio(self):
  99. print("************* Create platformio environment (Step 2/2) ***************")
  100. os.system(str(self.python_path.as_posix()) + " " + str(self.get_platformio_script_path.as_posix()))
  101. print("************* Done ***************")
  102. def make_platformio(self):
  103. self.copy_platformio_packages()
  104. self.copy_portble_python()
  105. self.install_platformio()
  106. if len(sys.argv) > 2:
  107. builder = PlatformioBuilder()
  108. rt_studio_version = sys.argv[1]
  109. if "global" in str(sys.argv[2]).lower():
  110. is_studio_global_version = True
  111. else:
  112. is_studio_global_version = False
  113. print("rt_studio_version: " + str(rt_studio_version))
  114. if rt_studio_version >= "2.0.0":
  115. if int(platform.release()) >= int("10"):
  116. result = os.popen(
  117. """reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\FileSystem /v LongPathsEnabled""")
  118. if "1" in result.read().strip().split(" ")[-1]:
  119. print("Long path support has enabled")
  120. else:
  121. print("Long path support has not enabled")
  122. print("Enable windows long path support...")
  123. bat_path = builder.current_folder.joinpath("longpathenable.bat").as_posix()
  124. os.system(str(bat_path))
  125. result = os.popen(
  126. """reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\FileSystem /v LongPathsEnabled""")
  127. if "1" in result.read().strip().split(" ")[-1]:
  128. print("Long path support has enabled")
  129. else:
  130. print("Enable long path support fail.")
  131. sys.exit(1)
  132. else:
  133. print("Current windows os version is lower than windows 10, skip enable long path support")
  134. if is_studio_global_version:
  135. builder.make_platformio()
  136. else:
  137. builder.modify_pip_source()
  138. builder.make_platformio()
  139. builder.recover_pip_source()
  140. else:
  141. print("RT-Thread Studio version is not match")
  142. else:
  143. print("Info:Please add RT-Thread Studio version number as parameter")