forked from FHPythonUtils/LicenseCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
125 lines (112 loc) · 3.26 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""Output the licenses used by dependencies and check if these are compatible with the project
license.
"""
from __future__ import annotations
import argparse
from pathlib import Path
from sys import exit as sysexit
from sys import stdout
from fhconfparser import FHConfParser, SimpleConf
from licensecheck import formatter, get_deps, types
stdout.reconfigure(encoding="utf-8")
def cli() -> None:
"""Cli entry point."""
exitCode = 0
parser = argparse.ArgumentParser(description=__doc__, argument_default=argparse.SUPPRESS)
parser.add_argument(
"--format",
"-f",
help=f"Output format. one of: {', '.join(list(formatter.formatMap))}. default=simple",
)
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)",
nargs="+",
)
parser.add_argument(
"--fail-packages",
help="a list of packages to fail (compat=False)",
nargs="+",
)
parser.add_argument(
"--ignore-licenses",
help="a list of licenses to ignore (skipped, compat may still be False)",
nargs="+",
)
parser.add_argument(
"--fail-licenses",
help="a list of licenses to fail (compat=False)",
nargs="+",
)
parser.add_argument(
"--skip-dependencies",
help="a list of packages to skip (compat=True)",
nargs="+",
)
parser.add_argument(
"--zero",
"-0",
help="Return non zero exit code if an incompatible license is found",
action="store_true",
)
args = vars(parser.parse_args())
# ConfigParser (Parses in the following order: `pyproject.toml`,
# `setup.cfg`, `licensecheck.toml`, `licensecheck.json`,
# `licensecheck.ini`, `~/licensecheck.toml`, `~/licensecheck.json`, `~/licensecheck.ini`)
configparser = FHConfParser()
namespace = ["tool"]
configparser.parseConfigList(
[("pyproject.toml", "toml"), ("setup.cfg", "ini")]
+ [
(f"{directory}/licensecheck.{ext}", ext)
for ext in ("toml", "json", "ini")
for directory in [".", str(Path.home())]
],
namespace,
namespace,
)
simpleConf = SimpleConf(configparser, "licensecheck", args)
# File
filename = (
stdout
if simpleConf.get("file") is None
else open(simpleConf.get("file"), "w", encoding="utf-8")
)
# Get list of licenses
myLice, depsWithLicenses = get_deps.getDepsWithLicenses(
simpleConf.get("using", "poetry"),
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("skip_dependencies", []))),
)
# Are any licenses incompatible?
incompatible = any(not lice.licenseCompat for lice in depsWithLicenses)
# Format the results
if simpleConf.get("format", "simple") in formatter.formatMap:
print(
formatter.formatMap[simpleConf.get("format", "simple")](
myLice, sorted(depsWithLicenses)
),
file=filename,
)
else:
exitCode = 2
# Exit code of 1 if args.zero
if simpleConf.get("zero", False) and incompatible:
exitCode = 1
# Cleanup + exit
filename.close()
sysexit(exitCode)