bitbake-whatchanged 11 KB

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