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