Skip to content

Commit

Permalink
Merge pull request #93 from FHPythonUtils/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
FredHappyface authored Dec 1, 2024
2 parents 79f237e + 41d9cb0 commit 7347d19
Show file tree
Hide file tree
Showing 32 changed files with 193 additions and 216 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
run: poetry install

- name: Run pytest
run: poetry run pytest
run: poetry run pytest ./tests/platform_independent

- name: Run ruff
run: poetry run ruff check --output-format=github
Expand Down
2 changes: 1 addition & 1 deletion documentation/reference/licensecheck/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def cli() -> None: ...

## main

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

Test entry point.

Expand Down
82 changes: 20 additions & 62 deletions documentation/reference/licensecheck/get_deps.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,88 +5,46 @@
> Auto-generated documentation for [licensecheck.get_deps](../../../licensecheck/get_deps.py) module.
- [Get Deps](#get-deps)
- [getDepsWithLicenses](#getdepswithlicenses)
- [getReqs](#getreqs)
- [check](#check)
- [resolve_requirements](#resolve_requirements)

## getDepsWithLicenses
## check

[Show source in get_deps.py:75](../../../licensecheck/get_deps.py#L75)

Get a set of dependencies with licenses and determine license compatibility.

#### Arguments

----
- `using` *str* - use requirements or poetry
- `myLice` *License* - user license
- `ignorePackages` *list[ucstr]* - a list of packages to ignore (compat=True)
- `failPackages` *list[ucstr]* - a list of packages to fail (compat=False)
- `ignoreLicenses` *list[ucstr]* - a list of licenses to ignore (skipped, compat may still be
False)
- `failLicenses` *list[ucstr]* - a list of licenses to fail (compat=False)
- `onlyLicenses` *list[ucstr]* - a list of allowed licenses (any other license will fail)
- `skipDependencies` *list[ucstr]* - a list of dependencies to skip (compat=False)

#### Returns

-------
- `tuple[License,` *set[PackageInfo]]* - tuple of
my package license
set of updated dependencies with licenseCompat set
[Show source in get_deps.py:41](../../../licensecheck/get_deps.py#L41)

#### Signature

```python
def getDepsWithLicenses(
using: str,
myLice: License,
ignorePackages: list[ucstr],
failPackages: list[ucstr],
ignoreLicenses: list[ucstr],
failLicenses: list[ucstr],
onlyLicenses: list[ucstr],
skipDependencies: list[ucstr],
) -> set[PackageInfo]: ...
def check(
requirements_paths: list[str],
groups: list[str],
this_license: License,
ignore_packages: list[ucstr] | None = None,
fail_packages: list[ucstr] | None = None,
ignore_licenses: list[ucstr] | None = None,
fail_licenses: list[ucstr] | None = None,
only_licenses: list[ucstr] | None = None,
skip_dependencies: list[ucstr] | None = None,
) -> tuple[bool, set[PackageInfo]]: ...
```

#### See also

- [License](./types.md#license)
- [PackageInfo](./types.md#packageinfo)
- [ucstr](./types.md#ucstr)



## getReqs

[Show source in get_deps.py:17](../../../licensecheck/get_deps.py#L17)

Get requirements for the end user project/ lib.

```python
>>> getReqs("poetry")
>>> getReqs("poetry:dev")
>>> getReqs("requirements")
>>> getReqs("requirements:requirements.txt;requirements-dev.txt")
>>> getReqs("PEP631")
>>> getReqs("PEP631:tests")
```

#### Arguments

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

#### Returns
## resolve_requirements

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

#### Signature

```python
def getReqs(using: str, skipDependencies: list[ucstr]) -> set[ucstr]: ...
def resolve_requirements(
requirements_paths: list[str], groups: list[str], skip_dependencies: list[ucstr]
) -> set[ucstr]: ...
```

#### See also
Expand Down
28 changes: 25 additions & 3 deletions documentation/reference/licensecheck/resolvers/native.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
> Auto-generated documentation for [licensecheck.resolvers.native](../../../../licensecheck/resolvers/native.py) module.
- [Native](#native)
- [do_get_reqs](#do_get_reqs)
- [get_reqs](#get_reqs)

## get_reqs
## do_get_reqs

[Show source in native.py:16](../../../../licensecheck/resolvers/native.py#L16)
[Show source in native.py:46](../../../../licensecheck/resolvers/native.py#L46)

Underlying machineary to get requirements.

Expand All @@ -30,7 +31,7 @@ Underlying machineary to get requirements.
#### Signature

```python
def get_reqs(
def do_get_reqs(
using: str,
skipDependencies: list[ucstr],
extras: list[str],
Expand All @@ -41,4 +42,25 @@ def get_reqs(

#### See also

- [ucstr](../types.md#ucstr)



## get_reqs

[Show source in native.py:17](../../../../licensecheck/resolvers/native.py#L17)

#### Signature

```python
def get_reqs(
skipDependencies: list[ucstr],
extras: list[str],
requirementsPaths: list[Path],
pyproject: dict[str, Any],
) -> set[ucstr]: ...
```

#### See also

- [ucstr](../types.md#ucstr)
5 changes: 1 addition & 4 deletions documentation/reference/licensecheck/resolvers/uv.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

```python
def get_reqs(
using: str,
skipDependencies: list[ucstr],
extras: list[str],
requirementsPaths: list[str],
skipDependencies: list[ucstr], extras: list[str], requirementsPaths: list[str]
) -> set[ucstr]: ...
```

Expand Down
71 changes: 43 additions & 28 deletions licensecheck/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from dataclasses import fields
from pathlib import Path
from sys import exit as sysexit
from sys import stdout
from sys import stdin, stdout

from fhconfparser import FHConfParser, SimpleConf

Expand All @@ -30,17 +30,23 @@ def cli() -> None: # pragma: no cover
"-f",
help=f"Output format. one of: {', '.join(list(formatter.formatMap))}. default=simple",
)
parser.add_argument(
"--requirements-paths",
"-r",
help="Filenames to read from (omit for stdin)",
nargs="+",
)
parser.add_argument(
"--groups",
"-g",
help="Select groups/extras from supported files",
nargs="+",
)
parser.add_argument(
"--file",
"-o",
help="Filename to write to (omit for stdout)",
)
parser.add_argument(
"--using",
"-u",
help="Environment to use e.g. requirements.txt. one of: "
f"{', '.join(get_deps.USINGS)}. default=poetry",
)
parser.add_argument(
"--ignore-packages",
help="a list of packages to ignore (compat=True)",
Expand Down Expand Up @@ -83,7 +89,13 @@ def cli() -> None: # pragma: no cover
action="store_true",
)
args = vars(parser.parse_args())
sysexit(main(args))
stdin_path = Path("__stdin__")
if stdin:
stdin_path.write_text("\n".join(stdin.readlines()), "utf-8")
ec = main(args)
stdin_path.unlink(missing_ok=True)

sysexit(ec)


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

# File
textIO = (
requirements_paths = simpleConf.get("requirements_paths") or ["__stdin__"]
output_file = (
stdout
if simpleConf.get("file") is None
else Path(simpleConf.get("file")).open("w", encoding="utf-8")

Check failure on line 129 in licensecheck/cli.py

View workflow job for this annotation

GitHub Actions / Python Test and Lint (3.8)

Ruff (SIM115)

licensecheck/cli.py:129:8: SIM115 Use context handler for opening files

Check failure on line 129 in licensecheck/cli.py

View workflow job for this annotation

GitHub Actions / Python Test and Lint (3.9)

Ruff (SIM115)

licensecheck/cli.py:129:8: SIM115 Use context handler for opening files

Check failure on line 129 in licensecheck/cli.py

View workflow job for this annotation

GitHub Actions / Python Test and Lint (3.10)

Ruff (SIM115)

licensecheck/cli.py:129:8: SIM115 Use context handler for opening files

Check failure on line 129 in licensecheck/cli.py

View workflow job for this annotation

GitHub Actions / Python Test and Lint (3.11)

Ruff (SIM115)

licensecheck/cli.py:129:8: SIM115 Use context handler for opening files

Check failure on line 129 in licensecheck/cli.py

View workflow job for this annotation

GitHub Actions / Python Test and Lint (3.12)

Ruff (SIM115)

licensecheck/cli.py:129:8: SIM115 Use context handler for opening files
)

# Get my license
myLiceTxt = args["license"] if args.get("license") else packageinfo.getMyPackageLicense()
myLice = license_matrix.licenseType(myLiceTxt)[0]

# Get list of licenses
depsWithLicenses = get_deps.getDepsWithLicenses(
simpleConf.get("using", "poetry"),
myLice,
list(map(types.ucstr, simpleConf.get("ignore_packages", []))),
list(map(types.ucstr, simpleConf.get("fail_packages", []))),
list(map(types.ucstr, simpleConf.get("ignore_licenses", []))),
list(map(types.ucstr, simpleConf.get("fail_licenses", []))),
list(map(types.ucstr, simpleConf.get("only_licenses", []))),
list(map(types.ucstr, simpleConf.get("skip_dependencies", []))),
this_license_text = (
args["license"] if args.get("license") else packageinfo.getMyPackageLicense()
)
this_license = license_matrix.licenseType(this_license_text)[0]

def getFromConfig(key: str) -> list[types.ucstr]:
return list(map(types.ucstr, simpleConf.get(key, [])))

incompatible, depsWithLicenses = get_deps.check(
requirements_paths=requirements_paths,
groups=simpleConf.get("groups", []),
this_license=this_license,
ignore_packages=getFromConfig("ignore_packages"),
fail_packages=getFromConfig("fail_packages"),
ignore_licenses=getFromConfig("ignore_licenses"),
fail_licenses=getFromConfig("fail_licenses"),
only_licenses=getFromConfig("only_licenses"),
skip_dependencies=getFromConfig("skip_dependencies"),
)

# Are any licenses incompatible?
incompatible = any(not lice.licenseCompat for lice in depsWithLicenses)

# Format the results
hide_output_parameters = [types.ucstr(x) for x in simpleConf.get("hide_output_parameters", [])]
Expand All @@ -147,11 +162,11 @@ def main(args: dict) -> int:
if simpleConf.get("format", "simple") in formatter.formatMap:
print(
formatter.formatMap[simpleConf.get("format", "simple")](
myLice,
this_license,
sorted(depsWithLicenses),
hide_output_parameters,
),
file=textIO,
file=output_file,
)
else:
exitCode = 2
Expand All @@ -162,5 +177,5 @@ def main(args: dict) -> int:

# Cleanup + exit
if simpleConf.get("file") is not None:
textIO.close()
output_file.close()
return exitCode
Loading

0 comments on commit 7347d19

Please sign in to comment.