mkdist.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import os
  2. import shutil
  3. from shutil import ignore_patterns
  4. def do_copy_file(src, dst):
  5. # check source file
  6. if not os.path.exists(src):
  7. return
  8. path = os.path.dirname(dst)
  9. # mkdir if path not exist
  10. if not os.path.exists(path):
  11. os.makedirs(path)
  12. shutil.copy2(src, dst)
  13. def do_copy_folder(src_dir, dst_dir, ignore=None):
  14. import shutil
  15. # check source directory
  16. if not os.path.exists(src_dir):
  17. return
  18. try:
  19. if os.path.exists(dst_dir):
  20. shutil.rmtree(dst_dir)
  21. except:
  22. print('Deletes folder: %s failed.' % dst_dir)
  23. return
  24. shutil.copytree(src_dir, dst_dir, ignore = ignore)
  25. source_ext = ["c", "h", "s", "S", "cpp", "xpm"]
  26. source_list = []
  27. def walk_children(child):
  28. global source_list
  29. global source_ext
  30. # print child
  31. full_path = child.rfile().abspath
  32. file_type = full_path.rsplit('.',1)[1]
  33. #print file_type
  34. if file_type in source_ext:
  35. if full_path not in source_list:
  36. source_list.append(full_path)
  37. children = child.all_children()
  38. if children != []:
  39. for item in children:
  40. walk_children(item)
  41. def walk_kconfig(RTT_ROOT, source_list):
  42. for parent, dirnames, filenames in os.walk(RTT_ROOT):
  43. if 'bsp' in parent:
  44. continue
  45. if '.git' in parent:
  46. continue
  47. if 'tools' in parent:
  48. continue
  49. if 'Kconfig' in filenames:
  50. pathfile = os.path.join(parent, 'Kconfig')
  51. source_list.append(pathfile)
  52. if 'KConfig' in filenames:
  53. pathfile = os.path.join(parent, 'KConfig')
  54. source_list.append(pathfile)
  55. def MakeCopy(program, BSP_ROOT, RTT_ROOT, Env):
  56. global source_list
  57. target_path = os.path.join(BSP_ROOT, 'rt-thread')
  58. if target_path.startswith(RTT_ROOT):
  59. print('please use scons --dist to make a distribution')
  60. return
  61. for item in program:
  62. walk_children(item)
  63. source_list.sort()
  64. # fill source file in RT-Thread
  65. target_list = []
  66. for src in source_list:
  67. if Env['PLATFORM'] == 'win32':
  68. src = src.lower()
  69. if src.startswith(RTT_ROOT):
  70. target_list.append(src)
  71. source_list = target_list
  72. # get source directory
  73. src_dir = []
  74. for src in source_list:
  75. src = src.replace(RTT_ROOT, '')
  76. if src[0] == os.sep or src[0] == '/':
  77. src = src[1:]
  78. path = os.path.dirname(src)
  79. sub_path = path.split(os.sep)
  80. full_path = RTT_ROOT
  81. for item in sub_path:
  82. full_path = os.path.join(full_path, item)
  83. if full_path not in src_dir:
  84. src_dir.append(full_path)
  85. for item in src_dir:
  86. source_list.append(os.path.join(item, 'SConscript'))
  87. walk_kconfig(RTT_ROOT, source_list)
  88. for src in source_list:
  89. dst = src.replace(RTT_ROOT, '')
  90. if dst[0] == os.sep or dst[0] == '/':
  91. dst = dst[1:]
  92. print '=> ', dst
  93. dst = os.path.join(target_path, dst)
  94. do_copy_file(src, dst)
  95. # copy tools directory
  96. print("=> tools")
  97. do_copy_folder(os.path.join(RTT_ROOT, "tools"), os.path.join(target_path, "tools"), ignore_patterns('*.pyc'))
  98. do_copy_file(os.path.join(RTT_ROOT, 'KConfig'), os.path.join(target_path, 'KConfig'))
  99. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  100. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  101. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  102. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  103. print('=> libc')
  104. do_copy_folder(os.path.join(RTT_ROOT, "components", 'libc', 'compilers'), os.path.join(target_path, "components", 'libc', 'compilers'))
  105. print('done!')
  106. def MakeCopyHeader(program, BSP_ROOT, RTT_ROOT, Env):
  107. global source_list
  108. global source_ext
  109. source_ext = []
  110. source_ext = ["h", "xpm"]
  111. target_path = os.path.join(BSP_ROOT, 'rt-thread')
  112. if target_path.startswith(RTT_ROOT):
  113. print('please use scons --dist to make a distribution')
  114. return
  115. for item in program:
  116. walk_children(item)
  117. source_list.sort()
  118. # fill source file in RT-Thread
  119. target_list = []
  120. for src in source_list:
  121. if Env['PLATFORM'] == 'win32':
  122. src = src.lower()
  123. if src.startswith(RTT_ROOT):
  124. target_list.append(src)
  125. source_list = target_list
  126. for src in source_list:
  127. dst = src.replace(RTT_ROOT, '')
  128. if dst[0] == os.sep or dst[0] == '/':
  129. dst = dst[1:]
  130. print '=> ', dst
  131. dst = os.path.join(target_path, dst)
  132. do_copy_file(src, dst)
  133. # copy tools directory
  134. print "=> tools"
  135. do_copy_folder(os.path.join(RTT_ROOT, "tools"), os.path.join(target_path, "tools"), ignore_patterns('*.pyc'))
  136. do_copy_file(os.path.join(RTT_ROOT, 'KConfig'), os.path.join(target_path, 'KConfig'))
  137. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  138. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  139. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  140. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  141. print('done!')
  142. def MkDist(program, BSP_ROOT, RTT_ROOT, Env):
  143. print("make distribution....")
  144. dist_name = os.path.basename(BSP_ROOT)
  145. dist_dir = os.path.join(BSP_ROOT, 'dist', dist_name)
  146. # copy BSP files
  147. do_copy_folder(os.path.join(BSP_ROOT), dist_dir,
  148. ignore_patterns('build', 'dist', '*.pyc', '*.old', '*.map', 'rtthread.bin', '.sconsign.dblite', '*.elf', '*.axf'))
  149. global source_list
  150. target_path = os.path.join(dist_dir, 'rt-thread')
  151. for item in program:
  152. walk_children(item)
  153. source_list.sort()
  154. # copy the source files in RT-Thread
  155. target_list = []
  156. for src in source_list:
  157. if src.lower().startswith(BSP_ROOT.lower()):
  158. continue
  159. if src.lower().startswith(RTT_ROOT.lower()):
  160. target_list.append(src)
  161. source_list = target_list
  162. # get source directory
  163. src_dir = []
  164. for src in source_list:
  165. src = src.replace(RTT_ROOT, '')
  166. if src[0] == os.sep or src[0] == '/':
  167. src = src[1:]
  168. path = os.path.dirname(src)
  169. sub_path = path.split(os.sep)
  170. full_path = RTT_ROOT
  171. for item in sub_path:
  172. full_path = os.path.join(full_path, item)
  173. if full_path not in src_dir:
  174. src_dir.append(full_path)
  175. for item in src_dir:
  176. source_list.append(os.path.join(item, 'SConscript'))
  177. # add all of Kconfig files
  178. walk_kconfig(RTT_ROOT, source_list)
  179. source_list.sort()
  180. for src in source_list:
  181. dst = src.replace(RTT_ROOT, '')
  182. if dst[0] == os.sep or dst[0] == '/':
  183. dst = dst[1:]
  184. print('=> %s' % dst)
  185. dst = os.path.join(target_path, dst)
  186. do_copy_file(src, dst)
  187. # copy tools directory
  188. print("=> tools")
  189. do_copy_folder(os.path.join(RTT_ROOT, "tools"), os.path.join(target_path, "tools"), ignore_patterns('*.pyc'))
  190. do_copy_file(os.path.join(RTT_ROOT, 'KConfig'), os.path.join(target_path, 'KConfig'))
  191. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  192. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  193. do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md'))
  194. do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md'))
  195. print('=> libc')
  196. do_copy_folder(os.path.join(RTT_ROOT, "components", 'libc', 'compilers'), os.path.join(target_path, "components", 'libc', 'compilers'))
  197. # change RTT_ROOT in SConstruct
  198. try:
  199. sconstruct = file(os.path.join(BSP_ROOT, 'SConstruct'))
  200. out = file(os.path.join(dist_dir, 'SConstruct'), 'w')
  201. for line in sconstruct:
  202. if line.find('RTT_ROOT') != -1:
  203. if line.find('sys.path') != -1:
  204. out.write('# set RTT_ROOT\n')
  205. out.write("if not os.getenv('RTT_ROOT'): \n RTT_ROOT='rt-thread'\n\n")
  206. out.write(line)
  207. except :
  208. print('')
  209. # change RTT_ROOT in KConfig
  210. try:
  211. if os.path.exists(os.path.join(BSP_ROOT, 'Kconfig')):
  212. Kconfig = file(os.path.join(BSP_ROOT, 'Kconfig'))
  213. out = file(os.path.join(dist_dir, 'Kconfig'), 'w')
  214. found = 0
  215. for line in Kconfig:
  216. if line.find('RTT_ROOT') != -1:
  217. found = 1
  218. if line.find('default') != -1 and found:
  219. position = line.find('default')
  220. line = line[0:position] + 'default: "rt-thread"\n'
  221. found = 0
  222. out.write(line)
  223. out.close()
  224. except :
  225. print('')
  226. # make zip package
  227. import zipfile
  228. zip_filename = os.path.join(BSP_ROOT, 'dist', dist_name)
  229. zip = zipfile.ZipFile(zip_filename + ".zip", 'w')
  230. pre_len = len(os.path.dirname(dist_dir))
  231. for parent, dirnames, filenames in os.walk(dist_dir):
  232. for filename in filenames:
  233. pathfile = os.path.join(parent, filename)
  234. arcname = pathfile[pre_len:].strip(os.path.sep)
  235. zip.write(pathfile, arcname)
  236. zip.close()
  237. print('done!')