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 | |
Marc MERLIN | 4158529 | 2013-10-09 15:00:49 -0700 | [diff] [blame^] | 15 | def GetPackagesLicensesFromHtml(html_file): |
| 16 | """Get the list of packages and licenses in a ChromeOS license file. |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 17 | |
| 18 | Args: |
| 19 | html_file: which html license file to scan for packages. |
| 20 | |
| 21 | Returns: |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 22 | tuple of dictionary of packages and version numbers and set of licenses. |
| 23 | |
| 24 | Raises: |
| 25 | AssertionError: if regex failed. |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 26 | """ |
| 27 | |
| 28 | packages = {} |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 29 | licenses = set() |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 30 | |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 31 | pkg_rgx = re.compile(r'<span class="title">(.+)-(.+)</span>') |
Marc MERLIN | 4158529 | 2013-10-09 15:00:49 -0700 | [diff] [blame^] | 32 | license_rgx1 = re.compile(r'Scanned (Source License .+):', re.IGNORECASE) |
| 33 | license_rgx2 = re.compile(r'<pre>(Custom License .+):', re.IGNORECASE) |
| 34 | # Gentoo Package Stock License BZIP2: |
| 35 | # <a ... class="title">Gentoo Package Stock License public-domain</a> |
| 36 | license_rgx3 = re.compile(r'Gentoo Package (Stock License [^<:]+)', |
| 37 | re.IGNORECASE) |
| 38 | license_rgx4 = re.compile(r'class="title">(Custom License .+)</a>', |
| 39 | re.IGNORECASE) |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 40 | with open(html_file, 'r') as f: |
| 41 | for line in f: |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 42 | # Grep and turn |
| 43 | # <span class="title">ath6k-34</span> |
| 44 | # into |
| 45 | # ath6k 34 |
| 46 | match = pkg_rgx.search(line) |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 47 | if match: |
| 48 | packages[match.group(1)] = match.group(2) |
| 49 | |
Marc MERLIN | 4158529 | 2013-10-09 15:00:49 -0700 | [diff] [blame^] | 50 | match = license_rgx1.search(line) |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 51 | if match: |
Marc MERLIN | 4158529 | 2013-10-09 15:00:49 -0700 | [diff] [blame^] | 52 | # Turn Source license simplejson-2.5.0/LICENSE.txt |
| 53 | # into Source license simplejson/LICENSE.txt |
| 54 | # (we don't want to create diffs based on version numbers) |
| 55 | lic = re.sub(r'(.+)-([^/]+)/(.+)', r'\1/\3', match.group(1)) |
| 56 | # Old files had this lowercased. |
| 57 | lic = re.sub(r'Source license', r'Source License', lic) |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 58 | licenses.add(lic) |
Marc MERLIN | 4158529 | 2013-10-09 15:00:49 -0700 | [diff] [blame^] | 59 | |
| 60 | for rgx in (license_rgx2, license_rgx3, license_rgx4): |
| 61 | match = rgx.search(line) |
| 62 | if match: |
| 63 | licenses.add(match.group(1)) |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 64 | |
| 65 | return (packages, licenses) |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 66 | |
| 67 | |
| 68 | def ComparePkgLists(pkg_list1, pkg_list2): |
| 69 | """Compare the package list in 2 dictionaries and output the differences. |
| 70 | |
| 71 | Args: |
Marc MERLIN | 4158529 | 2013-10-09 15:00:49 -0700 | [diff] [blame^] | 72 | pkg_list1: dict from GetPackagesLicensesFromHtml. |
| 73 | pkg_list2: dict from GetPackagesLicensesFromHtml. |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 74 | |
| 75 | Returns: |
| 76 | N/A (outputs result on stdout). |
| 77 | """ |
| 78 | |
| 79 | for removed_package in sorted(set(pkg_list1) - set(pkg_list2)): |
| 80 | print 'Package removed: %s-%s' % ( |
| 81 | removed_package, pkg_list1[removed_package]) |
| 82 | |
| 83 | print |
| 84 | for added_package in sorted(set(pkg_list2) - set(pkg_list1)): |
| 85 | print 'Package added: %s-%s' % ( |
| 86 | added_package, pkg_list2[added_package]) |
| 87 | |
| 88 | print |
| 89 | for changed_package in sorted(set(pkg_list1) & set(pkg_list2)): |
| 90 | ver1 = pkg_list1[changed_package] |
| 91 | ver2 = pkg_list2[changed_package] |
| 92 | if ver1 != ver2: |
| 93 | print 'Package updated: %s from %s to %s' % (changed_package, ver1, ver2) |
| 94 | |
| 95 | |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 96 | def CompareLicenseSets(set1, set2): |
| 97 | """Compare the license list in 2 sets and output the differences. |
| 98 | |
| 99 | Args: |
Marc MERLIN | 4158529 | 2013-10-09 15:00:49 -0700 | [diff] [blame^] | 100 | set1: set from GetPackagesLicensesFromHtml. |
| 101 | set2: set from GetPackagesLicensesFromHtml. |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 102 | |
| 103 | Returns: |
| 104 | N/A (outputs result on stdout). |
| 105 | """ |
| 106 | |
| 107 | for removed_license in sorted(set1 - set2): |
| 108 | print 'License removed: %s' % (removed_license) |
| 109 | |
| 110 | print |
| 111 | for added_license in sorted(set2 - set1): |
| 112 | print 'License added: %s' % (added_license) |
| 113 | |
| 114 | |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 115 | def main(args): |
| 116 | parser = commandline.ArgumentParser(usage=__doc__) |
| 117 | parser.add_argument('html1', metavar='license1.html', type='path', |
| 118 | help='old html file') |
| 119 | parser.add_argument('html2', metavar='license2.html', type='path', |
| 120 | help='new html file') |
| 121 | opts = parser.parse_args(args) |
| 122 | |
Marc MERLIN | 4158529 | 2013-10-09 15:00:49 -0700 | [diff] [blame^] | 123 | pkg_list1 = GetPackagesLicensesFromHtml(opts.html1) |
| 124 | pkg_list2 = GetPackagesLicensesFromHtml(opts.html2) |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 125 | ComparePkgLists(pkg_list1[0], pkg_list2[0]) |
| 126 | print |
| 127 | CompareLicenseSets(pkg_list1[1], pkg_list2[1]) |