Skip to content

Commit acaa562

Browse files
committed
wip
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 06b729a commit acaa562

24 files changed

+1008
-18
lines changed

cyclonedx_py/_internal/environment.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ def __call__(self, *, # type:ignore[override]
145145
rc = None
146146
else:
147147
pyproject = pyproject_load(pyproject_file)
148-
root_c = pyproject2component(pyproject, ctype=mc_type, fpath=pyproject_file, gather_license_texts=False)
148+
root_c = pyproject2component(pyproject, ctype=mc_type,
149+
fpath=pyproject_file,
150+
gather_license_texts=self._gather_license_texts,
151+
logger=self._logger)
149152
root_c.bom_ref.value = 'root-component'
150153
root_d = tuple(pyproject2dependencies(pyproject))
151154
rc = (root_c, root_d)

cyclonedx_py/_internal/pipenv.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def __call__(self, *, # type:ignore[override]
132132
if pyproject_file is None:
133133
rc = None
134134
else:
135-
rc = pyproject_file2component(pyproject_file, ctype=mc_type, gather_license_texts=False)
135+
rc = pyproject_file2component(pyproject_file, ctype=mc_type,
136+
gather_license_texts=False, logger=self._logger)
136137
rc.bom_ref.value = 'root-component'
137138

138139
return self._make_bom(rc,

cyclonedx_py/_internal/requirements.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def __call__(self, *, # type:ignore[override]
116116
if pyproject_file is None:
117117
rc = None
118118
else:
119-
rc = pyproject_file2component(pyproject_file, ctype=mc_type, gather_license_texts=False)
119+
rc = pyproject_file2component(pyproject_file, ctype=mc_type,
120+
gather_license_texts=False, logger=self._logger)
120121
rc.bom_ref.value = 'root-component'
121122

122123
if requirements_file == '-':

cyclonedx_py/_internal/utils/packaging.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@
2525

2626
from .cdx import url_label_to_ert
2727
from .pep621 import classifiers2licenses as pep621_classifiers2licenses
28-
from .pep639 import dist2licenses_from_files as pep639_dist2licenses_from_files
2928

3029
if TYPE_CHECKING: # pragma: no cover
31-
import sys
32-
from logging import Logger
33-
3430
from cyclonedx.factory.license import LicenseFactory
3531
from cyclonedx.model.license import License
3632

cyclonedx_py/_internal/utils/pep639.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@
4040
from cyclonedx.factory.license import LicenseFactory
4141
from cyclonedx.model.license import License
4242

43-
from ..py_interop.packagemetadata import PackageMetadata
44-
4543

4644
def project2licenses(project: dict[str, Any], lfac: 'LicenseFactory',
4745
gather_texts: bool, *,
48-
fpath: str) -> Generator['License', None, None]:
46+
fpath: str,
47+
logger: 'Logger') -> Generator['License', None, None]:
4948
lack = LicenseAcknowledgement.DECLARED
5049
if isinstance(plicense := project.get('license'), str) \
5150
and len(plicense) > 0:
@@ -60,7 +59,14 @@ def project2licenses(project: dict[str, Any], lfac: 'LicenseFactory',
6059
# per spec:
6160
# > Tools MUST assume that license file content is valid UTF-8 encoded text
6261
# anyway, we don't trust this and assume binary
63-
with open(join(plfiles_root, plfile), 'rb') as plicense_fileh:
62+
try:
63+
plicense_fileh = open(join(plfiles_root, plfile), 'rb')
64+
except Exception as err: # pragma: nocover
65+
logger.debug('Error: failed to read license file %r for project %r: %r',
66+
plfile, project.get('name', '<unnamed>'), err)
67+
del err
68+
continue
69+
with plicense_fileh:
6470
yield DisjunctiveLicense(name=f'declared license file: {plfile}',
6571
acknowledgement=lack,
6672
text=AttachedText(encoding=Encoding.BASE_64,
@@ -79,7 +85,7 @@ def dist2licenses_from_files(
7985
logger: 'Logger'
8086
) -> Generator['License', None, None]:
8187
lack = LicenseAcknowledgement.DECLARED
82-
metadata: 'PackageMetadata' = dist.metadata # see https://packaging.python.org/en/latest/specifications/core-metadata/
88+
metadata = dist.metadata # see https://packaging.python.org/en/latest/specifications/core-metadata/
8389
for mlfile in set(metadata.get_all('License-File', ())):
8490
# see spec: https://peps.python.org/pep-0639/#add-license-file-field
8591
# latest spec rev: https://discuss.python.org/t/pep-639-round-3-improving-license-clarity-with-better-package-metadata/53020 # noqa: E501

cyclonedx_py/_internal/utils/pyproject.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@
3535
from .toml import toml_loads
3636

3737
if TYPE_CHECKING: # pragma: no cover
38+
from logging import Logger
39+
3840
from cyclonedx.model.component import Component, ComponentType
3941
from packaging.requirements import Requirement
4042

4143

4244
def pyproject2component(data: dict[str, Any], *,
4345
ctype: 'ComponentType',
4446
fpath: str,
45-
gather_license_texts: bool
47+
gather_license_texts: bool,
48+
logger: 'Logger'
4649
) -> 'Component':
4750
tool = data.get('tool', {})
4851
if poetry := tool.get('poetry'):
@@ -51,7 +54,8 @@ def pyproject2component(data: dict[str, Any], *,
5154
component = pep621_project2component(project, ctype=ctype)
5255
# region licenses
5356
lfac = LicenseFactory()
54-
component.licenses.update(pep639_project2licenses(project, lfac, gather_license_texts, fpath=fpath))
57+
component.licenses.update(pep639_project2licenses(project, lfac, gather_license_texts,
58+
fpath=fpath, logger=logger))
5559
if len(component.licenses) == 0:
5660
# According to PEP 639 spec, if licenses are declared in the "new" style,
5761
# all other license declarations MUST be ignored.
@@ -74,12 +78,14 @@ def pyproject_load(pyproject_file: str) -> dict[str, Any]:
7478

7579
def pyproject_file2component(pyproject_file: str, *,
7680
ctype: 'ComponentType',
77-
gather_license_texts: bool
81+
gather_license_texts: bool,
82+
logger: 'Logger'
7883
) -> 'Component':
7984
return pyproject2component(
8085
pyproject_load(pyproject_file),
8186
ctype=ctype, fpath=pyproject_file,
82-
gather_license_texts=gather_license_texts
87+
gather_license_texts=gather_license_texts,
88+
logger=logger
8389
)
8490

8591

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
thisis the content of the NOTICE file.

tests/_data/infiles/environment/with-license-pep639/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ description = "depenndencies with license declaration accoring to PEP 639"
88
license = "MIT OR GPL-2.0-or-later OR (FSFUL AND BSD-2-Clause)"
99
# https://peps.python.org/pep-0639/#add-license-files-key
1010
license-files = [
11-
"LICEN[CS]E*", "AUTHORS*",
11+
"LICEN[CS]E*", "AUTHORS*", "NOTICE",
1212
"licenses_a/LICENSE.MIT", "licenses_a/*.CC0",
13-
"LICENSE.txt", "licenses_b/**",
13+
"licenses_b/**",
1414
"nonexisting_file", "nonexisting_dir/foo",
1515
]
1616

tests/_data/snapshots/environment/pep639-texts_with-license-pep639_1.3.json.bin

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/_data/snapshots/environment/pep639-texts_with-license-pep639_1.3.xml.bin

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)