Skip to content

Commit 8b21e93

Browse files
committed
Adding bare and json outputs to license command.
1 parent 23ac3a3 commit 8b21e93

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ History
66
-------------------
77

88
* Current unstable version
9+
* Added bare and json outputs to license command
910

1011
1.10.0 (2020-12-20)
1112
-------------------

safety/cli.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ def review(full_report, bare, file):
128128
"environment variable. Default: empty")
129129
@click.option("--db", default="",
130130
help="Path to a local license database. Default: empty")
131+
@click.option("--json/--no-json", default=False,
132+
help="Output packages licenses in JSON format. Default: --no-json")
133+
@click.option("--bare/--not-bare", default=False,
134+
help='Output packages licenses names only. '
135+
'Useful in combination with other tools. '
136+
'Default: --not-bare')
131137
@click.option("--cache/--no-cache", default=True,
132138
help='Whether license database file should be cached.'
133139
'Default: --cache')
@@ -139,7 +145,7 @@ def review(full_report, bare, file):
139145
help="Proxy port number --proxy-port")
140146
@click.option("proxyprotocol", "--proxy-protocol", "-pr", multiple=False, type=str, default='http',
141147
help="Proxy protocol (https or http) --proxy-protocol")
142-
def license(key, db, cache, files, proxyprotocol, proxyhost, proxyport):
148+
def license(key, db, json, bare, cache, files, proxyprotocol, proxyhost, proxyport):
143149

144150
if files:
145151
packages = list(itertools.chain.from_iterable(read_requirements(f, resolve=True) for f in files))
@@ -172,7 +178,12 @@ def license(key, db, cache, files, proxyprotocol, proxyhost, proxyport):
172178
click.secho("Unable to load licenses database", fg="red", file=sys.stderr)
173179
sys.exit(-1)
174180
filtered_packages_licenses = get_packages_licenses(packages, licenses_db)
175-
output_report = license_report(packages=packages, licenses=filtered_packages_licenses)
181+
output_report = license_report(
182+
packages=packages,
183+
licenses=filtered_packages_licenses,
184+
json_report=json,
185+
bare_report=bare
186+
)
176187
click.secho(output_report, nl=True)
177188

178189

safety/formatter.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ class JsonReport(object):
236236
@staticmethod
237237
def render(vulns, full):
238238
return json.dumps(vulns, indent=4, sort_keys=True)
239+
240+
@staticmethod
241+
def render_licenses(packages_licenses):
242+
return json.dumps(packages_licenses, indent=4, sort_keys=True)
239243

240244

241245
class BareReport(object):
@@ -244,6 +248,14 @@ class BareReport(object):
244248
def render(vulns, full):
245249
return " ".join(set([v.name for v in vulns]))
246250

251+
@staticmethod
252+
def render_licenses(packages_licenses):
253+
licenses = set([pkg_li.get('license') for pkg_li in packages_licenses])
254+
if "N/A" in licenses:
255+
licenses.remove("N/A")
256+
sorted_licenses = sorted(licenses)
257+
return " ".join(sorted_licenses)
258+
247259

248260
def get_used_db(key, db):
249261
key = key if key else os.environ.get("SAFETY_API_KEY", False)
@@ -266,9 +278,13 @@ def report(vulns, full=False, json_report=False, bare_report=False, checked_pack
266278
return BasicReport.render(vulns, full=full, checked_packages=checked_packages, used_db=used_db)
267279

268280

269-
def license_report(packages, licenses):
270-
size = get_terminal_size()
281+
def license_report(packages, licenses, json_report=False, bare_report=False):
282+
if json_report:
283+
return JsonReport.render_licenses(packages_licenses=licenses)
284+
elif bare_report:
285+
return BareReport.render_licenses(packages_licenses=licenses)
271286

287+
size = get_terminal_size()
272288
if size.columns >= 80:
273289
return SheetReport.render_licenses(packages, licenses)
274290
return BasicReport.render_licenses(packages, licenses)

0 commit comments

Comments
 (0)