Skip to content

Commit 7347d19

Browse files
Merge pull request #93 from FHPythonUtils/refactor
Refactor
2 parents 79f237e + 41d9cb0 commit 7347d19

32 files changed

+193
-216
lines changed

.github/workflows/test-lint.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
run: poetry install
4040

4141
- name: Run pytest
42-
run: poetry run pytest
42+
run: poetry run pytest ./tests/platform_independent
4343

4444
- name: Run ruff
4545
run: poetry run ruff check --output-format=github

documentation/reference/licensecheck/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def cli() -> None: ...
2424

2525
## main
2626

27-
[Show source in cli.py:89](../../../licensecheck/cli.py#L89)
27+
[Show source in cli.py:101](../../../licensecheck/cli.py#L101)
2828

2929
Test entry point.
3030

documentation/reference/licensecheck/get_deps.md

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,88 +5,46 @@
55
> Auto-generated documentation for [licensecheck.get_deps](../../../licensecheck/get_deps.py) module.
66
77
- [Get Deps](#get-deps)
8-
- [getDepsWithLicenses](#getdepswithlicenses)
9-
- [getReqs](#getreqs)
8+
- [check](#check)
9+
- [resolve_requirements](#resolve_requirements)
1010

11-
## getDepsWithLicenses
11+
## check
1212

13-
[Show source in get_deps.py:75](../../../licensecheck/get_deps.py#L75)
14-
15-
Get a set of dependencies with licenses and determine license compatibility.
16-
17-
#### Arguments
18-
19-
----
20-
- `using` *str* - use requirements or poetry
21-
- `myLice` *License* - user license
22-
- `ignorePackages` *list[ucstr]* - a list of packages to ignore (compat=True)
23-
- `failPackages` *list[ucstr]* - a list of packages to fail (compat=False)
24-
- `ignoreLicenses` *list[ucstr]* - a list of licenses to ignore (skipped, compat may still be
25-
False)
26-
- `failLicenses` *list[ucstr]* - a list of licenses to fail (compat=False)
27-
- `onlyLicenses` *list[ucstr]* - a list of allowed licenses (any other license will fail)
28-
- `skipDependencies` *list[ucstr]* - a list of dependencies to skip (compat=False)
29-
30-
#### Returns
31-
32-
-------
33-
- `tuple[License,` *set[PackageInfo]]* - tuple of
34-
my package license
35-
set of updated dependencies with licenseCompat set
13+
[Show source in get_deps.py:41](../../../licensecheck/get_deps.py#L41)
3614

3715
#### Signature
3816

3917
```python
40-
def getDepsWithLicenses(
41-
using: str,
42-
myLice: License,
43-
ignorePackages: list[ucstr],
44-
failPackages: list[ucstr],
45-
ignoreLicenses: list[ucstr],
46-
failLicenses: list[ucstr],
47-
onlyLicenses: list[ucstr],
48-
skipDependencies: list[ucstr],
49-
) -> set[PackageInfo]: ...
18+
def check(
19+
requirements_paths: list[str],
20+
groups: list[str],
21+
this_license: License,
22+
ignore_packages: list[ucstr] | None = None,
23+
fail_packages: list[ucstr] | None = None,
24+
ignore_licenses: list[ucstr] | None = None,
25+
fail_licenses: list[ucstr] | None = None,
26+
only_licenses: list[ucstr] | None = None,
27+
skip_dependencies: list[ucstr] | None = None,
28+
) -> tuple[bool, set[PackageInfo]]: ...
5029
```
5130

5231
#### See also
5332

5433
- [License](./types.md#license)
5534
- [PackageInfo](./types.md#packageinfo)
56-
- [ucstr](./types.md#ucstr)
57-
58-
59-
60-
## getReqs
61-
62-
[Show source in get_deps.py:17](../../../licensecheck/get_deps.py#L17)
63-
64-
Get requirements for the end user project/ lib.
65-
66-
```python
67-
>>> getReqs("poetry")
68-
>>> getReqs("poetry:dev")
69-
>>> getReqs("requirements")
70-
>>> getReqs("requirements:requirements.txt;requirements-dev.txt")
71-
>>> getReqs("PEP631")
72-
>>> getReqs("PEP631:tests")
73-
```
7435

75-
#### Arguments
7636

77-
----
78-
- `using` *str* - use requirements, poetry or PEP631.
79-
- `skipDependencies` *list[str]* - list of dependencies to skip.
8037

81-
#### Returns
38+
## resolve_requirements
8239

83-
-------
84-
- `set[str]` - set of requirement packages
40+
[Show source in get_deps.py:15](../../../licensecheck/get_deps.py#L15)
8541

8642
#### Signature
8743

8844
```python
89-
def getReqs(using: str, skipDependencies: list[ucstr]) -> set[ucstr]: ...
45+
def resolve_requirements(
46+
requirements_paths: list[str], groups: list[str], skip_dependencies: list[ucstr]
47+
) -> set[ucstr]: ...
9048
```
9149

9250
#### See also

documentation/reference/licensecheck/resolvers/native.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
> Auto-generated documentation for [licensecheck.resolvers.native](../../../../licensecheck/resolvers/native.py) module.
66
77
- [Native](#native)
8+
- [do_get_reqs](#do_get_reqs)
89
- [get_reqs](#get_reqs)
910

10-
## get_reqs
11+
## do_get_reqs
1112

12-
[Show source in native.py:16](../../../../licensecheck/resolvers/native.py#L16)
13+
[Show source in native.py:46](../../../../licensecheck/resolvers/native.py#L46)
1314

1415
Underlying machineary to get requirements.
1516

@@ -30,7 +31,7 @@ Underlying machineary to get requirements.
3031
#### Signature
3132

3233
```python
33-
def get_reqs(
34+
def do_get_reqs(
3435
using: str,
3536
skipDependencies: list[ucstr],
3637
extras: list[str],
@@ -41,4 +42,25 @@ def get_reqs(
4142

4243
#### See also
4344

45+
- [ucstr](../types.md#ucstr)
46+
47+
48+
49+
## get_reqs
50+
51+
[Show source in native.py:17](../../../../licensecheck/resolvers/native.py#L17)
52+
53+
#### Signature
54+
55+
```python
56+
def get_reqs(
57+
skipDependencies: list[ucstr],
58+
extras: list[str],
59+
requirementsPaths: list[Path],
60+
pyproject: dict[str, Any],
61+
) -> set[ucstr]: ...
62+
```
63+
64+
#### See also
65+
4466
- [ucstr](../types.md#ucstr)

documentation/reference/licensecheck/resolvers/uv.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515

1616
```python
1717
def get_reqs(
18-
using: str,
19-
skipDependencies: list[ucstr],
20-
extras: list[str],
21-
requirementsPaths: list[str],
18+
skipDependencies: list[ucstr], extras: list[str], requirementsPaths: list[str]
2219
) -> set[ucstr]: ...
2320
```
2421

licensecheck/cli.py

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from dataclasses import fields
99
from pathlib import Path
1010
from sys import exit as sysexit
11-
from sys import stdout
11+
from sys import stdin, stdout
1212

1313
from fhconfparser import FHConfParser, SimpleConf
1414

@@ -30,17 +30,23 @@ def cli() -> None: # pragma: no cover
3030
"-f",
3131
help=f"Output format. one of: {', '.join(list(formatter.formatMap))}. default=simple",
3232
)
33+
parser.add_argument(
34+
"--requirements-paths",
35+
"-r",
36+
help="Filenames to read from (omit for stdin)",
37+
nargs="+",
38+
)
39+
parser.add_argument(
40+
"--groups",
41+
"-g",
42+
help="Select groups/extras from supported files",
43+
nargs="+",
44+
)
3345
parser.add_argument(
3446
"--file",
3547
"-o",
3648
help="Filename to write to (omit for stdout)",
3749
)
38-
parser.add_argument(
39-
"--using",
40-
"-u",
41-
help="Environment to use e.g. requirements.txt. one of: "
42-
f"{', '.join(get_deps.USINGS)}. default=poetry",
43-
)
4450
parser.add_argument(
4551
"--ignore-packages",
4652
help="a list of packages to ignore (compat=True)",
@@ -83,7 +89,13 @@ def cli() -> None: # pragma: no cover
8389
action="store_true",
8490
)
8591
args = vars(parser.parse_args())
86-
sysexit(main(args))
92+
stdin_path = Path("__stdin__")
93+
if stdin:
94+
stdin_path.write_text("\n".join(stdin.readlines()), "utf-8")
95+
ec = main(args)
96+
stdin_path.unlink(missing_ok=True)
97+
98+
sysexit(ec)
8799

88100

89101
def main(args: dict) -> int:
@@ -110,30 +122,33 @@ def main(args: dict) -> int:
110122
simpleConf = SimpleConf(configparser, "licensecheck", args)
111123

112124
# File
113-
textIO = (
125+
requirements_paths = simpleConf.get("requirements_paths") or ["__stdin__"]
126+
output_file = (
114127
stdout
115128
if simpleConf.get("file") is None
116129
else Path(simpleConf.get("file")).open("w", encoding="utf-8")
117130
)
118131

119132
# Get my license
120-
myLiceTxt = args["license"] if args.get("license") else packageinfo.getMyPackageLicense()
121-
myLice = license_matrix.licenseType(myLiceTxt)[0]
122-
123-
# Get list of licenses
124-
depsWithLicenses = get_deps.getDepsWithLicenses(
125-
simpleConf.get("using", "poetry"),
126-
myLice,
127-
list(map(types.ucstr, simpleConf.get("ignore_packages", []))),
128-
list(map(types.ucstr, simpleConf.get("fail_packages", []))),
129-
list(map(types.ucstr, simpleConf.get("ignore_licenses", []))),
130-
list(map(types.ucstr, simpleConf.get("fail_licenses", []))),
131-
list(map(types.ucstr, simpleConf.get("only_licenses", []))),
132-
list(map(types.ucstr, simpleConf.get("skip_dependencies", []))),
133+
this_license_text = (
134+
args["license"] if args.get("license") else packageinfo.getMyPackageLicense()
135+
)
136+
this_license = license_matrix.licenseType(this_license_text)[0]
137+
138+
def getFromConfig(key: str) -> list[types.ucstr]:
139+
return list(map(types.ucstr, simpleConf.get(key, [])))
140+
141+
incompatible, depsWithLicenses = get_deps.check(
142+
requirements_paths=requirements_paths,
143+
groups=simpleConf.get("groups", []),
144+
this_license=this_license,
145+
ignore_packages=getFromConfig("ignore_packages"),
146+
fail_packages=getFromConfig("fail_packages"),
147+
ignore_licenses=getFromConfig("ignore_licenses"),
148+
fail_licenses=getFromConfig("fail_licenses"),
149+
only_licenses=getFromConfig("only_licenses"),
150+
skip_dependencies=getFromConfig("skip_dependencies"),
133151
)
134-
135-
# Are any licenses incompatible?
136-
incompatible = any(not lice.licenseCompat for lice in depsWithLicenses)
137152

138153
# Format the results
139154
hide_output_parameters = [types.ucstr(x) for x in simpleConf.get("hide_output_parameters", [])]
@@ -147,11 +162,11 @@ def main(args: dict) -> int:
147162
if simpleConf.get("format", "simple") in formatter.formatMap:
148163
print(
149164
formatter.formatMap[simpleConf.get("format", "simple")](
150-
myLice,
165+
this_license,
151166
sorted(depsWithLicenses),
152167
hide_output_parameters,
153168
),
154-
file=textIO,
169+
file=output_file,
155170
)
156171
else:
157172
exitCode = 2
@@ -162,5 +177,5 @@ def main(args: dict) -> int:
162177

163178
# Cleanup + exit
164179
if simpleConf.get("file") is not None:
165-
textIO.close()
180+
output_file.close()
166181
return exitCode

0 commit comments

Comments
 (0)