Skip to content

Commit ef37a0f

Browse files
committed
Prefer using PKG-INFO from .egg-info in assemble #3083
* Add test for checking that the .egg-info PKG-INFO is the only Package source reported * Update test expectations Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent c15414b commit ef37a0f

19 files changed

Lines changed: 10153 additions & 183 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
/src/*.egg-info
1212
*.egg-info
1313
!tests/packagedcode/data/pypi/source-package/pip-22.0.4/src/pip.egg-info
14+
!tests/packagedcode/data/pypi/unpacked_sdist/prefer-egg-info-pkg-info/celery/celery.egg-info
1415
/dist
1516
/build
1617
/bin

src/packagedcode/pypi.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,36 @@ def assemble(cls, package_data, resource, codebase, package_adder):
152152

153153
package_resource = None
154154
if resource.name == 'PKG-INFO':
155+
# Initially use current Resource as `package_resource`.
156+
# We'll want update `package_resource` with the Resource of a
157+
# PKG-INFO file that's in an .egg-info Directory.
155158
package_resource = resource
159+
# We want to use the PKG-INFO file from an .egg-info directory, as
160+
# the package info collected from a *.egg_info/PKG-INFO file has
161+
# dependency information that a PKG-INFO from the root of a Python
162+
# project lacks.
163+
parent_resource = resource.parent(codebase)
164+
if (
165+
parent_resource
166+
and not parent_resource.path.endswith('.egg-info')
167+
):
168+
# If we are not in an .egg-info directory, we assume we are at
169+
# the root of a Python codebase and we want to find the
170+
# .egg_info dir
171+
egg_info_dir = None
172+
for sibling in resource.siblings(codebase):
173+
if sibling.path.endswith('.egg-info'):
174+
egg_info_dir = sibling
175+
break
176+
177+
# If we find the .egg_info dir, then we look for the PKG-INFO
178+
# file in it and use that as our package_resource
179+
if egg_info_dir:
180+
for child in egg_info_dir.children(codebase):
181+
if not child.name == 'PKG-INFO':
182+
continue
183+
package_resource = child
184+
break
156185
elif resource.name in datafile_name_patterns:
157186
if resource.has_parent():
158187
siblings = resource.siblings(codebase)
@@ -221,7 +250,17 @@ def assemble(cls, package_data, resource, codebase, package_adder):
221250
package.license_expression = compute_normalized_license(package.declared_license)
222251
package_uid = package.package_uid
223252

224-
root = package_resource.parent(codebase)
253+
package_resource_parent = package_resource.parent(codebase)
254+
if (
255+
package_resource_parent
256+
and package_resource_parent.path.endswith('.egg-info')
257+
):
258+
root = package_resource_parent.parent(codebase)
259+
else:
260+
# We're assuming that our package resource is already at the
261+
# root
262+
root = package_resource_parent
263+
225264
if root:
226265
for py_res in cls.walk_pypi(resource=root, codebase=codebase):
227266
if py_res.is_dir:

tests/packagedcode/data/about/aboutfiles.expected.json

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
11
{
2-
"headers": [
3-
{
4-
"tool_name": "scancode-toolkit",
5-
"options": {
6-
"input": "<path>",
7-
"--json": "<file>",
8-
"--package": true
9-
},
10-
"notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
11-
"output_format_version": "2.0.0",
12-
"message": null,
13-
"errors": [],
14-
"warnings": [],
15-
"extra_data": {
16-
"system_environment": {
17-
"operating_system": "linux",
18-
"cpu_architecture": "64",
19-
"platform": "Linux-5.4.0-109-generic-x86_64-with-Ubuntu-18.04-bionic",
20-
"platform_version": "#123~18.04.1-Ubuntu SMP Fri Apr 8 09:48:52 UTC 2022",
21-
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22-
},
23-
"spdx_license_list_version": "3.16",
24-
"files_count": 3
25-
}
26-
}
27-
],
282
"dependencies": [],
293
"packages": [
304
{

tests/packagedcode/data/build/bazel/end2end-expected.json

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
11
{
2-
"headers": [
3-
{
4-
"tool_name": "scancode-toolkit",
5-
"options": {
6-
"input": "<path>",
7-
"--json-pp": "<file>",
8-
"--package": true
9-
},
10-
"notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
11-
"output_format_version": "2.0.0",
12-
"message": null,
13-
"errors": [],
14-
"warnings": [],
15-
"extra_data": {
16-
"system_environment": {
17-
"operating_system": "linux",
18-
"cpu_architecture": "64",
19-
"platform": "Linux-5.4.0-109-generic-x86_64-with-Ubuntu-18.04-bionic",
20-
"platform_version": "#123~18.04.1-Ubuntu SMP Fri Apr 8 09:48:52 UTC 2022",
21-
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22-
},
23-
"spdx_license_list_version": "3.16",
24-
"files_count": 6
25-
}
26-
}
27-
],
282
"dependencies": [],
293
"packages": [
304
{

tests/packagedcode/data/build/buck/end2end-expected.json

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
11
{
2-
"headers": [
3-
{
4-
"tool_name": "scancode-toolkit",
5-
"options": {
6-
"input": "<path>",
7-
"--json-pp": "<file>",
8-
"--package": true
9-
},
10-
"notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
11-
"output_format_version": "2.0.0",
12-
"message": null,
13-
"errors": [],
14-
"warnings": [],
15-
"extra_data": {
16-
"system_environment": {
17-
"operating_system": "linux",
18-
"cpu_architecture": "64",
19-
"platform": "Linux-5.4.0-109-generic-x86_64-with-Ubuntu-18.04-bionic",
20-
"platform_version": "#123~18.04.1-Ubuntu SMP Fri Apr 8 09:48:52 UTC 2022",
21-
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22-
},
23-
"spdx_license_list_version": "3.16",
24-
"files_count": 7
25-
}
26-
}
27-
],
282
"dependencies": [],
293
"packages": [
304
{

tests/packagedcode/data/cocoapods/assemble/solo/Podfile-expected.json

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
11
{
2-
"headers": [
3-
{
4-
"tool_name": "scancode-toolkit",
5-
"options": {
6-
"input": "<path>",
7-
"--json": "<file>",
8-
"--package": true
9-
},
10-
"notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
11-
"output_format_version": "2.0.0",
12-
"message": null,
13-
"errors": [],
14-
"warnings": [],
15-
"extra_data": {
16-
"system_environment": {
17-
"operating_system": "linux",
18-
"cpu_architecture": "64",
19-
"platform": "Linux-5.4.0-109-generic-x86_64-with-Ubuntu-18.04-bionic",
20-
"platform_version": "#123~18.04.1-Ubuntu SMP Fri Apr 8 09:48:52 UTC 2022",
21-
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22-
},
23-
"spdx_license_list_version": "3.16",
24-
"files_count": 1
25-
}
26-
}
27-
],
282
"dependencies": [],
293
"packages": [],
304
"files": [

tests/packagedcode/data/cocoapods/assemble/solo/RxDataSources.podspec-expected.json

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
11
{
2-
"headers": [
3-
{
4-
"tool_name": "scancode-toolkit",
5-
"options": {
6-
"input": "<path>",
7-
"--json": "<file>",
8-
"--package": true
9-
},
10-
"notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
11-
"output_format_version": "2.0.0",
12-
"message": null,
13-
"errors": [],
14-
"warnings": [],
15-
"extra_data": {
16-
"system_environment": {
17-
"operating_system": "linux",
18-
"cpu_architecture": "64",
19-
"platform": "Linux-5.4.0-109-generic-x86_64-with-Ubuntu-18.04-bionic",
20-
"platform_version": "#123~18.04.1-Ubuntu SMP Fri Apr 8 09:48:52 UTC 2022",
21-
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22-
},
23-
"spdx_license_list_version": "3.16",
24-
"files_count": 1
25-
}
26-
}
27-
],
282
"dependencies": [],
293
"packages": [
304
{

tests/packagedcode/data/npm/get_package_resources.scan.expected.json

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
11
{
2-
"headers": [
3-
{
4-
"tool_name": "scancode-toolkit",
5-
"options": {
6-
"input": "<path>",
7-
"--json": "<file>",
8-
"--package": true
9-
},
10-
"notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
11-
"output_format_version": "2.0.0",
12-
"message": null,
13-
"errors": [],
14-
"warnings": [],
15-
"extra_data": {
16-
"system_environment": {
17-
"operating_system": "linux",
18-
"cpu_architecture": "64",
19-
"platform": "Linux-5.4.0-109-generic-x86_64-with-Ubuntu-18.04-bionic",
20-
"platform_version": "#123~18.04.1-Ubuntu SMP Fri Apr 8 09:48:52 UTC 2022",
21-
"python_version": "3.6.9 (default, Mar 15 2022, 13:55:28) \n[GCC 8.4.0]"
22-
},
23-
"spdx_license_list_version": "3.16",
24-
"files_count": 3
25-
}
26-
}
27-
],
282
"dependencies": [],
293
"packages": [
304
{

0 commit comments

Comments
 (0)