getdeveloperlib.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. from __future__ import print_function
  2. import os
  3. import re
  4. import glob
  5. import subprocess
  6. import sys
  7. import unittest
  8. #
  9. # Patch parsing functions
  10. #
  11. FIND_INFRA_IN_PATCH = re.compile("^\+\$\(eval \$\((host-)?([^-]*)-package\)\)$")
  12. def analyze_patch(patch):
  13. """Parse one patch and return the list of files modified, added or
  14. removed by the patch."""
  15. files = set()
  16. infras = set()
  17. for line in patch:
  18. # If the patch is adding a package, find which infra it is
  19. m = FIND_INFRA_IN_PATCH.match(line)
  20. if m:
  21. infras.add(m.group(2))
  22. if not line.startswith("+++ "):
  23. continue
  24. line.strip()
  25. fname = line[line.find("/") + 1:].strip()
  26. if fname == "dev/null":
  27. continue
  28. files.add(fname)
  29. return (files, infras)
  30. FIND_INFRA_IN_MK = re.compile("^\$\(eval \$\((host-)?([^-]*)-package\)\)$")
  31. def fname_get_package_infra(fname):
  32. """Checks whether the file name passed as argument is a Buildroot .mk
  33. file describing a package, and find the infrastructure it's using."""
  34. if not fname.endswith(".mk"):
  35. return None
  36. if not os.path.exists(fname):
  37. return None
  38. with open(fname, "r") as f:
  39. for line in f:
  40. line = line.strip()
  41. m = FIND_INFRA_IN_MK.match(line)
  42. if m:
  43. return m.group(2)
  44. return None
  45. def get_infras(files):
  46. """Search in the list of files for .mk files, and collect the package
  47. infrastructures used by those .mk files."""
  48. infras = set()
  49. for fname in files:
  50. infra = fname_get_package_infra(fname)
  51. if infra:
  52. infras.add(infra)
  53. return infras
  54. def analyze_patches(patches):
  55. """Parse a list of patches and returns the list of files modified,
  56. added or removed by the patches, as well as the list of package
  57. infrastructures used by those patches (if any)"""
  58. allfiles = set()
  59. allinfras = set()
  60. for patch in patches:
  61. (files, infras) = analyze_patch(patch)
  62. allfiles = allfiles | files
  63. allinfras = allinfras | infras
  64. allinfras = allinfras | get_infras(allfiles)
  65. return (allfiles, allinfras)
  66. #
  67. # Unit-test parsing functions
  68. #
  69. def get_all_test_cases(suite):
  70. """Generate all test-cases from a given test-suite.
  71. :return: (test.module, test.name)"""
  72. if issubclass(type(suite), unittest.TestSuite):
  73. for test in suite:
  74. for res in get_all_test_cases(test):
  75. yield res
  76. else:
  77. yield (suite.__module__, suite.__class__.__name__)
  78. def list_unittests(path):
  79. """Use the unittest module to retreive all test cases from a given
  80. directory"""
  81. loader = unittest.TestLoader()
  82. suite = loader.discover(path)
  83. tests = {}
  84. for module, test in get_all_test_cases(suite):
  85. module_path = os.path.join(path, *module.split('.'))
  86. tests.setdefault(module_path, []).append('%s.%s' % (module, test))
  87. return tests
  88. unittests = {}
  89. #
  90. # DEVELOPERS file parsing functions
  91. #
  92. class Developer:
  93. def __init__(self, name, files):
  94. self.name = name
  95. self.files = files
  96. self.packages = parse_developer_packages(files)
  97. self.architectures = parse_developer_architectures(files)
  98. self.infras = parse_developer_infras(files)
  99. self.runtime_tests = parse_developer_runtime_tests(files)
  100. def hasfile(self, f):
  101. f = os.path.abspath(f)
  102. for fs in self.files:
  103. if f.startswith(fs):
  104. return True
  105. return False
  106. def __repr__(self):
  107. name = '\'' + self.name.split(' <')[0][:20] + '\''
  108. things = []
  109. if len(self.files):
  110. things.append('{} files'.format(len(self.files)))
  111. if len(self.packages):
  112. things.append('{} pkgs'.format(len(self.packages)))
  113. if len(self.architectures):
  114. things.append('{} archs'.format(len(self.architectures)))
  115. if len(self.infras):
  116. things.append('{} infras'.format(len(self.infras)))
  117. if len(self.runtime_tests):
  118. things.append('{} tests'.format(len(self.runtime_tests)))
  119. if things:
  120. return 'Developer <{} ({})>'.format(name, ', '.join(things))
  121. else:
  122. return 'Developer <' + name + '>'
  123. def parse_developer_packages(fnames):
  124. """Given a list of file patterns, travel through the Buildroot source
  125. tree to find which packages are implemented by those file
  126. patterns, and return a list of those packages."""
  127. packages = set()
  128. for fname in fnames:
  129. for root, dirs, files in os.walk(fname):
  130. for f in files:
  131. path = os.path.join(root, f)
  132. if fname_get_package_infra(path):
  133. pkg = os.path.splitext(f)[0]
  134. packages.add(pkg)
  135. return packages
  136. def parse_arches_from_config_in(fname):
  137. """Given a path to an arch/Config.in.* file, parse it to get the list
  138. of BR2_ARCH values for this architecture."""
  139. arches = set()
  140. with open(fname, "r") as f:
  141. parsing_arches = False
  142. for line in f:
  143. line = line.strip()
  144. if line == "config BR2_ARCH":
  145. parsing_arches = True
  146. continue
  147. if parsing_arches:
  148. m = re.match("^\s*default \"([^\"]*)\".*", line)
  149. if m:
  150. arches.add(m.group(1))
  151. else:
  152. parsing_arches = False
  153. return arches
  154. def parse_developer_architectures(fnames):
  155. """Given a list of file names, find the ones starting by
  156. 'arch/Config.in.', and use that to determine the architecture a
  157. developer is working on."""
  158. arches = set()
  159. for fname in fnames:
  160. if not re.match("^.*/arch/Config\.in\..*$", fname):
  161. continue
  162. arches = arches | parse_arches_from_config_in(fname)
  163. return arches
  164. def parse_developer_infras(fnames):
  165. infras = set()
  166. for fname in fnames:
  167. m = re.match("^package/pkg-([^.]*).mk$", fname)
  168. if m:
  169. infras.add(m.group(1))
  170. return infras
  171. def parse_developer_runtime_tests(fnames):
  172. """Given a list of file names, returns the runtime tests
  173. corresponding to the file."""
  174. all_files = []
  175. # List all files recursively
  176. for fname in fnames:
  177. if os.path.isdir(fname):
  178. for root, _dirs, files in os.walk(fname):
  179. all_files += [os.path.join(root, f) for f in files]
  180. else:
  181. all_files.append(fname)
  182. # Get all runtime tests
  183. runtimes = set()
  184. for f in all_files:
  185. name = os.path.splitext(f)[0]
  186. if name in unittests:
  187. runtimes |= set(unittests[name])
  188. return runtimes
  189. def parse_developers(basepath=None):
  190. """Parse the DEVELOPERS file and return a list of Developer objects."""
  191. developers = []
  192. linen = 0
  193. if basepath is None:
  194. basepath = os.getcwd()
  195. global unittests
  196. unittests = list_unittests(os.path.join(basepath, 'support/testing'))
  197. with open(os.path.join(basepath, "DEVELOPERS"), "r") as f:
  198. files = []
  199. name = None
  200. for line in f:
  201. line = line.strip()
  202. if line.startswith("#"):
  203. continue
  204. elif line.startswith("N:"):
  205. if name is not None or len(files) != 0:
  206. print("Syntax error in DEVELOPERS file, line %d" % linen,
  207. file=sys.stderr)
  208. name = line[2:].strip()
  209. elif line.startswith("F:"):
  210. fname = line[2:].strip()
  211. dev_files = glob.glob(os.path.join(basepath, fname))
  212. if len(dev_files) == 0:
  213. print("WARNING: '%s' doesn't match any file" % fname,
  214. file=sys.stderr)
  215. files += dev_files
  216. elif line == "":
  217. if not name:
  218. continue
  219. developers.append(Developer(name, files))
  220. files = []
  221. name = None
  222. else:
  223. print("Syntax error in DEVELOPERS file, line %d: '%s'" % (linen, line),
  224. file=sys.stderr)
  225. return None
  226. linen += 1
  227. # handle last developer
  228. if name is not None:
  229. developers.append(Developer(name, files))
  230. return developers
  231. def check_developers(developers, basepath=None):
  232. """Look at the list of files versioned in Buildroot, and returns the
  233. list of files that are not handled by any developer"""
  234. if basepath is None:
  235. basepath = os.getcwd()
  236. cmd = ["git", "--git-dir", os.path.join(basepath, ".git"), "ls-files"]
  237. files = subprocess.check_output(cmd).strip().split("\n")
  238. unhandled_files = []
  239. for f in files:
  240. handled = False
  241. for d in developers:
  242. if d.hasfile(os.path.join(basepath, f)):
  243. handled = True
  244. break
  245. if not handled:
  246. unhandled_files.append(f)
  247. return unhandled_files