|
37 | 37 |
|
38 | 38 | from .cdx import url_label_to_ert |
39 | 39 | from .license_trove_classifier import is_license_trove, license_trove2spdx |
| 40 | +from .mimetypes import guess_type |
40 | 41 |
|
41 | 42 | if TYPE_CHECKING: |
42 | 43 | from cyclonedx.factory.license import LicenseFactory |
@@ -73,22 +74,30 @@ def project2licenses(project: dict[str, Any], lfac: 'LicenseFactory', |
73 | 74 | if gather_text and 'file' in plicense: |
74 | 75 | # Per PEP 621 spec: |
75 | 76 | # > [...] 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! |
78 | 77 | 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'))) |
83 | 91 | elif len(plicense_text := plicense.get('text', '')) > 0: |
84 | 92 | license = lfac.make_from_string(plicense_text, |
85 | 93 | license_acknowledgement=lack) |
86 | 94 | if isinstance(license, DisjunctiveLicense) and license.id is None: |
87 | 95 | if gather_text: |
88 | 96 | # 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)) |
92 | 101 | else: |
93 | 102 | yield license |
94 | 103 | # Silently skip any other types (including string/PEP 639) |
|
0 commit comments