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