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