Skip to content

Commit 9bc8fc8

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

17 files changed

+285
-275
lines changed

cyclonedx_py/_internal/utils/pep639.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from base64 import b64encode
2525
from collections.abc import Generator
2626
from os.path import dirname, join
27-
from typing import TYPE_CHECKING, Any
27+
from typing import TYPE_CHECKING, Any, AnyStr
2828

2929
from cyclonedx.model import AttachedText, Encoding
3030
from cyclonedx.model.license import DisjunctiveLicense, LicenseAcknowledgement
@@ -67,10 +67,8 @@ def project2licenses(project: dict[str, Any], lfac: 'LicenseFactory',
6767
del err
6868
continue
6969
with plicense_fileh:
70-
yield DisjunctiveLicense(name=f'declared license file: {plfile}',
71-
acknowledgement=lack,
72-
text=AttachedText(encoding=Encoding.BASE_64,
73-
content=b64encode(plicense_fileh.read()).decode()))
70+
content = plicense_fileh.read()
71+
yield _make_license_from_content(plfile, content, lack)
7472
# Silently skip any other types (including string/PEP 621)
7573
return None
7674

@@ -107,17 +105,29 @@ def dist2licenses_from_files(
107105
logger.debug('Error: failed to read license file %r for dist %r',
108106
mlfile, metadata['Name'])
109107
continue
110-
encoding = None
111-
content_type = guess_type(mlfile) or AttachedText.DEFAULT_CONTENT_TYPE
112-
# per default, license files are human-readable texts.
113-
if not content_type.startswith('text/'):
114-
encoding = Encoding.BASE_64
115-
content = b64encode(content.encode('utf-8')).decode('ascii')
116-
yield DisjunctiveLicense(
117-
name=f'declared license file: {mlfile}',
118-
acknowledgement=lack,
119-
text=AttachedText(
120-
content=content,
121-
encoding=encoding,
122-
content_type=content_type
123-
))
108+
yield _make_license_from_content(mlfile, content, lack)
109+
110+
111+
def _make_license_from_content(file_name: str, content: AnyStr, lack: 'LicenseAcknowledgement') -> DisjunctiveLicense:
112+
encoding = None
113+
content_type = guess_type(file_name) or AttachedText.DEFAULT_CONTENT_TYPE
114+
# per default, license files are human-readable texts.
115+
if content_type.startswith('text/'):
116+
content_s = bytes2str(content) \
117+
if isinstance(content, bytes) \
118+
else content
119+
else:
120+
encoding = Encoding.BASE_64
121+
content_s = b64encode(
122+
content
123+
if isinstance(content, bytes)
124+
else content.encode('utf-8')
125+
).decode('ascii')
126+
return DisjunctiveLicense(
127+
name=f'{lack.value} license file: {file_name}',
128+
acknowledgement=lack,
129+
text=AttachedText(
130+
content=content_s,
131+
encoding=encoding,
132+
content_type=content_type
133+
))

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

Lines changed: 16 additions & 24 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: 16 additions & 8 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.4.json.bin

Lines changed: 16 additions & 24 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.4.xml.bin

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

0 commit comments

Comments
 (0)