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