blob: d375b045d8d71c3d249cf5a0a85dd38e157a1c3f [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#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License version 2 as
10# published by the Free Software Foundation.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050021import collections
22import os
23import sys
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024import optparse
25import logging
26
27def logger_create():
28 logger = logging.getLogger("buildhistory")
29 loggerhandler = logging.StreamHandler()
30 loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
31 logger.addHandler(loggerhandler)
32 logger.setLevel(logging.INFO)
33 return logger
34
35logger = logger_create()
36
37def main():
38 parser = optparse.OptionParser(
39 description = "Collects the recorded SRCREV values from buildhistory and reports on them.",
40 usage = """
41 %prog [options]""")
42
43 parser.add_option("-a", "--report-all",
44 help = "Report all SRCREV values, not just ones where AUTOREV has been used",
45 action="store_true", dest="reportall")
46 parser.add_option("-f", "--forcevariable",
47 help = "Use forcevariable override for all output lines",
48 action="store_true", dest="forcevariable")
49 parser.add_option("-p", "--buildhistory-dir",
50 help = "Specify path to buildhistory directory (defaults to buildhistory/ under cwd)",
51 action="store", dest="buildhistory_dir", default='buildhistory/')
52
53 options, args = parser.parse_args(sys.argv)
54
55 if len(args) > 1:
56 sys.stderr.write('Invalid argument(s) specified: %s\n\n' % ' '.join(args[1:]))
57 parser.print_help()
58 sys.exit(1)
59
60 if not os.path.exists(options.buildhistory_dir):
61 sys.stderr.write('Buildhistory directory "%s" does not exist\n\n' % options.buildhistory_dir)
62 parser.print_help()
63 sys.exit(1)
64
65 if options.forcevariable:
66 forcevariable = '_forcevariable'
67 else:
68 forcevariable = ''
69
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050070 all_srcrevs = collections.defaultdict(list)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 for root, dirs, files in os.walk(options.buildhistory_dir):
72 if '.git' in dirs:
73 dirs.remove('.git')
74 for fn in files:
75 if fn == 'latest_srcrev':
76 curdir = os.path.basename(os.path.dirname(root))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 fullpath = os.path.join(root, fn)
78 pn = os.path.basename(root)
79 srcrev = None
80 orig_srcrev = None
81 orig_srcrevs = {}
82 srcrevs = {}
83 with open(fullpath) as f:
84 for line in f:
85 if '=' in line:
86 splitval = line.split('=')
87 value = splitval[1].strip('" \t\n\r')
88 if line.startswith('# SRCREV = '):
89 orig_srcrev = value
90 elif line.startswith('# SRCREV_'):
91 splitval = line.split('=')
92 name = splitval[0].split('_')[1].strip()
93 orig_srcrevs[name] = value
94 elif line.startswith('SRCREV ='):
95 srcrev = value
96 elif line.startswith('SRCREV_'):
97 name = splitval[0].split('_')[1].strip()
98 srcrevs[name] = value
99 if srcrev and (options.reportall or srcrev != orig_srcrev):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500100 all_srcrevs[curdir].append((pn, None, srcrev))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 for name, value in srcrevs.items():
102 orig = orig_srcrevs.get(name, orig_srcrev)
103 if options.reportall or value != orig:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500104 all_srcrevs[curdir].append((pn, name, value))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500105
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600106 for curdir, srcrevs in sorted(all_srcrevs.items()):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107 if srcrevs:
108 print('# %s' % curdir)
109 for pn, name, srcrev in srcrevs:
110 if name:
111 print('SRCREV_%s_pn-%s%s = "%s"' % (name, pn, forcevariable, srcrev))
112 else:
113 print('SRCREV_pn-%s%s = "%s"' % (pn, forcevariable, srcrev))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114
115
116if __name__ == "__main__":
117 main()