blob: c937e49c2a5906609b4e02945ebbd81faa8c990f [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002#
3# Collects the recorded SRCREV values from buildhistory and reports on them
4#
5# Copyright 2013 Intel Corporation
6# Authored-by: Paul Eggleton <paul.eggleton@intel.com>
7#
Brad Bishopc342db32019-05-15 21:57:59 -04008# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050011import collections
12import os
13import sys
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014import optparse
15import logging
16
17def logger_create():
18 logger = logging.getLogger("buildhistory")
19 loggerhandler = logging.StreamHandler()
20 loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
21 logger.addHandler(loggerhandler)
22 logger.setLevel(logging.INFO)
23 return logger
24
25logger = logger_create()
26
27def main():
28 parser = optparse.OptionParser(
29 description = "Collects the recorded SRCREV values from buildhistory and reports on them.",
30 usage = """
31 %prog [options]""")
32
33 parser.add_option("-a", "--report-all",
34 help = "Report all SRCREV values, not just ones where AUTOREV has been used",
35 action="store_true", dest="reportall")
36 parser.add_option("-f", "--forcevariable",
37 help = "Use forcevariable override for all output lines",
38 action="store_true", dest="forcevariable")
39 parser.add_option("-p", "--buildhistory-dir",
40 help = "Specify path to buildhistory directory (defaults to buildhistory/ under cwd)",
41 action="store", dest="buildhistory_dir", default='buildhistory/')
42
43 options, args = parser.parse_args(sys.argv)
44
45 if len(args) > 1:
46 sys.stderr.write('Invalid argument(s) specified: %s\n\n' % ' '.join(args[1:]))
47 parser.print_help()
48 sys.exit(1)
49
50 if not os.path.exists(options.buildhistory_dir):
51 sys.stderr.write('Buildhistory directory "%s" does not exist\n\n' % options.buildhistory_dir)
52 parser.print_help()
53 sys.exit(1)
54
55 if options.forcevariable:
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050056 forcevariable = ':forcevariable'
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 else:
58 forcevariable = ''
59
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050060 all_srcrevs = collections.defaultdict(list)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 for root, dirs, files in os.walk(options.buildhistory_dir):
Andrew Geisslerd25ed322020-06-27 00:28:28 -050062 dirs.sort()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 if '.git' in dirs:
64 dirs.remove('.git')
65 for fn in files:
66 if fn == 'latest_srcrev':
67 curdir = os.path.basename(os.path.dirname(root))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068 fullpath = os.path.join(root, fn)
69 pn = os.path.basename(root)
70 srcrev = None
71 orig_srcrev = None
72 orig_srcrevs = {}
73 srcrevs = {}
74 with open(fullpath) as f:
75 for line in f:
76 if '=' in line:
77 splitval = line.split('=')
78 value = splitval[1].strip('" \t\n\r')
79 if line.startswith('# SRCREV = '):
80 orig_srcrev = value
81 elif line.startswith('# SRCREV_'):
82 splitval = line.split('=')
83 name = splitval[0].split('_')[1].strip()
84 orig_srcrevs[name] = value
85 elif line.startswith('SRCREV ='):
86 srcrev = value
87 elif line.startswith('SRCREV_'):
88 name = splitval[0].split('_')[1].strip()
89 srcrevs[name] = value
90 if srcrev and (options.reportall or srcrev != orig_srcrev):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050091 all_srcrevs[curdir].append((pn, None, srcrev))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 for name, value in srcrevs.items():
93 orig = orig_srcrevs.get(name, orig_srcrev)
94 if options.reportall or value != orig:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050095 all_srcrevs[curdir].append((pn, name, value))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050096
Patrick Williamsc0f7c042017-02-23 20:41:17 -060097 for curdir, srcrevs in sorted(all_srcrevs.items()):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050098 if srcrevs:
99 print('# %s' % curdir)
100 for pn, name, srcrev in srcrevs:
101 if name:
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500102 print('SRCREV_%s:pn-%s%s = "%s"' % (name, pn, forcevariable, srcrev))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500103 else:
Patrick Williams213cb262021-08-07 19:21:33 -0500104 print('SRCREV:pn-%s%s = "%s"' % (pn, forcevariable, srcrev))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105
106
107if __name__ == "__main__":
108 main()