Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | # |
| 7 | |
| 8 | """Compares the packages between 2 images by parsing the license file output.""" |
| 9 | |
| 10 | import re |
| 11 | |
| 12 | from chromite.lib import commandline |
| 13 | |
| 14 | |
| 15 | def GetTreePackages(html_file): |
| 16 | """Get the list of debian packages in an unpacked ProdNG tree. |
| 17 | |
| 18 | Args: |
| 19 | html_file: which html license file to scan for packages. |
| 20 | |
| 21 | Returns: |
| 22 | dictionary of packages and version numbers. |
| 23 | """ |
| 24 | |
| 25 | packages = {} |
| 26 | |
| 27 | # Grep and turn |
| 28 | # <span class="title">ath6k-34</span> |
| 29 | # into |
| 30 | # ath6k 34 |
| 31 | exp = re.compile(r'<span class="title">(.+)-(.+)</span>') |
| 32 | with open(html_file, 'r') as f: |
| 33 | for line in f: |
| 34 | match = exp.search(line) |
| 35 | if match: |
| 36 | packages[match.group(1)] = match.group(2) |
| 37 | |
| 38 | return packages |
| 39 | |
| 40 | |
| 41 | def ComparePkgLists(pkg_list1, pkg_list2): |
| 42 | """Compare the package list in 2 dictionaries and output the differences. |
| 43 | |
| 44 | Args: |
| 45 | pkg_list1: dict from GetTreePackages. |
| 46 | pkg_list2: dict from GetTreePackages. |
| 47 | |
| 48 | Returns: |
| 49 | N/A (outputs result on stdout). |
| 50 | """ |
| 51 | |
| 52 | for removed_package in sorted(set(pkg_list1) - set(pkg_list2)): |
| 53 | print 'Package removed: %s-%s' % ( |
| 54 | removed_package, pkg_list1[removed_package]) |
| 55 | |
| 56 | print |
| 57 | for added_package in sorted(set(pkg_list2) - set(pkg_list1)): |
| 58 | print 'Package added: %s-%s' % ( |
| 59 | added_package, pkg_list2[added_package]) |
| 60 | |
| 61 | print |
| 62 | for changed_package in sorted(set(pkg_list1) & set(pkg_list2)): |
| 63 | ver1 = pkg_list1[changed_package] |
| 64 | ver2 = pkg_list2[changed_package] |
| 65 | if ver1 != ver2: |
| 66 | print 'Package updated: %s from %s to %s' % (changed_package, ver1, ver2) |
| 67 | |
| 68 | |
| 69 | def main(args): |
| 70 | parser = commandline.ArgumentParser(usage=__doc__) |
| 71 | parser.add_argument('html1', metavar='license1.html', type='path', |
| 72 | help='old html file') |
| 73 | parser.add_argument('html2', metavar='license2.html', type='path', |
| 74 | help='new html file') |
| 75 | opts = parser.parse_args(args) |
| 76 | |
| 77 | pkg_list1 = GetTreePackages(opts.html1) |
| 78 | pkg_list2 = GetTreePackages(opts.html2) |
| 79 | ComparePkgLists(pkg_list1, pkg_list2) |