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: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 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: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 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: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 22 | AssertionError: if regex failed. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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) |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 34 | # pylint: disable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | # This regex isn't as tight because it has to match these: |
| 36 | # Gentoo Package Stock License BZIP2: |
| 37 | # <a ... class="title">Gentoo Package Provided Stock License public-domain</a> |
| 38 | # <a ... class="title">Gentoo Package Stock License public-domain</a> |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 39 | # pylint: enable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | license_rgx4 = re.compile(r"(Stock License [^<:]+)", re.IGNORECASE) |
| 41 | license_rgx5 = re.compile( |
| 42 | r'class="title">(Custom License .+)</a>', re.IGNORECASE |
| 43 | ) |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame] | 44 | with open(html_file, "r", encoding="utf-8") as f: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | for line in f: |
| 46 | # Grep and turn |
| 47 | # <span class="title">ath6k-34</span> |
| 48 | # into |
| 49 | # ath6k 34 |
| 50 | match = pkg_rgx.search(line) |
| 51 | if match: |
| 52 | packages[match.group(1)] = match.group(2) |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 53 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | match = license_rgx1.search(line) |
| 55 | if match: |
| 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) |
| 62 | licenses.add(lic) |
Marc MERLIN | 4158529 | 2013-10-09 15:00:49 -0700 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | for rgx in (license_rgx2, license_rgx3, license_rgx4, license_rgx5): |
| 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 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | """Compare the package list in 2 dictionaries and output the differences. |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 74 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [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 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 79 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 80 | N/A (outputs result on stdout). |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | """ |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 82 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 83 | for removed_package in sorted(set(pkg_list1) - set(pkg_list2)): |
| 84 | print( |
| 85 | "Package removed: %s-%s" |
| 86 | % (removed_package, pkg_list1[removed_package]) |
| 87 | ) |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 88 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 89 | print() |
| 90 | for added_package in sorted(set(pkg_list2) - set(pkg_list1)): |
| 91 | print( |
| 92 | "Package added: %s-%s" % (added_package, pkg_list2[added_package]) |
| 93 | ) |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 94 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 95 | print() |
| 96 | for changed_package in sorted(set(pkg_list1) & set(pkg_list2)): |
| 97 | ver1 = pkg_list1[changed_package] |
| 98 | ver2 = pkg_list2[changed_package] |
| 99 | if ver1 != ver2: |
| 100 | print( |
| 101 | "Package updated: %s from %s to %s" |
| 102 | % (changed_package, ver1, ver2) |
| 103 | ) |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 104 | |
| 105 | |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 106 | def CompareLicenseSets(set1, set2): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | """Compare the license list in 2 sets and output the differences. |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 108 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 109 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 110 | set1: set from GetPackagesLicensesFromHtml. |
| 111 | set2: set from GetPackagesLicensesFromHtml. |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 112 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame^] | 114 | N/A (outputs result on stdout). |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 115 | """ |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 116 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 117 | for removed_license in sorted(set1 - set2): |
| 118 | print("License removed: %s" % (removed_license)) |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | print() |
| 121 | for added_license in sorted(set2 - set1): |
| 122 | print("License added: %s" % (added_license)) |
Marc MERLIN | ea95f20 | 2013-10-02 17:07:00 -0700 | [diff] [blame] | 123 | |
| 124 | |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 125 | def main(args): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 126 | parser = commandline.ArgumentParser(description=__doc__) |
| 127 | parser.add_argument( |
| 128 | "html1", metavar="license1.html", type="path", help="old html file" |
| 129 | ) |
| 130 | parser.add_argument( |
| 131 | "html2", metavar="license2.html", type="path", help="new html file" |
| 132 | ) |
| 133 | opts = parser.parse_args(args) |
Marc MERLIN | 0a62194 | 2013-09-30 15:22:38 -0700 | [diff] [blame] | 134 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 135 | pkg_list1 = GetPackagesLicensesFromHtml(opts.html1) |
| 136 | pkg_list2 = GetPackagesLicensesFromHtml(opts.html2) |
| 137 | ComparePkgLists(pkg_list1[0], pkg_list2[0]) |
| 138 | print() |
| 139 | CompareLicenseSets(pkg_list1[1], pkg_list2[1]) |