getdeveloperlib.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. from __future__ import print_function
  2. import os
  3. import re
  4. import glob
  5. import subprocess
  6. import sys
  7. #
  8. # Patch parsing functions
  9. #
  10. FIND_INFRA_IN_PATCH = re.compile("^\+\$\(eval \$\((host-)?([^-]*)-package\)\)$")
  11. def analyze_patch(patch):
  12. """Parse one patch and return the list of files modified, added or
  13. removed by the patch."""
  14. files = set()
  15. infras = set()
  16. for line in patch:
  17. # If the patch is adding a package, find which infra it is
  18. m = FIND_INFRA_IN_PATCH.match(line)
  19. if m:
  20. infras.add(m.group(2))
  21. if not line.startswith("+++ "):
  22. continue
  23. line.strip()
  24. fname = line[line.find("/") + 1:].strip()
  25. if fname == "dev/null":
  26. continue
  27. files.add(fname)
  28. return (files, infras)
  29. FIND_INFRA_IN_MK = re.compile("^\$\(eval \$\((host-)?([^-]*)-package\)\)$")
  30. def fname_get_package_infra(fname):
  31. """Checks whether the file name passed as argument is a Buildroot .mk
  32. file describing a package, and find the infrastructure it's using."""
  33. if not fname.endswith(".mk"):
  34. return None
  35. if not os.path.exists(fname):
  36. return None
  37. with open(fname, "r") as f:
  38. for line in f:
  39. line = line.strip()
  40. m = FIND_INFRA_IN_MK.match(line)
  41. if m:
  42. return m.group(2)
  43. return None
  44. def get_infras(files):
  45. """Search in the list of files for .mk files, and collect the package
  46. infrastructures used by those .mk files."""
  47. infras = set()
  48. for fname in files:
  49. infra = fname_get_package_infra(fname)
  50. if infra:
  51. infras.add(infra)
  52. return infras
  53. def analyze_patches(patches):
  54. """Parse a list of patches and returns the list of files modified,
  55. added or removed by the patches, as well as the list of package
  56. infrastructures used by those patches (if any)"""
  57. allfiles = set()
  58. allinfras = set()
  59. for patch in patches:
  60. (files, infras) = analyze_patch(patch)
  61. allfiles = allfiles | files
  62. allinfras = allinfras | infras
  63. allinfras = allinfras | get_infras(allfiles)
  64. return (allfiles, allinfras)
  65. #
  66. # DEVELOPERS file parsing functions
  67. #
  68. class Developer:
  69. def __init__(self, name, files):
  70. self.name = name
  71. self.files = files
  72. self.packages = parse_developer_packages(files)
  73. self.architectures = parse_developer_architectures(files)
  74. self.infras = parse_developer_infras(files)
  75. def hasfile(self, f):
  76. f = os.path.abspath(f)
  77. for fs in self.files:
  78. if f.startswith(fs):
  79. return True
  80. return False
  81. def parse_developer_packages(fnames):
  82. """Given a list of file patterns, travel through the Buildroot source
  83. tree to find which packages are implemented by those file
  84. patterns, and return a list of those packages."""
  85. packages = set()
  86. for fname in fnames:
  87. for root, dirs, files in os.walk(fname):
  88. for f in files:
  89. path = os.path.join(root, f)
  90. if fname_get_package_infra(path):
  91. pkg = os.path.splitext(f)[0]
  92. packages.add(pkg)
  93. return packages
  94. def parse_arches_from_config_in(fname):
  95. """Given a path to an arch/Config.in.* file, parse it to get the list
  96. of BR2_ARCH values for this architecture."""
  97. arches = set()
  98. with open(fname, "r") as f:
  99. parsing_arches = False
  100. for line in f:
  101. line = line.strip()
  102. if line == "config BR2_ARCH":
  103. parsing_arches = True
  104. continue
  105. if parsing_arches:
  106. m = re.match("^\s*default \"([^\"]*)\".*", line)
  107. if m:
  108. arches.add(m.group(1))
  109. else:
  110. parsing_arches = False
  111. return arches
  112. def parse_developer_architectures(fnames):
  113. """Given a list of file names, find the ones starting by
  114. 'arch/Config.in.', and use that to determine the architecture a
  115. developer is working on."""
  116. arches = set()
  117. for fname in fnames:
  118. if not re.match("^.*/arch/Config\.in\..*$", fname):
  119. continue
  120. arches = arches | parse_arches_from_config_in(fname)
  121. return arches
  122. def parse_developer_infras(fnames):
  123. infras = set()
  124. for fname in fnames:
  125. m = re.match("^package/pkg-([^.]*).mk$", fname)
  126. if m:
  127. infras.add(m.group(1))
  128. return infras
  129. def parse_developers(basepath=None):
  130. """Parse the DEVELOPERS file and return a list of Developer objects."""
  131. developers = []
  132. linen = 0
  133. if basepath is None:
  134. basepath = os.getcwd()
  135. with open(os.path.join(basepath, "DEVELOPERS"), "r") as f:
  136. files = []
  137. name = None
  138. for line in f:
  139. line = line.strip()
  140. if line.startswith("#"):
  141. continue
  142. elif line.startswith("N:"):
  143. if name is not None or len(files) != 0:
  144. print("Syntax error in DEVELOPERS file, line %d" % linen,
  145. file=sys.stderr)
  146. name = line[2:].strip()
  147. elif line.startswith("F:"):
  148. fname = line[2:].strip()
  149. dev_files = glob.glob(os.path.join(basepath, fname))
  150. if len(dev_files) == 0:
  151. print("WARNING: '%s' doesn't match any file" % fname,
  152. file=sys.stderr)
  153. files += dev_files
  154. elif line == "":
  155. if not name:
  156. continue
  157. developers.append(Developer(name, files))
  158. files = []
  159. name = None
  160. else:
  161. print("Syntax error in DEVELOPERS file, line %d: '%s'" % (linen, line),
  162. file=sys.stderr)
  163. return None
  164. linen += 1
  165. # handle last developer
  166. if name is not None:
  167. developers.append(Developer(name, files))
  168. return developers
  169. def check_developers(developers, basepath=None):
  170. """Look at the list of files versioned in Buildroot, and returns the
  171. list of files that are not handled by any developer"""
  172. if basepath is None:
  173. basepath = os.getcwd()
  174. cmd = ["git", "--git-dir", os.path.join(basepath, ".git"), "ls-files"]
  175. files = subprocess.check_output(cmd).strip().split("\n")
  176. unhandled_files = []
  177. for f in files:
  178. handled = False
  179. for d in developers:
  180. if d.hasfile(os.path.join(basepath, f)):
  181. handled = True
  182. break
  183. if not handled:
  184. unhandled_files.append(f)
  185. return unhandled_files