buildhistory-collect-srcrevs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python3
  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. import collections
  21. import os
  22. import sys
  23. import optparse
  24. import logging
  25. def logger_create():
  26. logger = logging.getLogger("buildhistory")
  27. loggerhandler = logging.StreamHandler()
  28. loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
  29. logger.addHandler(loggerhandler)
  30. logger.setLevel(logging.INFO)
  31. return logger
  32. logger = logger_create()
  33. def main():
  34. parser = optparse.OptionParser(
  35. description = "Collects the recorded SRCREV values from buildhistory and reports on them.",
  36. usage = """
  37. %prog [options]""")
  38. parser.add_option("-a", "--report-all",
  39. help = "Report all SRCREV values, not just ones where AUTOREV has been used",
  40. action="store_true", dest="reportall")
  41. parser.add_option("-f", "--forcevariable",
  42. help = "Use forcevariable override for all output lines",
  43. action="store_true", dest="forcevariable")
  44. parser.add_option("-p", "--buildhistory-dir",
  45. help = "Specify path to buildhistory directory (defaults to buildhistory/ under cwd)",
  46. action="store", dest="buildhistory_dir", default='buildhistory/')
  47. options, args = parser.parse_args(sys.argv)
  48. if len(args) > 1:
  49. sys.stderr.write('Invalid argument(s) specified: %s\n\n' % ' '.join(args[1:]))
  50. parser.print_help()
  51. sys.exit(1)
  52. if not os.path.exists(options.buildhistory_dir):
  53. sys.stderr.write('Buildhistory directory "%s" does not exist\n\n' % options.buildhistory_dir)
  54. parser.print_help()
  55. sys.exit(1)
  56. if options.forcevariable:
  57. forcevariable = '_forcevariable'
  58. else:
  59. forcevariable = ''
  60. all_srcrevs = collections.defaultdict(list)
  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))
  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):
  90. all_srcrevs[curdir].append((pn, None, srcrev))
  91. for name, value in srcrevs.items():
  92. orig = orig_srcrevs.get(name, orig_srcrev)
  93. if options.reportall or value != orig:
  94. all_srcrevs[curdir].append((pn, name, value))
  95. for curdir, srcrevs in sorted(all_srcrevs.items()):
  96. if srcrevs:
  97. print('# %s' % curdir)
  98. for pn, name, srcrev in srcrevs:
  99. if name:
  100. print('SRCREV_%s_pn-%s%s = "%s"' % (name, pn, forcevariable, srcrev))
  101. else:
  102. print('SRCREV_pn-%s%s = "%s"' % (pn, forcevariable, srcrev))
  103. if __name__ == "__main__":
  104. main()