getdeveloperlib.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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(r"^\+\$\(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(r"^\$\(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. self.defconfigs = parse_developer_defconfigs(files)
  101. def hasfile(self, f):
  102. f = os.path.abspath(f)
  103. for fs in self.files:
  104. if f.startswith(fs):
  105. return True
  106. return False
  107. def __repr__(self):
  108. name = '\'' + self.name.split(' <')[0][:20] + '\''
  109. things = []
  110. if len(self.files):
  111. things.append('{} files'.format(len(self.files)))
  112. if len(self.packages):
  113. things.append('{} pkgs'.format(len(self.packages)))
  114. if len(self.architectures):
  115. things.append('{} archs'.format(len(self.architectures)))
  116. if len(self.infras):
  117. things.append('{} infras'.format(len(self.infras)))
  118. if len(self.runtime_tests):
  119. things.append('{} tests'.format(len(self.runtime_tests)))
  120. if len(self.defconfigs):
  121. things.append('{} defconfigs'.format(len(self.defconfigs)))
  122. if things:
  123. return 'Developer <{} ({})>'.format(name, ', '.join(things))
  124. else:
  125. return 'Developer <' + name + '>'
  126. def parse_developer_packages(fnames):
  127. """Given a list of file patterns, travel through the Buildroot source
  128. tree to find which packages are implemented by those file
  129. patterns, and return a list of those packages."""
  130. packages = set()
  131. for fname in fnames:
  132. for root, dirs, files in os.walk(fname):
  133. for f in files:
  134. path = os.path.join(root, f)
  135. if fname_get_package_infra(path):
  136. pkg = os.path.splitext(f)[0]
  137. packages.add(pkg)
  138. return packages
  139. def parse_arches_from_config_in(fname):
  140. """Given a path to an arch/Config.in.* file, parse it to get the list
  141. of BR2_ARCH values for this architecture."""
  142. arches = set()
  143. with open(fname, "r") as f:
  144. parsing_arches = False
  145. for line in f:
  146. line = line.strip()
  147. if line == "config BR2_ARCH":
  148. parsing_arches = True
  149. continue
  150. if parsing_arches:
  151. m = re.match(r"^\s*default \"([^\"]*)\".*", line)
  152. if m:
  153. arches.add(m.group(1))
  154. else:
  155. parsing_arches = False
  156. return arches
  157. def parse_developer_architectures(fnames):
  158. """Given a list of file names, find the ones starting by
  159. 'arch/Config.in.', and use that to determine the architecture a
  160. developer is working on."""
  161. arches = set()
  162. for fname in fnames:
  163. if not re.match(r"^.*/arch/Config\.in\..*$", fname):
  164. continue
  165. arches = arches | parse_arches_from_config_in(fname)
  166. return arches
  167. def parse_developer_infras(fnames):
  168. infras = set()
  169. for fname in fnames:
  170. m = re.match(r"^package/pkg-([^.]*).mk$", fname)
  171. if m:
  172. infras.add(m.group(1))
  173. return infras
  174. def parse_developer_defconfigs(fnames):
  175. """Given a list of file names, returns the config names
  176. corresponding to defconfigs."""
  177. return {os.path.basename(fname[:-10])
  178. for fname in fnames
  179. if fname.endswith('_defconfig')}
  180. def parse_developer_runtime_tests(fnames):
  181. """Given a list of file names, returns the runtime tests
  182. corresponding to the file."""
  183. all_files = []
  184. # List all files recursively
  185. for fname in fnames:
  186. if os.path.isdir(fname):
  187. for root, _dirs, files in os.walk(fname):
  188. all_files += [os.path.join(root, f) for f in files]
  189. else:
  190. all_files.append(fname)
  191. # Get all runtime tests
  192. runtimes = set()
  193. for f in all_files:
  194. name = os.path.splitext(f)[0]
  195. if name in unittests:
  196. runtimes |= set(unittests[name])
  197. return runtimes
  198. def parse_developers(basepath=None):
  199. """Parse the DEVELOPERS file and return a list of Developer objects."""
  200. developers = []
  201. linen = 0
  202. if basepath is None:
  203. basepath = os.getcwd()
  204. global unittests
  205. unittests = list_unittests(os.path.join(basepath, 'support/testing'))
  206. with open(os.path.join(basepath, "DEVELOPERS"), "r") as f:
  207. files = []
  208. name = None
  209. for line in f:
  210. line = line.strip()
  211. if line.startswith("#"):
  212. continue
  213. elif line.startswith("N:"):
  214. if name is not None or len(files) != 0:
  215. print("Syntax error in DEVELOPERS file, line %d" % linen,
  216. file=sys.stderr)
  217. name = line[2:].strip()
  218. elif line.startswith("F:"):
  219. fname = line[2:].strip()
  220. dev_files = glob.glob(os.path.join(basepath, fname))
  221. if len(dev_files) == 0:
  222. print("WARNING: '%s' doesn't match any file" % fname,
  223. file=sys.stderr)
  224. files += dev_files
  225. elif line == "":
  226. if not name:
  227. continue
  228. developers.append(Developer(name, files))
  229. files = []
  230. name = None
  231. else:
  232. print("Syntax error in DEVELOPERS file, line %d: '%s'" % (linen, line),
  233. file=sys.stderr)
  234. return None
  235. linen += 1
  236. # handle last developer
  237. if name is not None:
  238. developers.append(Developer(name, files))
  239. return developers
  240. def check_developers(developers, basepath=None):
  241. """Look at the list of files versioned in Buildroot, and returns the
  242. list of files that are not handled by any developer"""
  243. if basepath is None:
  244. basepath = os.getcwd()
  245. cmd = ["git", "--git-dir", os.path.join(basepath, ".git"), "ls-files"]
  246. files = subprocess.check_output(cmd).strip().split("\n")
  247. unhandled_files = []
  248. for f in files:
  249. handled = False
  250. for d in developers:
  251. if d.hasfile(os.path.join(basepath, f)):
  252. handled = True
  253. break
  254. if not handled:
  255. unhandled_files.append(f)
  256. return unhandled_files