|
3 | 3 | import argparse |
4 | 4 | import json |
5 | 5 | import subprocess |
6 | | -import tempfile |
7 | | -from inspect import cleandoc |
8 | | -from json import loads |
9 | 6 | from pathlib import Path |
10 | 7 |
|
11 | 8 | import nox |
12 | 9 | from nox import Session |
13 | 10 |
|
| 11 | +from exasol.toolbox.util.dependencies.licenses import ( |
| 12 | + licenses, |
| 13 | + packages_to_markdown, |
| 14 | +) |
14 | 15 | from exasol.toolbox.util.dependencies.poetry_dependencies import ( |
15 | | - Package, |
16 | 16 | PoetryDependencies, |
17 | | - PoetryDependency, |
18 | 17 | PoetryToml, |
19 | 18 | ) |
20 | 19 |
|
21 | 20 |
|
22 | | -class PackageLicense(Package): |
23 | | - package_link: str |
24 | | - license: str |
25 | | - license_link: str |
26 | | - |
27 | | - |
28 | | -def _normalize(_license: str) -> str: |
29 | | - def is_multi_license(l): |
30 | | - return ";" in l |
31 | | - |
32 | | - def select_most_restrictive(licenses: list) -> str: |
33 | | - _max = 0 |
34 | | - lic = "Unknown" |
35 | | - _mapping = { |
36 | | - "Unknown": -1, |
37 | | - "Unlicensed": 0, |
38 | | - "BSD": 1, |
39 | | - "MIT": 2, |
40 | | - "MPLv2": 3, |
41 | | - "LGPLv2": 4, |
42 | | - "GPLv2": 5, |
43 | | - "GPLv3": 6, |
44 | | - } |
45 | | - for l in licenses: |
46 | | - if l in _mapping: |
47 | | - if _mapping[l] > _mapping[lic]: |
48 | | - lic = l |
49 | | - else: |
50 | | - return "<br>".join(licenses) |
51 | | - return lic |
52 | | - |
53 | | - mapping = { |
54 | | - "BSD License": "BSD", |
55 | | - "MIT License": "MIT", |
56 | | - "The Unlicensed (Unlicensed)": "Unlicensed", |
57 | | - "Mozilla Public License 2.0 (MPL 2.0)": "MPLv2", |
58 | | - "GNU General Public License (GPL)": "GPL", |
59 | | - "GNU Lesser General Public License v2 (LGPLv2)": "LGPLv2", |
60 | | - "GNU General Public License v2 (GPLv2)": "GPLv2", |
61 | | - "GNU General Public License v2 or later (GPLv2+)": "GPLv2+", |
62 | | - "GNU General Public License v3 (GPLv3)": "GPLv3", |
63 | | - "Apache Software License": "Apache", |
64 | | - } |
65 | | - |
66 | | - if is_multi_license(_license): |
67 | | - items = [] |
68 | | - for item in _license.split(";"): |
69 | | - item = str(item).strip() |
70 | | - if item in mapping: |
71 | | - items.append(mapping[item]) |
72 | | - else: |
73 | | - items.append(item) |
74 | | - return select_most_restrictive(items) |
75 | | - |
76 | | - if _license not in mapping: |
77 | | - return _license |
78 | | - |
79 | | - return mapping[_license] |
80 | | - |
81 | | - |
82 | | -def _packages_from_json(json: str) -> list[PackageLicense]: |
83 | | - packages = loads(json) |
84 | | - packages_list = [] |
85 | | - mapping = { |
86 | | - "GPLv1": "https://www.gnu.org/licenses/old-licenses/gpl-1.0.html", |
87 | | - "GPLv2": "https://www.gnu.org/licenses/old-licenses/gpl-2.0.html", |
88 | | - "LGPLv2": "https://www.gnu.org/licenses/old-licenses/lgpl-2.0.html", |
89 | | - "GPLv3": "https://www.gnu.org/licenses/gpl-3.0.html", |
90 | | - "LGPLv3": "https://www.gnu.org/licenses/lgpl-3.0.html", |
91 | | - "Apache": "https://www.apache.org/licenses/LICENSE-2.0", |
92 | | - "MIT": "https://mit-license.org/", |
93 | | - "BSD": "https://opensource.org/license/bsd-3-clause", |
94 | | - } |
95 | | - for package in packages: |
96 | | - package_license = _normalize(package["License"]) |
97 | | - packages_list.append( |
98 | | - PackageLicense( |
99 | | - name=package["Name"], |
100 | | - package_link="" if package["URL"] == "UNKNOWN" else package["URL"], |
101 | | - version=package["Version"], |
102 | | - license=package_license, |
103 | | - license_link=( |
104 | | - "" if package_license not in mapping else mapping[package_license] |
105 | | - ), |
106 | | - ) |
107 | | - ) |
108 | | - return packages_list |
109 | | - |
110 | | - |
111 | | -def _licenses() -> list[PackageLicense]: |
112 | | - with tempfile.NamedTemporaryFile() as file: |
113 | | - subprocess.run( |
114 | | - [ |
115 | | - "poetry", |
116 | | - "run", |
117 | | - "pip-licenses", |
118 | | - "--format=json", |
119 | | - "--output-file=" + file.name, |
120 | | - "--with-system", |
121 | | - "--with-urls", |
122 | | - ], |
123 | | - capture_output=True, |
124 | | - ) |
125 | | - result = _packages_from_json(file.read().decode()) |
126 | | - return result |
127 | | - |
128 | | - |
129 | | -def _packages_to_markdown( |
130 | | - dependencies: dict[str, list], packages: list[PackageLicense] |
131 | | -) -> str: |
132 | | - def heading(): |
133 | | - text = "# Dependencies\n" |
134 | | - return text |
135 | | - |
136 | | - def dependency( |
137 | | - group: str, |
138 | | - group_packages: list[PoetryDependency], |
139 | | - packages: list[PackageLicense], |
140 | | - ) -> str: |
141 | | - def _header(_group: str): |
142 | | - _group = "".join([word.capitalize() for word in _group.strip().split()]) |
143 | | - text = f"## {_group} Dependencies\n" |
144 | | - text += "|Package|version|Licence|\n" |
145 | | - text += "|---|---|---|\n" |
146 | | - return text |
147 | | - |
148 | | - def _rows( |
149 | | - _group_packages: list[PoetryDependency], _packages: list[PackageLicense] |
150 | | - ) -> str: |
151 | | - text = "" |
152 | | - for package in _group_packages: |
153 | | - consistent = filter( |
154 | | - lambda elem: elem.normalized_name == package.normalized_name, |
155 | | - _packages, |
156 | | - ) |
157 | | - for content in consistent: |
158 | | - if content.package_link: |
159 | | - text += f"|[{content.name}]({content.package_link})" |
160 | | - else: |
161 | | - text += f"|{content.name}" |
162 | | - text += f"|{content.version}" |
163 | | - if content.license_link: |
164 | | - text += f"|[{content.license}]({content.license_link})|\n" |
165 | | - else: |
166 | | - text += f"|{content.license}|\n" |
167 | | - text += "\n" |
168 | | - return text |
169 | | - |
170 | | - _template = cleandoc( |
171 | | - """ |
172 | | - {header}{rows} |
173 | | - """ |
174 | | - ) |
175 | | - return _template.format( |
176 | | - header=_header(group), rows=_rows(group_packages, packages) |
177 | | - ) |
178 | | - |
179 | | - template = cleandoc( |
180 | | - """ |
181 | | - {heading}{rows} |
182 | | - """ |
183 | | - ) |
184 | | - |
185 | | - rows = "" |
186 | | - for group in dependencies: |
187 | | - rows += dependency(group, dependencies[group], packages) |
188 | | - return template.format(heading=heading(), rows=rows) |
189 | | - |
190 | | - |
191 | 21 | class Audit: |
192 | 22 | @staticmethod |
193 | 23 | def _filter_json_for_vulnerabilities(audit_json_bytes: bytes) -> dict: |
@@ -262,8 +92,8 @@ def dependency_licenses(session: Session) -> None: |
262 | 92 | dependencies = PoetryDependencies( |
263 | 93 | groups=groups, working_directory=working_directory |
264 | 94 | ).direct_dependencies |
265 | | - package_infos = _licenses() |
266 | | - print(_packages_to_markdown(dependencies=dependencies, packages=package_infos)) |
| 95 | + package_infos = licenses() |
| 96 | + print(packages_to_markdown(dependencies=dependencies, packages=package_infos)) |
267 | 97 |
|
268 | 98 |
|
269 | 99 | @nox.session(name="dependency:audit", python=False) |
|
0 commit comments