| 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: | 
|  | 56 | forcevariable = '_forcevariable' | 
|  | 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): | 
|  | 62 | if '.git' in dirs: | 
|  | 63 | dirs.remove('.git') | 
|  | 64 | for fn in files: | 
|  | 65 | if fn == 'latest_srcrev': | 
|  | 66 | curdir = os.path.basename(os.path.dirname(root)) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | fullpath = os.path.join(root, fn) | 
|  | 68 | pn = os.path.basename(root) | 
|  | 69 | srcrev = None | 
|  | 70 | orig_srcrev = None | 
|  | 71 | orig_srcrevs = {} | 
|  | 72 | srcrevs = {} | 
|  | 73 | with open(fullpath) as f: | 
|  | 74 | for line in f: | 
|  | 75 | if '=' in line: | 
|  | 76 | splitval = line.split('=') | 
|  | 77 | value = splitval[1].strip('" \t\n\r') | 
|  | 78 | if line.startswith('# SRCREV = '): | 
|  | 79 | orig_srcrev = value | 
|  | 80 | elif line.startswith('# SRCREV_'): | 
|  | 81 | splitval = line.split('=') | 
|  | 82 | name = splitval[0].split('_')[1].strip() | 
|  | 83 | orig_srcrevs[name] = value | 
|  | 84 | elif line.startswith('SRCREV ='): | 
|  | 85 | srcrev = value | 
|  | 86 | elif line.startswith('SRCREV_'): | 
|  | 87 | name = splitval[0].split('_')[1].strip() | 
|  | 88 | srcrevs[name] = value | 
|  | 89 | if srcrev and (options.reportall or srcrev != orig_srcrev): | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 90 | all_srcrevs[curdir].append((pn, None, srcrev)) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 91 | for name, value in srcrevs.items(): | 
|  | 92 | orig = orig_srcrevs.get(name, orig_srcrev) | 
|  | 93 | if options.reportall or value != orig: | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 94 | all_srcrevs[curdir].append((pn, name, value)) | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 95 |  | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 96 | for curdir, srcrevs in sorted(all_srcrevs.items()): | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 97 | if srcrevs: | 
|  | 98 | print('# %s' % curdir) | 
|  | 99 | for pn, name, srcrev in srcrevs: | 
|  | 100 | if name: | 
|  | 101 | print('SRCREV_%s_pn-%s%s = "%s"' % (name, pn, forcevariable, srcrev)) | 
|  | 102 | else: | 
|  | 103 | print('SRCREV_pn-%s%s = "%s"' % (pn, forcevariable, srcrev)) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 104 |  | 
|  | 105 |  | 
|  | 106 | if __name__ == "__main__": | 
|  | 107 | main() |