| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # | 
|  | 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 Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 8 | # SPDX-License-Identifier: GPL-2.0-only | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | # | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 11 | import collections | 
|  | 12 | import os | 
|  | 13 | import sys | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 | import optparse | 
|  | 15 | import logging | 
|  | 16 |  | 
|  | 17 | def 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 |  | 
|  | 25 | logger = logger_create() | 
|  | 26 |  | 
|  | 27 | def 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 Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 56 | forcevariable = ':forcevariable' | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | else: | 
|  | 58 | forcevariable = '' | 
|  | 59 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 60 | all_srcrevs = collections.defaultdict(list) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | for root, dirs, files in os.walk(options.buildhistory_dir): | 
| Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 62 | dirs.sort() | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 91 | all_srcrevs[curdir].append((pn, None, srcrev)) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 92 | for name, value in srcrevs.items(): | 
|  | 93 | orig = orig_srcrevs.get(name, orig_srcrev) | 
|  | 94 | if options.reportall or value != orig: | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 95 | all_srcrevs[curdir].append((pn, name, value)) | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 96 |  | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 97 | for curdir, srcrevs in sorted(all_srcrevs.items()): | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 98 | if srcrevs: | 
|  | 99 | print('# %s' % curdir) | 
|  | 100 | for pn, name, srcrev in srcrevs: | 
|  | 101 | if name: | 
| Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 102 | print('SRCREV_%s:pn-%s%s = "%s"' % (name, pn, forcevariable, srcrev)) | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 103 | else: | 
| Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 104 | print('SRCREV:pn-%s%s = "%s"' % (pn, forcevariable, srcrev)) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 |  | 
|  | 106 |  | 
|  | 107 | if __name__ == "__main__": | 
|  | 108 | main() |