Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also look in static for license files #238

Merged
merged 2 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions jupyterlab_server/licenses_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class LicensesManager(LoggingConfigurable):

third_party_licenses_files = List(
Unicode(),
default_value=[DEFAULT_THIRD_PARTY_LICENSE_FILE],
default_value=[
DEFAULT_THIRD_PARTY_LICENSE_FILE,
f"static/{DEFAULT_THIRD_PARTY_LICENSE_FILE}"
],
help="the license report data in built app and federated extensions",
)

Expand Down Expand Up @@ -156,14 +159,13 @@ def report_markdown(self, bundles, full_text=True):
def license_bundle(self, path, bundle):
"""Return the content of a packages's license bundles"""
bundle_json = {"packages": []}
checked_paths = []

for license_file in self.third_party_licenses_files:
licenses_path = path / license_file
self.log.debug("Loading licenses from %s", licenses_path)
if not licenses_path.exists():
self.log.warning(
"Third-party licenses not found for %s: %s", bundle, licenses_path
)
checked_paths += [licenses_path]
continue

try:
Expand All @@ -188,6 +190,11 @@ def license_bundle(self, path, bundle):
)
continue

if not bundle_json["packages"]:
self.log.warning(
"Third-party licenses not found for %s: %s", bundle, checked_paths
)

return bundle_json

def app_static_info(self):
Expand Down Expand Up @@ -222,7 +229,7 @@ def bundles(self, bundles_pattern=".*"):
bundles[app_name] = self.license_bundle(app_path, app_name)

if not bundles:
self.log.warn("No license bundles found at all")
self.log.warning("No license bundles found at all")

return bundles

Expand Down
32 changes: 31 additions & 1 deletion jupyterlab_server/tests/test_licenses_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import mistune

from .. import LicensesApp
from ..licenses_handler import DEFAULT_THIRD_PARTY_LICENSE_FILE
from ..licenses_handler import DEFAULT_THIRD_PARTY_LICENSE_FILE, LicensesManager

# utilities

Expand Down Expand Up @@ -166,3 +166,33 @@ async def test_licenses_cli(licenses_app, capsys, mime_format_parser):

captured = capsys.readouterr()
assert parse(captured.out) is not None


@pytest.fixture
def a_fake_labextension(tmp_path):
"""just enough of an extension to be parsed"""
ext_name = "@an-org/an-extension"
ext_path = tmp_path / ext_name
package_data = {"name": ext_name}
bundle_data = {"packages": [dict(FULL_ENTRY)]}

package_json = ext_path / "package.json"
third_party_licenses = ext_path / "static" / DEFAULT_THIRD_PARTY_LICENSE_FILE

third_party_licenses.parent.mkdir(parents=True)

package_json.write_text(json.dumps(package_data), encoding="utf-8")
third_party_licenses.write_text(json.dumps(bundle_data), encoding="utf-8")

yield ext_path, ext_name


@pytest.fixture
def a_licenses_manager():
yield LicensesManager()


def test_labextension_bundle(a_fake_labextension, a_licenses_manager):
ext_path, ext_name = a_fake_labextension
bundle = a_licenses_manager.license_bundle(ext_path, ext_name)
assert bundle["packages"][0]["name"] == dict(FULL_ENTRY)["name"]