install_env.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #! /usr/bin/env python
  2. #coding=utf-8
  3. #
  4. # File : env.py
  5. # This file is part of RT-Thread RTOS
  6. # COPYRIGHT (C) 2024, RT-Thread Development Team
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. # Change Logs:
  23. # Date Author Notes
  24. # 2024-04-05 Bernard Add copyright information
  25. #
  26. import os
  27. import sys
  28. import platform
  29. import shutil
  30. import zipfile
  31. PKG_URL = 'https://github.com/RT-Thread/packages.git'
  32. SDK_URL = 'https://github.com/RT-Thread/sdk.git'
  33. ENV_URL = 'https://github.com/RT-Thread/env.git'
  34. def help_info():
  35. print("**********************************************************************************\n"
  36. "* Help infomation:\n"
  37. "* Git tool install step.\n"
  38. "* If your system is linux, you can use command below to install git.\n"
  39. "* $ sudo yum install git\n"
  40. "* $ sudo apt-get install git\n"
  41. "* If your system is windows, you should download git software(msysGit).\n"
  42. "* Download path: http://git-scm.com/download/win\n"
  43. "* After you install it, be sure to add the git command execution PATH \n"
  44. "* to your system PATH.\n"
  45. "* Usually, git command PATH is $YOUR_INSTALL_DIR\\Git\\bin\n"
  46. "* If your system is OSX, please download git and install it.\n"
  47. "* Download path: http://git-scm.com/download/mac\n"
  48. "**********************************************************************************\n")
  49. def touch_env():
  50. if sys.platform != 'win32':
  51. home_dir = os.environ['HOME']
  52. else:
  53. home_dir = os.environ['USERPROFILE']
  54. pkg_url = os.getenv('RTT_PACKAGE_URL') or PKG_URL
  55. sdk_url = os.getenv('RTT_SDK_URL') or SDK_URL
  56. env_url = os.getenv('RTT_ENV_URL') or ENV_URL
  57. if len(sys.argv) == 2 and sys.argv[1] == '--gitee':
  58. # "Install RT-Thread Environment from Gitee"
  59. sdk_url = 'https://gitee.com/RT-Thread-Mirror/sdk.git'
  60. pkg_url = 'https://gitee.com/RT-Thread-Mirror/packages.git'
  61. env_url = 'https://gitee.com/RT-Thread-Mirror/env.git'
  62. # make .env and other directories
  63. env_dir = os.path.join(home_dir, '.env')
  64. if not os.path.exists(env_dir):
  65. os.mkdir(env_dir)
  66. os.mkdir(os.path.join(env_dir, 'local_pkgs'))
  67. os.mkdir(os.path.join(env_dir, 'packages'))
  68. os.mkdir(os.path.join(env_dir, 'tools'))
  69. kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  70. kconfig.close()
  71. # git clone packages
  72. if not os.path.exists(os.path.join(env_dir, 'packages', 'packages')):
  73. try:
  74. ret = os.system('git clone %s %s' % (pkg_url, os.path.join(env_dir, 'packages', 'packages')))
  75. if ret != 0:
  76. shutil.rmtree(os.path.join(env_dir, 'packages', 'packages'))
  77. print("********************************************************************************\n"
  78. "* Warnning:\n"
  79. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\".\n"
  80. "* This error may have been caused by not found a git tool or network error.\n"
  81. "* If the git tool is not installed, install the git tool first.\n"
  82. "* If the git utility is installed, check whether the git command is added to \n"
  83. "* the system PATH.\n"
  84. "* This error may cause the RT-Thread packages to not work properly.\n"
  85. "********************************************************************************\n")
  86. help_info()
  87. else:
  88. kconfig = open(os.path.join(env_dir, 'packages', 'Kconfig'), 'w')
  89. kconfig.write('source "$PKGS_DIR/packages/Kconfig"')
  90. kconfig.close()
  91. except:
  92. print("**********************************************************************************\n"
  93. "* Warnning:\n"
  94. "* Run command error for \"git clone https://github.com/RT-Thread/packages.git\". \n"
  95. "* This error may have been caused by not found a git tool or git tool not in \n"
  96. "* the system PATH. \n"
  97. "* This error may cause the RT-Thread packages to not work properly. \n"
  98. "**********************************************************************************\n")
  99. help_info()
  100. # git clone env scripts
  101. if not os.path.exists(os.path.join(env_dir, 'tools', 'scripts')):
  102. try:
  103. ret = os.system('git clone %s %s' % (env_url, os.path.join(env_dir, 'tools', 'scripts')))
  104. if ret != 0:
  105. shutil.rmtree(os.path.join(env_dir, 'tools', 'scripts'))
  106. print("********************************************************************************\n"
  107. "* Warnning:\n"
  108. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\".\n"
  109. "* This error may have been caused by not found a git tool or network error.\n"
  110. "* If the git tool is not installed, install the git tool first.\n"
  111. "* If the git utility is installed, check whether the git command is added \n"
  112. "* to the system PATH.\n"
  113. "* This error may cause script tools to fail to work properly.\n"
  114. "********************************************************************************\n")
  115. help_info()
  116. except:
  117. print("********************************************************************************\n"
  118. "* Warnning:\n"
  119. "* Run command error for \"git clone https://github.com/RT-Thread/env.git\". \n"
  120. "* This error may have been caused by not found a git tool or git tool not in \n"
  121. "* the system PATH. \n"
  122. "* This error may cause script tools to fail to work properly. \n"
  123. "********************************************************************************\n")
  124. help_info()
  125. # git clone sdk
  126. if not os.path.exists(os.path.join(env_dir, 'packages', 'sdk')):
  127. try:
  128. ret = os.system('git clone %s %s' % (sdk_url, os.path.join(env_dir, 'packages', 'sdk')))
  129. if ret != 0:
  130. shutil.rmtree(os.path.join(env_dir, 'packages', 'sdk'))
  131. print("********************************************************************************\n"
  132. "* Warnning:\n"
  133. "* Run command error for \"git clone https://github.com/RT-Thread/sdk.git\".\n"
  134. "* This error may have been caused by not found a git tool or network error.\n"
  135. "* If the git tool is not installed, install the git tool first.\n"
  136. "* If the git utility is installed, check whether the git command is added \n"
  137. "* to the system PATH.\n"
  138. "* This error may cause the RT-Thread SDK to not work properly.\n"
  139. "********************************************************************************\n")
  140. help_info()
  141. except:
  142. print("********************************************************************************\n"
  143. "* Warnning:\n"
  144. "* Run command error for \"https://github.com/RT-Thread/sdk.git\".\n"
  145. "* This error may have been caused by not found a git tool or git tool not in \n"
  146. "* the system PATH. \n"
  147. "* This error may cause the RT-Thread SDK to not work properly. \n"
  148. "********************************************************************************\n")
  149. help_info()
  150. # try to create an empty .config file
  151. if not os.path.exists(os.path.join(env_dir, 'tools', '.config')):
  152. kconfig = open(os.path.join(env_dir, 'tools', '.config'), 'w')
  153. kconfig.close()
  154. # copy env.sh or env.ps1, Kconfig
  155. shutil.copy(os.path.join(env_dir, 'tools', 'scripts', 'Kconfig'), os.path.join(home_dir, '.env', 'tools'))
  156. if sys.platform != 'win32':
  157. shutil.copy(os.path.join(env_dir, 'tools', 'scripts', 'env.sh'), os.path.join(home_dir, '.env', 'env.sh'))
  158. else:
  159. shutil.copy(os.path.join(env_dir, 'tools', 'scripts', 'env.ps1'), os.path.join(home_dir, '.env', 'env.ps1'))
  160. # unzip kconfig-mconf.zip
  161. zip_file = os.path.join(env_dir, 'tools', 'scripts', 'kconfig-mconf.zip')
  162. if os.path.exists(zip_file):
  163. zip_file_dir = os.path.join(env_dir, 'tools', 'bin')
  164. if os.path.exists(zip_file_dir):
  165. shutil.rmtree(zip_file_dir)
  166. zip_file_obj = zipfile.ZipFile(zip_file, 'r')
  167. for file in zip_file_obj.namelist():
  168. zip_file_obj.extract(file, zip_file_dir)
  169. zip_file_obj.close()
  170. def main():
  171. print("Install RT-Thread Environment")
  172. touch_env()
  173. return
  174. if __name__ == "__main__":
  175. main()