Skip to content

Commit

Permalink
Handling single special license (#40)
Browse files Browse the repository at this point in the history
* Handling single special license

---------

Signed-off-by: Christian Henkel <[email protected]>
  • Loading branch information
ct2034 committed Oct 31, 2023
1 parent 861b49f commit 014f354
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 30 deletions.
4 changes: 2 additions & 2 deletions how_to_update.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Note for me:
# Note for me

How to update the project:

Expand All @@ -22,4 +22,4 @@ How to update the project:

## source

<https://realpython.com/pypi-publish-python-package/#build-your-package>
<https://realpython.com/pypi-publish-python-package/#build-your-package>
11 changes: 5 additions & 6 deletions src/ros_license_toolkit/license_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ def to_spdx_license_tag(license_name: str) -> str:

def _eval_glob(glob_str: str, pkg_path: str) -> Set[str]:
"""Evaluate a glob string and return a set of matching relative paths."""
paths = set()
for fpath in glob(os.path.join(pkg_path, glob_str), recursive=True):
if not os.path.isfile(fpath):
continue
paths.add(os.path.relpath(fpath, pkg_path))
return paths
return {
os.path.relpath(fpath, pkg_path)
for fpath in glob(os.path.join(pkg_path, glob_str), recursive=True)
if os.path.isfile(fpath)
}


class LicenseTag:
Expand Down
59 changes: 39 additions & 20 deletions src/ros_license_toolkit/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@

def get_spdx_license_name(scan_results: Dict[str, Any]) -> Optional[str]:
"""Get the SPDX license name from scan results."""
for _license in scan_results["licenses"]:
if _license["score"] >= 99:
return _license["spdx_license_key"]
return None
return next(
(
_license["spdx_license_key"]
for _license in scan_results["licenses"]
if _license["score"] >= 99
),
None,
)


class PackageException(Exception):
Expand Down Expand Up @@ -156,19 +160,10 @@ def package_xml(self):
os.path.join(self.abspath, 'package.xml'))
return self._package_xml

@property
def license_tags(self) -> Dict[str, LicenseTag]:
"""Get all license tags in the package.xml file."""
if self._license_tags is not None:
return self._license_tags
self._license_tags = {}
for license_tag in self.package_xml.iterfind('license'):
tag = LicenseTag(license_tag, self.abspath)
self._license_tags[tag.get_license_id()] = tag

# One license tag can have no source-files attribute.
# But there can be only one such tag.
# This is then the main license.
def _check_single_license_tag_without_source_files(self):
"""One license tag can have no source-files attribute.
But there can be only one such tag.
This is then the main license."""
if len(list(filter(lambda x: not x.has_source_files(),
self._license_tags.values()))) > 1:
raise MoreThanOneLicenseWithoutSourceFilesTag(
Expand All @@ -180,9 +175,10 @@ def license_tags(self) -> Dict[str, LicenseTag]:
list(self._license_tags.values()))
break

# One license tag can have no file attribute, but only one.
# It may be associated to a license text file in the package or
# the repo.
def _check_single_license_tag_without_file_attribute(self):
"""One license tag can have no file attribute, but only one.
It may be associated to a license text file in the package or
the repo."""
if len(list(filter(lambda x: not x.has_license_text_file(),
self._license_tags.values()))) > 1:
raise MoreThanOneLicenseWithoutLicenseTextFile(
Expand All @@ -198,8 +194,31 @@ def license_tags(self) -> Dict[str, LicenseTag]:
if "LICENSE" in license_text_file:
tag.license_text_file = license_text_file
break
# there was no license text found by content, but we can
# look for some files by their name, e.g. LICENSE or COPYING
potential_license_files = [
file
for file in os.listdir(self.abspath)
if "LICENSE" in file or "COPYING" in file
]
# only if there is exactly one such file, we can use it
if len(potential_license_files) == 1:
tag.license_text_file = potential_license_files[0]
break

@property
def license_tags(self) -> Dict[str, LicenseTag]:
"""Get all license tags in the package.xml file."""
if self._license_tags is not None:
return self._license_tags
self._license_tags = {}
for license_tag in self.package_xml.iterfind('license'):
tag = LicenseTag(license_tag, self.abspath)
self._license_tags[tag.get_license_id()] = tag

self._check_single_license_tag_without_source_files()
self._check_single_license_tag_without_file_attribute()

return self._license_tags

@property
Expand Down
12 changes: 12 additions & 0 deletions test/_test_data/copyright_file_contents/test_pkg_unknown_license
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Source: https://fake.git
Upstream-Name: test_pkg_unknown_license

Files:
**
Copyright: (c) 2017 - 2018
License: my own fancy license 1.0
My own fancy license content !! (c) 2017 - 2018
🌴


2 changes: 2 additions & 0 deletions test/_test_data/test_pkg_unknown_license/my_LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
My own fancy license content !! (c) 2017 - 2018
🌴
4 changes: 2 additions & 2 deletions test/systemtest/test_copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"test_pkg_has_code_of_different_license_and_tag",
"test_pkg_spdx_name",
"test_pkg_spdx_tag",
"test_pkg_with_license_and_file"
"test_pkg_with_license_and_file",
"test_pkg_unknown_license"
]


Expand Down Expand Up @@ -70,7 +71,6 @@ def test_get_copyright_file_contents():
pkg_path = os.path.join(TEST_DATA_FOLDER, pkg_name)
pkg = get_packages_in_path(pkg_path)[0]
copyright_file_content = pkg.get_copyright_file_contents()
print(copyright_file_content)
with open(os.path.join(
TEST_DATA_FOLDER,
"copyright_file_contents",
Expand Down

0 comments on commit 014f354

Please sign in to comment.