Skip to content

Commit 3d3eebe

Browse files
Refactor package assembly for installed wheels
Reference: #4167 Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent e795bc6 commit 3d3eebe

File tree

4 files changed

+42
-57
lines changed

4 files changed

+42
-57
lines changed

src/packagedcode/pypi.py

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,11 @@ def assign_package_to_resources(cls, package, resource, codebase, package_adder)
390390
Assign files to package for an installed wheel. This requires a bit
391391
of navigation around as the files can be in multiple places.
392392
"""
393-
site_packages = resource.parent(codebase).parent(codebase)
393+
dist_info_dir = resource.parent(codebase)
394+
if not dist_info_dir:
395+
return
396+
397+
site_packages = dist_info_dir.parent(codebase)
394398
if not site_packages:
395399
return
396400
package_data = resource.package_data
@@ -400,9 +404,7 @@ def assign_package_to_resources(cls, package, resource, codebase, package_adder)
400404
)
401405

402406
package_data = models.PackageData.from_dict(package_data[0])
403-
404407
package_uid = package.package_uid
405-
406408
if package_uid:
407409
# save thyself!
408410
package_adder(package_uid, resource, codebase)
@@ -414,76 +416,32 @@ def assign_package_to_resources(cls, package, resource, codebase, package_adder)
414416
# relative paths need special treatment
415417
# most of thense are references to bin ../../../bin/wheel
416418
cannot_resolve = False
417-
ref_resource = None
419+
ref_resource = site_packages
418420
while path_ref.startswith('..'):
419421
_, _, path_ref = path_ref.partition('../')
420-
ref_resource = site_packages.parent(codebase)
422+
ref_resource = ref_resource.parent(codebase)
421423
if not ref_resource:
422424
cannot_resolve = True
423425
break
426+
424427
if cannot_resolve or not ref_resource:
425428
# TODO:w e should log these kind of things
426429
continue
427430
else:
428-
if package_uid:
431+
ref_resource = codebase.get_resource(
432+
path=os.path.join(ref_resource.path, path_ref)
433+
)
434+
if ref_resource and package_uid:
429435
package_adder(package_uid, ref_resource, codebase)
430436
else:
431-
ref_resource = get_resource_for_path(
432-
path=path_ref,
433-
root=site_packages,
434-
codebase=codebase,
437+
# These are absolute paths from the site-packages directory
438+
ref_resource = codebase.get_resource(
439+
path=os.path.join(site_packages.path, path_ref)
435440
)
436441
if ref_resource and package_uid:
437442
package_adder(package_uid, ref_resource, codebase)
438443

439444

440-
def get_resource_for_path(path, root, codebase):
441-
"""
442-
Return a resource in ``codebase`` that has a ``path`` relative to the
443-
``root` Resource
444-
445-
For example, say we start from this:
446-
path: this/is/that therefore segments [this, is, that]
447-
root: /usr/foo
448-
449-
We would have these iterations:
450-
iteration1
451-
root = /usr/foo
452-
segments = [this, is, that]
453-
seg this
454-
segments = [is, that]
455-
children = [/usr/foo/this]
456-
root = /usr/foo/this
457-
458-
iteration2
459-
root = /usr/foo/this
460-
segments = [is, that]
461-
seg is
462-
segments = [that]
463-
children = [/usr/foo/this/is]
464-
root = /usr/foo/this/is
465-
466-
iteration3
467-
root = /usr/foo/this/is
468-
segments = [that]
469-
seg that
470-
segments = []
471-
children = [/usr/foo/this/is/that]
472-
root = /usr/foo/this/is/that
473-
474-
finally return root as /usr/foo/this/is/that
475-
"""
476-
segments = path.strip('/').split('/')
477-
while segments:
478-
seg = segments.pop(0)
479-
children = [c for c in root.children(codebase) if c.name == seg]
480-
if len(children) != 1:
481-
return
482-
else:
483-
root = children[0]
484-
return root
485-
486-
487445
class PyprojectTomlHandler(BaseExtractedPythonLayout):
488446
datasource_id = 'pypi_pyproject_toml'
489447
path_patterns = ('*pyproject.toml',)

tests/packagedcode/data/pypi/site-packages/codebase/bin/pip

Whitespace-only changes.

tests/packagedcode/data/pypi/site-packages/codebase/lib/python3.9/site-packages/click-8.0.4.dist-info/RECORD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ click/termui.py,sha256=mZ12uc3-loFaV__vr8buxn9uIjAhy7QwVuZOQ8jDdjc,28873
3939
click/testing.py,sha256=ptpMYgRY7dVfE3UDgkgwayu9ePw98sQI3D7zZXiCpj4,16063
4040
click/types.py,sha256=rj2g3biAXKkNKV8vlwbIKSUlixhXybH84N84fwCYqUU,35092
4141
click/utils.py,sha256=M8tuplcFFHROha3vQ60ZRSakSB_ng6w9e8Uc1AugPZ0,18934
42+
../../../bin/pip,,

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,23 @@
325325
],
326326
"scan_errors": []
327327
},
328+
{
329+
"path": "bin",
330+
"type": "directory",
331+
"package_data": [],
332+
"for_packages": [],
333+
"scan_errors": []
334+
},
335+
{
336+
"path": "bin/pip",
337+
"type": "file",
338+
"package_data": [],
339+
"for_packages": [
340+
"pkg:pypi/pyjpstring@0.0.3?uuid=fixed-uid-done-for-testing-5642512d1758",
341+
"pkg:pypi/click@8.0.4?uuid=fixed-uid-done-for-testing-5642512d1758"
342+
],
343+
"scan_errors": []
344+
},
328345
{
329346
"path": "lib",
330347
"type": "directory",
@@ -849,6 +866,15 @@
849866
"sha256": "33cb6ea6570514744e85adef43ad194526a4481fe783ac3d7bc51cd40ba03d9d",
850867
"sha512": null,
851868
"extra_data": {}
869+
},
870+
{
871+
"path": "../../../bin/pip",
872+
"size": null,
873+
"sha1": null,
874+
"md5": null,
875+
"sha256": null,
876+
"sha512": null,
877+
"extra_data": {}
852878
}
853879
],
854880
"is_private": false,

0 commit comments

Comments
 (0)