Skip to content

Commit e29f842

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

12 files changed

+31
-29
lines changed

cyclonedx_py/_internal/utils/pep621.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,25 @@ def project2licenses(project: dict[str, Any], lfac: 'LicenseFactory',
7171
# per spec:
7272
# > These keys are mutually exclusive, so a tool MUST raise an error if the metadata specifies both keys.
7373
raise ValueError('`license.file` and `license.text` are mutually exclusive,')
74-
if gather_text and 'file' in plicense:
75-
# Per PEP 621 spec:
76-
# > [...] a string value that is a relative file path [...].
77-
with open(join(dirname(fpath), *PurePosixPath(plicense['file']).parts), 'rb') as plicense_fileh:
78-
content_type = guess_type(plicense_fileh.name) or AttachedText.DEFAULT_CONTENT_TYPE
79-
yield DisjunctiveLicense(
80-
name=f"declared license of '{project['name']}'",
81-
acknowledgement=lack,
82-
text=AttachedText(
83-
content_type=content_type,
84-
encoding=Encoding.BASE_64,
85-
# Per PEP 621 spec:
86-
# > Tools MUST assume the file’s encoding is UTF-8.
87-
# But in reality, we found non-printable bytes in some files!
88-
content=b64encode(
89-
plicense_fileh.read()
90-
).decode('ascii')))
74+
if len(plicense_file := plicense.get('file', '')) > 0:
75+
if gather_text:
76+
# Per PEP 621 spec:
77+
# > [...] a string value that is a relative file path [...].
78+
# > Tools MUST assume the file’s encoding is UTF-8.
79+
# But in reality, we found non-printable bytes in some files!
80+
with open(join(dirname(fpath), *PurePosixPath(plicense_file).parts), 'rb') as plicense_fileh:
81+
content_type = guess_type(plicense_file) or AttachedText.DEFAULT_CONTENT_TYPE
82+
yield DisjunctiveLicense(
83+
name=f"declared license of '{project['name']}'",
84+
acknowledgement=lack,
85+
text=AttachedText(
86+
content_type=content_type,
87+
encoding=Encoding.BASE_64,
88+
content=b64encode(
89+
plicense_fileh.read()
90+
).decode('ascii')))
9191
elif len(plicense_text := plicense.get('text', '')) > 0:
92-
license = lfac.make_from_string(plicense_text,
93-
license_acknowledgement=lack)
92+
license = lfac.make_from_string(plicense_text, license_acknowledgement=lack)
9493
if isinstance(license, DisjunctiveLicense) and license.id is None:
9594
if gather_text:
9695
# per spec, `License` is either a SPDX ID/Expression, or a license text(not name!)

cyclonedx_py/_internal/utils/pep639.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def dist2licenses_from_files(
107107

108108
def _make_license_from_content(file_name: str, content: Union[str, bytes],
109109
lack: 'LicenseAcknowledgement') -> DisjunctiveLicense:
110+
# In the past, we did best-effort decoding to string,
111+
# see https://github.com/CycloneDX/cyclonedx-python/blob/b7a8f64ae212c5a5fd6b7cf8c83851ba692df256/cyclonedx_py/_internal/utils/pep639.py#L67-L71 # noqa:E501
112+
# But this was dropped, in favour of base64 encoding; CycloneDXis for machines, not humans!
110113
content_type = guess_type(file_name) or AttachedText.DEFAULT_CONTENT_TYPE
111114
return DisjunctiveLicense(
112115
name=f'{lack.value} license file: {"/".join(Path(file_name).parts)}',

tests/_data/snapshots/environment/texts_with-license-file_1.2.json.bin

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/_data/snapshots/environment/texts_with-license-file_1.2.xml.bin

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/_data/snapshots/environment/texts_with-license-file_1.4.json.bin

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/_data/snapshots/environment/texts_with-license-file_1.4.xml.bin

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/_data/snapshots/environment/texts_with-license-file_1.5.json.bin

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/_data/snapshots/environment/texts_with-license-file_1.5.xml.bin

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)