utils.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #
  2. # File : utils.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. # Change Logs:
  21. # Date Author Notes
  22. # 2015-01-20 Bernard Add copyright information
  23. #
  24. import sys
  25. import os
  26. def splitall(loc):
  27. """
  28. Return a list of the path components in loc. (Used by relpath_).
  29. The first item in the list will be either ``os.curdir``, ``os.pardir``, empty,
  30. or the root directory of loc (for example, ``/`` or ``C:\\).
  31. The other items in the list will be strings.
  32. Adapted from *path.py* by Jason Orendorff.
  33. """
  34. parts = []
  35. while loc != os.curdir and loc != os.pardir:
  36. prev = loc
  37. loc, child = os.path.split(prev)
  38. if loc == prev:
  39. break
  40. parts.append(child)
  41. parts.append(loc)
  42. parts.reverse()
  43. return parts
  44. def _make_path_relative(origin, dest):
  45. """
  46. Return the relative path between origin and dest.
  47. If it's not possible return dest.
  48. If they are identical return ``os.curdir``
  49. Adapted from `path.py <http://www.jorendorff.com/articles/python/path/>`_ by Jason Orendorff.
  50. """
  51. origin = os.path.abspath(origin).replace('\\', '/')
  52. dest = os.path.abspath(dest).replace('\\', '/')
  53. #
  54. orig_list = splitall(os.path.normcase(origin))
  55. # Don't normcase dest! We want to preserve the case.
  56. dest_list = splitall(dest)
  57. #
  58. if orig_list[0] != os.path.normcase(dest_list[0]):
  59. # Can't get here from there.
  60. return dest
  61. #
  62. # Find the location where the two paths start to differ.
  63. i = 0
  64. for start_seg, dest_seg in zip(orig_list, dest_list):
  65. if start_seg != os.path.normcase(dest_seg):
  66. break
  67. i += 1
  68. #
  69. # Now i is the point where the two paths diverge.
  70. # Need a certain number of "os.pardir"s to work up
  71. # from the origin to the point of divergence.
  72. segments = [os.pardir] * (len(orig_list) - i)
  73. # Need to add the diverging part of dest_list.
  74. segments += dest_list[i:]
  75. if len(segments) == 0:
  76. # If they happen to be identical, use os.curdir.
  77. return os.curdir
  78. else:
  79. # return os.path.join(*segments).replace('\\', '/')
  80. return os.path.join(*segments)
  81. def xml_indent(elem, level=0):
  82. i = "\n" + level*" "
  83. if len(elem):
  84. if not elem.text or not elem.text.strip():
  85. elem.text = i + " "
  86. if not elem.tail or not elem.tail.strip():
  87. elem.tail = i
  88. for elem in elem:
  89. xml_indent(elem, level+1)
  90. if not elem.tail or not elem.tail.strip():
  91. elem.tail = i
  92. else:
  93. if level and (not elem.tail or not elem.tail.strip()):
  94. elem.tail = i
  95. source_ext = ["c", "h", "s", "S", "cpp", "xpm"]
  96. source_list = []
  97. def walk_children(child):
  98. global source_list
  99. global source_ext
  100. # print child
  101. full_path = child.rfile().abspath
  102. file_type = full_path.rsplit('.',1)[1]
  103. #print file_type
  104. if file_type in source_ext:
  105. if full_path not in source_list:
  106. source_list.append(full_path)
  107. children = child.all_children()
  108. if children != []:
  109. for item in children:
  110. walk_children(item)
  111. def PrefixPath(prefix, path):
  112. path = os.path.abspath(path)
  113. prefix = os.path.abspath(prefix)
  114. if sys.platform == 'win32':
  115. prefix = prefix.lower()
  116. path = path.lower()
  117. if path.startswith(prefix):
  118. return True
  119. return False
  120. def ListMap(l):
  121. ret_list = []
  122. for item in l:
  123. if type(item) == type(()):
  124. ret = ListMap(item)
  125. ret_list += ret
  126. elif type(item) == type([]):
  127. ret = ListMap(item)
  128. ret_list += ret
  129. else:
  130. ret_list.append(item)
  131. return ret_list
  132. def TargetGetList(env, postfix):
  133. global source_ext
  134. global source_list
  135. target = env['target']
  136. source_ext = postfix
  137. for item in target:
  138. walk_children(item)
  139. source_list.sort()
  140. return source_list
  141. def ProjectInfo(env):
  142. project = env['project']
  143. RTT_ROOT = env['RTT_ROOT']
  144. BSP_ROOT = env['BSP_ROOT']
  145. FILES = []
  146. DIRS = []
  147. HEADERS = []
  148. CPPPATH = []
  149. CPPDEFINES = []
  150. for group in project:
  151. # get each files
  152. if group.has_key('src') and group['src']:
  153. FILES += group['src']
  154. # get each include path
  155. if group.has_key('CPPPATH') and group['CPPPATH']:
  156. CPPPATH += group['CPPPATH']
  157. if env.has_key('CPPDEFINES'):
  158. CPPDEFINES = env['CPPDEFINES']
  159. CPPDEFINES = ListMap(CPPDEFINES)
  160. # process FILES and DIRS
  161. if len(FILES):
  162. # use absolute path
  163. for i in range(len(FILES)):
  164. FILES[i] = os.path.abspath(str(FILES[i]))
  165. DIRS.append(os.path.dirname(FILES[i]))
  166. FILES.sort()
  167. DIRS = list(set(DIRS))
  168. DIRS.sort()
  169. # process HEADERS
  170. HEADERS = TargetGetList(env, ['h'])
  171. # process CPPPATH
  172. if len(CPPPATH):
  173. # use absolute path
  174. for i in range(len(CPPPATH)):
  175. CPPPATH[i] = os.path.abspath(CPPPATH[i])
  176. # remove repeat path
  177. paths = [i for i in set(CPPPATH)]
  178. CPPPATH = []
  179. for path in paths:
  180. if PrefixPath(RTT_ROOT, path):
  181. CPPPATH += [os.path.abspath(path).replace('\\', '/')]
  182. elif PrefixPath(BSP_ROOT, path):
  183. CPPPATH += [os.path.abspath(path).replace('\\', '/')]
  184. else:
  185. CPPPATH += ['"%s",' % path.replace('\\', '/')]
  186. CPPPATH.sort()
  187. # process CPPDEFINES
  188. if len(CPPDEFINES):
  189. CPPDEFINES = [i for i in set(CPPDEFINES)]
  190. CPPDEFINES.sort()
  191. proj = {}
  192. proj['FILES'] = FILES
  193. proj['DIRS'] = DIRS
  194. proj['HEADERS'] = HEADERS
  195. proj['CPPPATH'] = CPPPATH
  196. proj['CPPDEFINES'] = CPPDEFINES
  197. return proj