getdeveloperlib.py 6.4 KB

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