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 | # |
| 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 21 | import collections |
| 22 | import os |
| 23 | import sys |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | import optparse |
| 25 | import logging |
| 26 | |
| 27 | def 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 | |
| 35 | logger = logger_create() |
| 36 | |
| 37 | def 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 70 | all_srcrevs = collections.defaultdict(list) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 100 | all_srcrevs[curdir].append((pn, None, srcrev)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | for name, value in srcrevs.items(): |
| 102 | orig = orig_srcrevs.get(name, orig_srcrev) |
| 103 | if options.reportall or value != orig: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 104 | all_srcrevs[curdir].append((pn, name, value)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 105 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 106 | for curdir, srcrevs in sorted(all_srcrevs.items()): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 107 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 114 | |
| 115 | |
| 116 | if __name__ == "__main__": |
| 117 | main() |