Skip to content

Commit d8eb4dd

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

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

cyclonedx_py/_internal/utils/packaging.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def metadata2licenses(metadata: 'PackageMetadata', lfac: 'LicenseFactory',
5050
# see spec: https://packaging.python.org/en/latest/specifications/core-metadata/#license
5151
if len(mlicense) <= 0:
5252
continue
53-
license = lfac.make_from_string(mlicense,
54-
license_acknowledgement=lack)
53+
license = lfac.make_from_string(mlicense, license_acknowledgement=lack)
5554
if isinstance(license, DisjunctiveLicense) and license.id is None:
5655
if gather_texts:
5756
# per spec, `License` is either a SPDX ID/Expression, or a license text(not name!)

cyclonedx_py/_internal/utils/pep621.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
from .cdx import url_label_to_ert
3939
from .license_trove_classifier import is_license_trove, license_trove2spdx
40+
from .mimetypes import guess_type
4041

4142
if TYPE_CHECKING:
4243
from cyclonedx.factory.license import LicenseFactory
@@ -73,22 +74,30 @@ def project2licenses(project: dict[str, Any], lfac: 'LicenseFactory',
7374
if gather_text and 'file' in plicense:
7475
# Per PEP 621 spec:
7576
# > [...] a string value that is a relative file path [...].
76-
# > Tools MUST assume the file’s encoding is UTF-8.
77-
# But in reality, we found non-printable bytes in some files!
7877
with open(join(dirname(fpath), *PurePosixPath(plicense['file']).parts), 'rb') as plicense_fileh:
79-
yield DisjunctiveLicense(name=f"declared license of '{project['name']}'",
80-
acknowledgement=lack,
81-
text=AttachedText(encoding=Encoding.BASE_64,
82-
content=b64encode(plicense_fileh.read()).decode()))
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')))
8391
elif len(plicense_text := plicense.get('text', '')) > 0:
8492
license = lfac.make_from_string(plicense_text,
8593
license_acknowledgement=lack)
8694
if isinstance(license, DisjunctiveLicense) and license.id is None:
8795
if gather_text:
8896
# per spec, `License` is either a SPDX ID/Expression, or a license text(not name!)
89-
yield DisjunctiveLicense(name=f"declared license of '{project['name']}'",
90-
acknowledgement=lack,
91-
text=AttachedText(content=plicense_text))
97+
yield DisjunctiveLicense(
98+
name=f"declared license of '{project['name']}'",
99+
acknowledgement=lack,
100+
text=AttachedText(content=plicense_text))
92101
else:
93102
yield license
94103
# Silently skip any other types (including string/PEP 639)

cyclonedx_py/_internal/utils/pep639.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,16 @@ def dist2licenses_from_files(
108108
def _make_license_from_content(file_name: str, content: Union[str, bytes],
109109
lack: 'LicenseAcknowledgement') -> DisjunctiveLicense:
110110
content_type = guess_type(file_name) or AttachedText.DEFAULT_CONTENT_TYPE
111-
# Per PEP 639 spec, license files are human-readable texts.
112-
# But in reality, we found non-printable bytes in some files!
113-
encoding = Encoding.BASE_64
114-
content_s = b64encode(
115-
content
116-
if isinstance(content, bytes)
117-
else content.encode('utf-8')
118-
).decode('ascii')
119-
return DisjunctiveLicense(name=f'{lack.value} license file: {"/".join(Path(file_name).parts)}',
120-
acknowledgement=lack,
121-
text=AttachedText(content=content_s,
122-
encoding=encoding,
123-
content_type=content_type))
111+
return DisjunctiveLicense(
112+
name=f'{lack.value} license file: {"/".join(Path(file_name).parts)}',
113+
acknowledgement=lack,
114+
text=AttachedText(
115+
content_type=content_type,
116+
encoding=Encoding.BASE_64,
117+
# Per PEP 639 spec, license files are human-readable texts.
118+
# But in reality, we found non-printable bytes in some files!
119+
content=b64encode(
120+
content
121+
if isinstance(content, bytes)
122+
else content.encode('utf-8')
123+
).decode('ascii')))

0 commit comments

Comments
 (0)