Skip to content

Commit 4e567d8

Browse files
Fix license file parsing in pypi metadata files
Reference: #4174 Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent 92b7dae commit 4e567d8

File tree

10 files changed

+849
-21
lines changed

10 files changed

+849
-21
lines changed

src/packagedcode/pypi.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1642,10 +1642,10 @@ def get_declared_license(metainfo):
16421642
# TODO: We should make the declared license as it is, this should be
16431643
# updated in scancode to parse a pure string
16441644
lic = get_attribute(metainfo, 'License')
1645-
1646-
license_file = None
1647-
if lic and 'file' in lic:
1648-
license_file = lic.pop('file')
1645+
license_file = get_attribute(metainfo, 'License-File')
1646+
if not license_file and lic:
1647+
if isinstance(lic, dict) and 'file' in lic.keys():
1648+
license_file = lic.pop('file')
16491649

16501650
if lic and not lic == 'UNKNOWN':
16511651
if 'text' in lic:

tests/packagedcode/data/pypi/site-packages/site-packages-expected.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@
187187
"Documentation": "https://click.palletsprojects.com/",
188188
"Changes": "https://click.palletsprojects.com/changes/",
189189
"Twitter": "https://twitter.com/PalletsTeam",
190-
"Chat": "https://discord.gg/pallets"
190+
"Chat": "https://discord.gg/pallets",
191+
"license_file": "LICENSE.rst"
191192
},
192193
"repository_homepage_url": "https://pypi.org/project/click",
193194
"repository_download_url": "https://pypi.org/packages/source/c/click/click-8.0.4.tar.gz",
@@ -717,7 +718,8 @@
717718
"Documentation": "https://click.palletsprojects.com/",
718719
"Changes": "https://click.palletsprojects.com/changes/",
719720
"Twitter": "https://twitter.com/PalletsTeam",
720-
"Chat": "https://discord.gg/pallets"
721+
"Chat": "https://discord.gg/pallets",
722+
"license_file": "LICENSE.rst"
721723
},
722724
"dependencies": [
723725
{

tests/packagedcode/data/pypi/solo-metadata/expected.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@
9393
"source_packages": [],
9494
"is_private": false,
9595
"is_virtual": false,
96-
"extra_data": {},
96+
"extra_data": {
97+
"license_file": "apache-2.0.LICENSE"
98+
},
9799
"repository_homepage_url": "https://pypi.org/project/scancode-toolkit",
98100
"repository_download_url": "https://pypi.org/packages/source/s/scancode-toolkit/scancode-toolkit-31.0.0b1.tar.gz",
99101
"api_data_url": "https://pypi.org/pypi/scancode-toolkit/31.0.0b1/json",
@@ -207,7 +209,9 @@
207209
"file_references": [],
208210
"is_private": false,
209211
"is_virtual": false,
210-
"extra_data": {},
212+
"extra_data": {
213+
"license_file": "apache-2.0.LICENSE"
214+
},
211215
"dependencies": [],
212216
"repository_homepage_url": "https://pypi.org/project/scancode-toolkit",
213217
"repository_download_url": "https://pypi.org/packages/source/s/scancode-toolkit/scancode-toolkit-31.0.0b1.tar.gz",

tests/packagedcode/data/pypi/source-package/pip-22.0.4-pypi-package-expected.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@
103103
"is_virtual": false,
104104
"extra_data": {
105105
"Documentation": "https://pip.pypa.io",
106-
"Changelog": "https://pip.pypa.io/en/stable/news/"
106+
"Changelog": "https://pip.pypa.io/en/stable/news/",
107+
"license_file": "LICENSE.txt"
107108
},
108109
"repository_homepage_url": "https://pypi.org/project/pip",
109110
"repository_download_url": "https://pypi.org/packages/source/p/pip/pip-22.0.4.tar.gz",
@@ -264,7 +265,8 @@
264265
"is_virtual": false,
265266
"extra_data": {
266267
"Documentation": "https://pip.pypa.io",
267-
"Changelog": "https://pip.pypa.io/en/stable/news/"
268+
"Changelog": "https://pip.pypa.io/en/stable/news/",
269+
"license_file": "LICENSE.txt"
268270
},
269271
"dependencies": [],
270272
"repository_homepage_url": "https://pypi.org/project/pip",
@@ -1111,7 +1113,8 @@
11111113
"is_virtual": false,
11121114
"extra_data": {
11131115
"Documentation": "https://pip.pypa.io",
1114-
"Changelog": "https://pip.pypa.io/en/stable/news/"
1116+
"Changelog": "https://pip.pypa.io/en/stable/news/",
1117+
"license_file": "LICENSE.txt"
11151118
},
11161119
"dependencies": [],
11171120
"repository_homepage_url": "https://pypi.org/project/pip",

tests/packagedcode/data/pypi/source-package/pip-22.0.4-pypi-package-only-expected.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
"is_virtual": false,
9494
"extra_data": {
9595
"Documentation": "https://pip.pypa.io",
96-
"Changelog": "https://pip.pypa.io/en/stable/news/"
96+
"Changelog": "https://pip.pypa.io/en/stable/news/",
97+
"license_file": "LICENSE.txt"
9798
},
9899
"dependencies": [],
99100
"repository_homepage_url": "https://pypi.org/project/pip",
@@ -409,12 +410,12 @@
409410
"notice_text": null,
410411
"source_packages": [],
411412
"file_references": [],
413+
"is_private": false,
414+
"is_virtual": false,
412415
"extra_data": {
413416
"Documentation": "https://pip.pypa.io",
414417
"Changelog": "https://pip.pypa.io/en/stable/news/"
415418
},
416-
"is_private": false,
417-
"is_virtual": false,
418419
"dependencies": [],
419420
"repository_homepage_url": "https://pypi.org/project/pip",
420421
"repository_download_url": null,
@@ -793,7 +794,8 @@
793794
"is_virtual": false,
794795
"extra_data": {
795796
"Documentation": "https://pip.pypa.io",
796-
"Changelog": "https://pip.pypa.io/en/stable/news/"
797+
"Changelog": "https://pip.pypa.io/en/stable/news/",
798+
"license_file": "LICENSE.txt"
797799
},
798800
"dependencies": [],
799801
"repository_homepage_url": "https://pypi.org/project/pip",

tests/packagedcode/data/pypi/source-package/pip-22.0.4-pypi-package-with-license-expected.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@
103103
"is_virtual": false,
104104
"extra_data": {
105105
"Documentation": "https://pip.pypa.io",
106-
"Changelog": "https://pip.pypa.io/en/stable/news/"
106+
"Changelog": "https://pip.pypa.io/en/stable/news/",
107+
"license_file": "LICENSE.txt"
107108
},
108109
"repository_homepage_url": "https://pypi.org/project/pip",
109110
"repository_download_url": "https://pypi.org/packages/source/p/pip/pip-22.0.4.tar.gz",
@@ -518,7 +519,8 @@
518519
"is_virtual": false,
519520
"extra_data": {
520521
"Documentation": "https://pip.pypa.io",
521-
"Changelog": "https://pip.pypa.io/en/stable/news/"
522+
"Changelog": "https://pip.pypa.io/en/stable/news/",
523+
"license_file": "LICENSE.txt"
522524
},
523525
"dependencies": [],
524526
"repository_homepage_url": "https://pypi.org/project/pip",
@@ -1660,7 +1662,8 @@
16601662
"is_virtual": false,
16611663
"extra_data": {
16621664
"Documentation": "https://pip.pypa.io",
1663-
"Changelog": "https://pip.pypa.io/en/stable/news/"
1665+
"Changelog": "https://pip.pypa.io/en/stable/news/",
1666+
"license_file": "LICENSE.txt"
16641667
},
16651668
"dependencies": [],
16661669
"repository_homepage_url": "https://pypi.org/project/pip",

tests/packagedcode/data/pypi/unpacked_sdist/prefer-egg-info-pkg-info/celery-expected.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@
107107
"extra_data": {
108108
"Documentation": "https://docs.celeryproject.org/en/latest/index.html",
109109
"Changelog": "https://docs.celeryproject.org/en/stable/changelog.html",
110-
"Funding": "https://opencollective.com/celery"
110+
"Funding": "https://opencollective.com/celery",
111+
"license_file": "LICENSE"
111112
},
112113
"repository_homepage_url": "https://pypi.org/project/celery",
113114
"repository_download_url": "https://pypi.org/packages/source/c/celery/celery-5.2.7.tar.gz",
@@ -1336,7 +1337,8 @@
13361337
"extra_data": {
13371338
"Documentation": "https://docs.celeryproject.org/en/latest/index.html",
13381339
"Changelog": "https://docs.celeryproject.org/en/stable/changelog.html",
1339-
"Funding": "https://opencollective.com/celery"
1340+
"Funding": "https://opencollective.com/celery",
1341+
"license_file": "LICENSE"
13401342
},
13411343
"dependencies": [],
13421344
"repository_homepage_url": "https://pypi.org/project/celery",
@@ -7339,7 +7341,8 @@
73397341
"extra_data": {
73407342
"Documentation": "https://docs.celeryproject.org/en/latest/index.html",
73417343
"Changelog": "https://docs.celeryproject.org/en/stable/changelog.html",
7342-
"Funding": "https://opencollective.com/celery"
7344+
"Funding": "https://opencollective.com/celery",
7345+
"license_file": "LICENSE"
73437346
},
73447347
"dependencies": [
73457348
{

0 commit comments

Comments
 (0)