Coverage for pipxl/license.py: 100%

1# SPDX-FileCopyrightText: 2022-present Jeroen van Zundert <mail@jeroenvanzundert.nl>

2#

3# SPDX-License-Identifier: MIT

4

5 from __future__ import annotations

6

7 from pathlib import Path

8

9 from pipxl.resolver import pip_resolve

10

11

12 def license(files_in: list[Path] | None = None, package_spec: list[str] | None = None) -> dict[str | None, list[str]]:

13 reqs, _ = pip_resolve(files_in, package_spec)

14

15 by_license = {}

16 for r in reqs:

17 lic = _parse_license(r.license)

18 if lic not in by_license:

19 by_license[lic] = [r.name]

20 else:

21 by_license[lic] += [r.name]

22

23 # return sorted by license

24 by_license = dict(sorted(by_license.items(), key=lambda item: item[0] if item[0] is not None else "*"))

25

26 return by_license

27

28

29 def _parse_license(lic: str | None) -> str | None:

30 return lic if (lic is not None and len(lic) < 50) else None