Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:webscopeio/license.sh into fix/wh…
Browse files Browse the repository at this point in the history
…itelist
  • Loading branch information
Bogdan Sikora committed Jan 16, 2020
2 parents 915f1b2 + 4beafdc commit 271444e
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 3 deletions.
55 changes: 55 additions & 0 deletions license_sh/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def normalize(license_expression):


licensing = Licensing()
UNKNOWN = "Unknown"

RED = "\033[1;31m"
BLUE = "\033[1;34m"
Expand All @@ -22,6 +23,60 @@ def normalize(license_expression):
REVERSE = "\033[;7m"


def get_npm_license_from_licenses_array(licenses_array):
"""
Extract licenses name from licenses array and join them with AND
"licenses": [{"type":"MIT"}, {"type":"Apache"}]
Arguments:
json_data {json} -- json data to parse license from
version {str} -- version of the package
Returns:
str -- name on the license or Unknown if not found
"""
if not licenses_array:
return None

license_name = UNKNOWN
for license_item in licenses_array:
license_item_type = license_item.get("type", UNKNOWN)
if license_name != UNKNOWN:
license_name = f"{license_name} AND {license_item_type}"
else:
license_name = license_item_type
return license_name


def extract_npm_license(json_data, version: str):
"""
Extract license name from npm package data json
Arguments:
json_data {json} -- json data to parse license from
version {str} -- version of the package
Returns:
str -- name on the license or Unknown if not found
"""
if not json_data:
return None
licenses_array = json_data.get("licenses")
if licenses_array:
return get_npm_license_from_licenses_array(licenses_array)

license_name = json_data.get("license")
if license_name:
return license_name

version_data = json_data.get("versions", {}).get(version, {})
licenses_array = version_data.get("licenses")
if licenses_array:
return get_npm_license_from_licenses_array(licenses_array)
return version_data.get("license", UNKNOWN)


def flatten_dependency_tree(tree):
# remove the root node
return set(
Expand Down
6 changes: 4 additions & 2 deletions license_sh/runners/runners_shared.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import asyncio
import aiohttp as aiohttp
from license_sh.helpers import extract_npm_license

NPM_HOST = "https://registry.npmjs.org"

Expand Down Expand Up @@ -29,9 +30,10 @@ async def fetch_concurrent(urls):
try:
output, version = await result
page = json.loads(output)
license_map[f"{page['name']}@{version}"] = page.get(
"license", "Unknown"
license_map[f"{page['name']}@{version}"] = extract_npm_license(
page, version
)

except json.JSONDecodeError:
pass

Expand Down
177 changes: 176 additions & 1 deletion tests/runners/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import unittest
import json

from anytree import AnyNode
from anytree.exporter import DictExporter
from license_sh.helpers import flatten_dependency_tree, annotate_dep_tree, is_license_ok
from license_sh.helpers import (
flatten_dependency_tree,
annotate_dep_tree,
is_license_ok,
extract_npm_license,
get_npm_license_from_licenses_array,
UNKNOWN,
)


def get_tree():
Expand Down Expand Up @@ -241,6 +249,173 @@ def test_is_license_ok_json(self):
True,
)

def test_extract_npm_license_None_data(self):
name = extract_npm_license(None, None)
self.assertEqual(name, None)

def test_extract_npm_license_None_version(self):
data = json.loads(
"""{
"name": "name"
}"""
)
name = extract_npm_license(data, "Unknown")
self.assertEqual(name, UNKNOWN)

def test_extract_npm_license_simple(self):
data = json.loads(
"""{
"name": "name",
"license": "MIT"
}"""
)
name = extract_npm_license(data, "Unknown")
self.assertEqual(name, "MIT")

def test_extract_npm_licenses_global_single(self):
data = json.loads(
"""{
"name": "name",
"licenses": [{"type":"MIT"}]
}"""
)
name = extract_npm_license(data, "Unknown")
self.assertEqual(name, "MIT")

def test_extract_npm_licenses_global_multiple(self):
data = json.loads(
"""{
"name": "name",
"licenses": [{"type":"MIT"}, {"type":"Apache"}]
}"""
)
name = extract_npm_license(data, "Unknown")
self.assertEqual(name, "MIT AND Apache")

def test_extract_npm_licenses_global_empty(self):
data = json.loads(
"""{
"name": "name",
"licenses": []
}"""
)
name = extract_npm_license(data, "Unknown")
self.assertEqual(name, UNKNOWN)

def test_extract_npm_licenses_specific_version_simple(self):
data = json.loads(
"""{
"name": "name",
"versions": {
"1.0.0": {
"licenses": [{"type":"MIT"}]
}
}
}"""
)
name = extract_npm_license(data, "1.0.0")
self.assertEqual(name, "MIT")

def test_extract_npm_licenses_specific_version_multiple(self):
data = json.loads(
"""{
"name": "name",
"versions": {
"1.0.0": {
"licenses": [{"type":"MIT"}, {"type":"Apache"}]
}
}
}"""
)
name = extract_npm_license(data, "1.0.0")
self.assertEqual(name, "MIT AND Apache")

def test_extract_npm_license_from_versions(self):
data = json.loads(
"""{
"name": "name",
"versions": {
"1.0.0": {
"license": "APACHE"
}
}
}"""
)
name = extract_npm_license(data, "1.0.0")
self.assertEqual(name, "APACHE")

def test_extract_npm_license_global_priority(self):
data = json.loads(
"""{
"name": "name",
"versions": {
"1.0.0": {
"license": "APACHE"
}
},
"license": "MIT"
}"""
)
name = extract_npm_license(data, "1.0.0")
self.assertEqual(name, "MIT")

def test_extract_npm_license_no_versions(self):
data = json.loads(
"""{
"name": "name"
}"""
)
name = extract_npm_license(data, "1.0.0")
self.assertEqual(name, UNKNOWN)

def test_extract_npm_license_no_version(self):
data = json.loads(
"""{
"name": "name",
"versions": {
"6.6.6": {
"license": "APACHE"
}
}
}"""
)
name = extract_npm_license(data, "1.0.0")
self.assertEqual(name, UNKNOWN)

def test_extract_npm_license_no_version_license(self):
data = json.loads(
"""{
"name": "name",
"versions": {
"1.0.0": {}
}
}"""
)
name = extract_npm_license(data, "1.0.0")
self.assertEqual(name, UNKNOWN)

def test_get_npm_license_from_licenses_array_None(self):
name = get_npm_license_from_licenses_array(None)
self.assertEqual(name, None)

def test_get_npm_license_from_licenses_array_None(self):
data = json.loads(
"""{
"licenses": [{"type":"MIT"}, {"type":"Apache"}]
}"""
)
name = get_npm_license_from_licenses_array(data.get("licenses"))
self.assertEqual(name, "MIT AND Apache")

def test_get_npm_license_from_licenses_empty(self):
data = json.loads(
"""{
"licenses": [{}]
}"""
)
name = get_npm_license_from_licenses_array(data.get("licenses"))
self.assertEqual(name, UNKNOWN)


if __name__ == "__main__":
unittest.main()

0 comments on commit 271444e

Please sign in to comment.