script.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. def rm_fr_list(self, item_list, dst_path):
  21. for item_path in item_list:
  22. dst_item_path = str(dst_path.joinpath(item_path))
  23. dst_item_path = dst_item_path.replace('\\', '/')
  24. dst_item_path = Path(dst_item_path)
  25. if dst_item_path.exists():
  26. if Path.is_dir(dst_item_path):
  27. shutil.rmtree(self.long_path_enable(dst_item_path))
  28. else:
  29. os.remove(self.long_path_enable(dst_item_path))
  30. @staticmethod
  31. def long_path_enable(path_in):
  32. if os.name == "nt":
  33. return str('\\\\?\\' + str(path_in))
  34. else:
  35. return path_in
  36. def cp_fr_list(self, item_list, src_path, dst_path):
  37. item_list_temp = []
  38. for item_path in item_list:
  39. if str(item_path) == "*":
  40. item_list_temp.extend(os.listdir(src_path))
  41. else:
  42. item_list_temp.append(item_path)
  43. item_list_all = list(set(item_list_temp))
  44. for item_path in item_list_all:
  45. src_item_path = str(src_path.joinpath(item_path))
  46. dst_item_path = str(dst_path.joinpath(item_path))
  47. src_item_path = src_item_path.replace('\\', '/')
  48. dst_item_path = dst_item_path.replace('\\', '/')
  49. src_item_path = Path(src_item_path)
  50. dst_item_path = Path(dst_item_path)
  51. if dst_item_path.exists():
  52. if Path.is_dir(dst_item_path):
  53. shutil.rmtree(self.long_path_enable(dst_item_path))
  54. else:
  55. os.remove(self.long_path_enable(dst_item_path))
  56. if not dst_item_path.parent.exists():
  57. os.makedirs(self.long_path_enable(dst_item_path.parent))
  58. if src_item_path.is_dir():
  59. shutil.copytree(self.long_path_enable(src_item_path), self.long_path_enable(dst_item_path))
  60. else:
  61. shutil.copy(self.long_path_enable(src_item_path), self.long_path_enable(dst_item_path))
  62. def copy_platformio_packages(self):
  63. print("************* Copy and install platformio packages (Step 1/2) ***************")
  64. if not self.platformio_path.exists():
  65. os.makedirs(self.long_path_enable(self.platformio_path))
  66. self.cp_fr_list([".platformio"], self.current_folder, self.home_path)
  67. else:
  68. self.cp_fr_list(os.listdir(self.current_folder.joinpath(".platformio/packages")),
  69. self.current_folder.joinpath(".platformio/packages"),
  70. self.platformio_path.joinpath("packages"))
  71. self.cp_fr_list(os.listdir(self.current_folder.joinpath(".platformio/platforms")),
  72. self.current_folder.joinpath(".platformio/platforms"),
  73. self.platformio_path.joinpath("platforms"))
  74. print(self.current_folder.joinpath(".platformio"))
  75. print(os.listdir(self.current_folder.joinpath(".platformio")))
  76. other_file_and_folders = os.listdir(self.current_folder.joinpath(".platformio"))
  77. other_file_and_folders.remove("platforms")
  78. other_file_and_folders.remove("packages")
  79. self.cp_fr_list(other_file_and_folders, self.current_folder.joinpath(".platformio"), self.platformio_path)
  80. if self.platformio_path.joinpath("penv").exists():
  81. if Path.is_dir(self.platformio_path.joinpath("penv")):
  82. shutil.rmtree(self.long_path_enable(self.platformio_path.joinpath("penv")))
  83. else:
  84. os.remove(self.long_path_enable(self.platformio_path.joinpath("penv")))
  85. def install_platformio(self):
  86. print("************* Create platformio environment (Step 2/2) ***************")
  87. os.system(str(self.python_path.as_posix()) + " " + str(self.get_platformio_script_path.as_posix()))
  88. print("************* Done ***************")
  89. def make_platformio(self):
  90. self.copy_platformio_packages()
  91. self.install_platformio()
  92. if len(sys.argv)>1:
  93. rt_studio_version = sys.argv[1]
  94. print("rt_studio_version: "+str(rt_studio_version))
  95. if rt_studio_version >= "2.0.0":
  96. builder = PlatformioBuilder()
  97. builder.make_platformio()
  98. else:
  99. print("RT-Thread Studio version is not match")
  100. else:
  101. print("Info:")
  102. print("Please add RT-Thread Studio version number as parameter")