bitbake-whatchanged 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2013 Wind River Systems, Inc.
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. #
  7. import os
  8. import sys
  9. import getopt
  10. import shutil
  11. import re
  12. import warnings
  13. import subprocess
  14. import argparse
  15. scripts_path = os.path.abspath(os.path.dirname(os.path.abspath(sys.argv[0])))
  16. lib_path = scripts_path + '/lib'
  17. sys.path = sys.path + [lib_path]
  18. import scriptpath
  19. # Figure out where is the bitbake/lib/bb since we need bb.siggen and bb.process
  20. bitbakepath = scriptpath.add_bitbake_lib_path()
  21. if not bitbakepath:
  22. sys.stderr.write("Unable to find bitbake by searching parent directory of this script or PATH\n")
  23. sys.exit(1)
  24. scriptpath.add_oe_lib_path()
  25. import argparse_oe
  26. import bb.siggen
  27. import bb.process
  28. # Match the stamp's filename
  29. # group(1): PE_PV (may no PE)
  30. # group(2): PR
  31. # group(3): TASK
  32. # group(4): HASH
  33. stamp_re = re.compile("(?P<pv>.*)-(?P<pr>r\d+)\.(?P<task>do_\w+)\.(?P<hash>[^\.]*)")
  34. sigdata_re = re.compile(".*\.sigdata\..*")
  35. def gen_dict(stamps):
  36. """
  37. Generate the dict from the stamps dir.
  38. The output dict format is:
  39. {fake_f: {pn: PN, pv: PV, pr: PR, task: TASK, path: PATH}}
  40. Where:
  41. fake_f: pv + task + hash
  42. path: the path to the stamp file
  43. """
  44. # The member of the sub dict (A "path" will be appended below)
  45. sub_mem = ("pv", "pr", "task")
  46. d = {}
  47. for dirpath, _, files in os.walk(stamps):
  48. for f in files:
  49. # The "bitbake -S" would generate ".sigdata", but no "_setscene".
  50. fake_f = re.sub('_setscene.', '.', f)
  51. fake_f = re.sub('.sigdata', '', fake_f)
  52. subdict = {}
  53. tmp = stamp_re.match(fake_f)
  54. if tmp:
  55. for i in sub_mem:
  56. subdict[i] = tmp.group(i)
  57. if len(subdict) != 0:
  58. pn = os.path.basename(dirpath)
  59. subdict['pn'] = pn
  60. # The path will be used by os.stat() and bb.siggen
  61. subdict['path'] = dirpath + "/" + f
  62. fake_f = tmp.group('pv') + tmp.group('task') + tmp.group('hash')
  63. d[fake_f] = subdict
  64. return d
  65. # Re-construct the dict
  66. def recon_dict(dict_in):
  67. """
  68. The output dict format is:
  69. {pn_task: {pv: PV, pr: PR, path: PATH}}
  70. """
  71. dict_out = {}
  72. for k in dict_in.keys():
  73. subdict = {}
  74. # The key
  75. pn_task = "%s_%s" % (dict_in.get(k).get('pn'), dict_in.get(k).get('task'))
  76. # If more than one stamps are found, use the latest one.
  77. if pn_task in dict_out:
  78. full_path_pre = dict_out.get(pn_task).get('path')
  79. full_path_cur = dict_in.get(k).get('path')
  80. if os.stat(full_path_pre).st_mtime > os.stat(full_path_cur).st_mtime:
  81. continue
  82. subdict['pv'] = dict_in.get(k).get('pv')
  83. subdict['pr'] = dict_in.get(k).get('pr')
  84. subdict['path'] = dict_in.get(k).get('path')
  85. dict_out[pn_task] = subdict
  86. return dict_out
  87. def split_pntask(s):
  88. """
  89. Split the pn_task in to (pn, task) and return it
  90. """
  91. tmp = re.match("(.*)_(do_.*)", s)
  92. return (tmp.group(1), tmp.group(2))
  93. def print_added(d_new = None, d_old = None):
  94. """
  95. Print the newly added tasks
  96. """
  97. added = {}
  98. for k in list(d_new.keys()):
  99. if k not in d_old:
  100. # Add the new one to added dict, and remove it from
  101. # d_new, so the remaining ones are the changed ones
  102. added[k] = d_new.get(k)
  103. del(d_new[k])
  104. if not added:
  105. return 0
  106. # Format the output, the dict format is:
  107. # {pn: task1, task2 ...}
  108. added_format = {}
  109. counter = 0
  110. for k in added.keys():
  111. pn, task = split_pntask(k)
  112. if pn in added_format:
  113. # Append the value
  114. added_format[pn] = "%s %s" % (added_format.get(pn), task)
  115. else:
  116. added_format[pn] = task
  117. counter += 1
  118. print("=== Newly added tasks: (%s tasks)" % counter)
  119. for k in added_format.keys():
  120. print(" %s: %s" % (k, added_format.get(k)))
  121. return counter
  122. def print_vrchanged(d_new = None, d_old = None, vr = None):
  123. """
  124. Print the pv or pr changed tasks.
  125. The arg "vr" is "pv" or "pr"
  126. """
  127. pvchanged = {}
  128. counter = 0
  129. for k in list(d_new.keys()):
  130. if d_new.get(k).get(vr) != d_old.get(k).get(vr):
  131. counter += 1
  132. pn, task = split_pntask(k)
  133. if pn not in pvchanged:
  134. # Format the output, we only print pn (no task) since
  135. # all the tasks would be changed when pn or pr changed,
  136. # the dict format is:
  137. # {pn: pv/pr_old -> pv/pr_new}
  138. pvchanged[pn] = "%s -> %s" % (d_old.get(k).get(vr), d_new.get(k).get(vr))
  139. del(d_new[k])
  140. if not pvchanged:
  141. return 0
  142. print("\n=== %s changed: (%s tasks)" % (vr.upper(), counter))
  143. for k in pvchanged.keys():
  144. print(" %s: %s" % (k, pvchanged.get(k)))
  145. return counter
  146. def print_depchanged(d_new = None, d_old = None, verbose = False):
  147. """
  148. Print the dependency changes
  149. """
  150. depchanged = {}
  151. counter = 0
  152. for k in d_new.keys():
  153. counter += 1
  154. pn, task = split_pntask(k)
  155. if (verbose):
  156. full_path_old = d_old.get(k).get("path")
  157. full_path_new = d_new.get(k).get("path")
  158. # No counter since it is not ready here
  159. if sigdata_re.match(full_path_old) and sigdata_re.match(full_path_new):
  160. output = bb.siggen.compare_sigfiles(full_path_old, full_path_new)
  161. if output:
  162. print("\n=== The verbose changes of %s.%s:" % (pn, task))
  163. print('\n'.join(output))
  164. else:
  165. # Format the output, the format is:
  166. # {pn: task1, task2, ...}
  167. if pn in depchanged:
  168. depchanged[pn] = "%s %s" % (depchanged.get(pn), task)
  169. else:
  170. depchanged[pn] = task
  171. if len(depchanged) > 0:
  172. print("\n=== Dependencies changed: (%s tasks)" % counter)
  173. for k in depchanged.keys():
  174. print(" %s: %s" % (k, depchanged[k]))
  175. return counter
  176. def main():
  177. """
  178. Print what will be done between the current and last builds:
  179. 1) Run "STAMPS_DIR=<path> bitbake -S recipe" to re-generate the stamps
  180. 2) Figure out what are newly added and changed, can't figure out
  181. what are removed since we can't know the previous stamps
  182. clearly, for example, if there are several builds, we can't know
  183. which stamps the last build has used exactly.
  184. 3) Use bb.siggen.compare_sigfiles to diff the old and new stamps
  185. """
  186. parser = argparse_oe.ArgumentParser(usage = """%(prog)s [options] [package ...]
  187. print what will be done between the current and last builds, for example:
  188. $ bitbake core-image-sato
  189. # Edit the recipes
  190. $ bitbake-whatchanged core-image-sato
  191. The changes will be printed.
  192. Note:
  193. The amount of tasks is not accurate when the task is "do_build" since
  194. it usually depends on other tasks.
  195. The "nostamp" task is not included.
  196. """
  197. )
  198. parser.add_argument("recipe", help="recipe to check")
  199. parser.add_argument("-v", "--verbose", help = "print the verbose changes", action = "store_true")
  200. args = parser.parse_args()
  201. # Get the STAMPS_DIR
  202. print("Figuring out the STAMPS_DIR ...")
  203. cmdline = "bitbake -e | sed -ne 's/^STAMPS_DIR=\"\(.*\)\"/\\1/p'"
  204. try:
  205. stampsdir, err = bb.process.run(cmdline)
  206. except:
  207. raise
  208. if not stampsdir:
  209. print("ERROR: No STAMPS_DIR found for '%s'" % args.recipe, file=sys.stderr)
  210. return 2
  211. stampsdir = stampsdir.rstrip("\n")
  212. if not os.path.isdir(stampsdir):
  213. print("ERROR: stamps directory \"%s\" not found!" % stampsdir, file=sys.stderr)
  214. return 2
  215. # The new stamps dir
  216. new_stampsdir = stampsdir + ".bbs"
  217. if os.path.exists(new_stampsdir):
  218. print("ERROR: %s already exists!" % new_stampsdir, file=sys.stderr)
  219. return 2
  220. try:
  221. # Generate the new stamps dir
  222. print("Generating the new stamps ... (need several minutes)")
  223. cmdline = "STAMPS_DIR=%s bitbake -S none %s" % (new_stampsdir, args.recipe)
  224. # FIXME
  225. # The "bitbake -S" may fail, not fatal error, the stamps will still
  226. # be generated, this might be a bug of "bitbake -S".
  227. try:
  228. bb.process.run(cmdline)
  229. except Exception as exc:
  230. print(exc)
  231. # The dict for the new and old stamps.
  232. old_dict = gen_dict(stampsdir)
  233. new_dict = gen_dict(new_stampsdir)
  234. # Remove the same one from both stamps.
  235. cnt_unchanged = 0
  236. for k in list(new_dict.keys()):
  237. if k in old_dict:
  238. cnt_unchanged += 1
  239. del(new_dict[k])
  240. del(old_dict[k])
  241. # Re-construct the dict to easily find out what is added or changed.
  242. # The dict format is:
  243. # {pn_task: {pv: PV, pr: PR, path: PATH}}
  244. new_recon = recon_dict(new_dict)
  245. old_recon = recon_dict(old_dict)
  246. del new_dict
  247. del old_dict
  248. # Figure out what are changed, the new_recon would be changed
  249. # by the print_xxx function.
  250. # Newly added
  251. cnt_added = print_added(new_recon, old_recon)
  252. # PV (including PE) and PR changed
  253. # Let the bb.siggen handle them if verbose
  254. cnt_rv = {}
  255. if not args.verbose:
  256. for i in ('pv', 'pr'):
  257. cnt_rv[i] = print_vrchanged(new_recon, old_recon, i)
  258. # Dependencies changed (use bitbake-diffsigs)
  259. cnt_dep = print_depchanged(new_recon, old_recon, args.verbose)
  260. total_changed = cnt_added + (cnt_rv.get('pv') or 0) + (cnt_rv.get('pr') or 0) + cnt_dep
  261. print("\n=== Summary: (%s changed, %s unchanged)" % (total_changed, cnt_unchanged))
  262. if args.verbose:
  263. print("Newly added: %s\nDependencies changed: %s\n" % \
  264. (cnt_added, cnt_dep))
  265. else:
  266. print("Newly added: %s\nPV changed: %s\nPR changed: %s\nDependencies changed: %s\n" % \
  267. (cnt_added, cnt_rv.get('pv') or 0, cnt_rv.get('pr') or 0, cnt_dep))
  268. except:
  269. print("ERROR occurred!")
  270. raise
  271. finally:
  272. # Remove the newly generated stamps dir
  273. if os.path.exists(new_stampsdir):
  274. print("Removing the newly generated stamps dir ...")
  275. shutil.rmtree(new_stampsdir)
  276. if __name__ == "__main__":
  277. sys.exit(main())