3131from pip ._internal .utils .filetypes import is_archive_file
3232from pip ._internal .utils .misc import is_installable_dir
3333from pip ._internal .utils .packaging import get_requirement
34+ from pip ._internal .utils .pylock import (
35+ package_archive_url ,
36+ package_directory_url ,
37+ package_sdist_url ,
38+ package_vcs_url ,
39+ package_wheel_url ,
40+ )
3441from pip ._internal .utils .urls import path_to_url
3542from pip ._internal .vcs import is_url , vcs
3643
@@ -572,34 +579,6 @@ def install_req_extend_extras(
572579 return result
573580
574581
575- def _url_from_path_and_url (path : str | None , url : str | None ) -> str :
576- if path :
577- return path_to_url (path )
578- else :
579- assert url # guaranteed by pylock validation
580- return url
581-
582-
583- def _url_from_package_vcs (package_vcs : pylock .PackageVcs ) -> str :
584- url = (
585- package_vcs .type
586- + "+"
587- + _url_from_path_and_url (package_vcs .path , package_vcs .url )
588- + "@"
589- + package_vcs .commit_id
590- )
591- if package_vcs .subdirectory :
592- url += "#" + package_vcs .subdirectory
593- return url
594-
595-
596- def _url_from_package_archive (package_archive : pylock .PackageArchive ) -> str :
597- url = _url_from_path_and_url (package_archive .path , package_archive .url )
598- if package_archive .subdirectory :
599- url += "#" + package_archive .subdirectory
600- return url
601-
602-
603582def _pylock_hashes_to_hash_options (hashes : Mapping [str , str ]) -> dict [str , list [str ]]:
604583 return {k : [v ] for k , v in hashes .items ()}
605584
@@ -613,33 +592,37 @@ def install_req_from_pylock_package(
613592 | pylock .PackageSdist
614593 | pylock .PackageWheel
615594 ),
616- comes_from : str | None ,
595+ comes_from : str ,
617596) -> InstallRequirement :
618597 pass
619- # TODO no binary -> use package.sdist
620- # TODO package.index
621- # TODO user_supplied
622- # TODO validate file size?
598+ # TODO: no binary -> use package.sdist
599+ # TODO: package.index
600+ # TODO: user_supplied
601+ # TODO: validate file size?
623602 if isinstance (package_dist , pylock .PackageVcs ):
624603 return InstallRequirement (
625- req = Requirement (f"{ package .name } @ { _url_from_package_vcs (package_dist )} " ),
604+ req = Requirement (
605+ f"{ package .name } @ { package_vcs_url (comes_from , package_dist )} "
606+ ),
626607 comes_from = comes_from ,
627608 )
628609 elif isinstance (package_dist , pylock .PackageArchive ):
629610 return InstallRequirement (
630611 req = Requirement (
631- f"{ package .name } @ { _url_from_package_archive ( package_dist )} "
612+ f"{ package .name } @ { package_archive_url ( comes_from , package_dist )} "
632613 ),
633614 comes_from = comes_from ,
634615 hash_options = _pylock_hashes_to_hash_options (package_dist .hashes ),
635616 )
636617 elif isinstance (package_dist , pylock .PackageDirectory ):
637- # TODO subdirectory
638618 if package_dist .editable :
639- return install_req_from_editable (package_dist .path , comes_from = comes_from )
619+ return install_req_from_editable (
620+ package_directory_url (comes_from , package_dist ), comes_from = comes_from
621+ )
640622 else :
641- # TODO this may interpret the path as a requirement
642- return install_req_from_line (package_dist .path , comes_from = comes_from )
623+ return install_req_from_line (
624+ package_directory_url (comes_from , package_dist ), comes_from = comes_from
625+ )
643626 elif isinstance (package_dist , pylock .PackageSdist ):
644627 if package .version :
645628 version = package .version
@@ -648,7 +631,7 @@ def install_req_from_pylock_package(
648631 ireq = InstallRequirement (
649632 req = Requirement (f"{ package .name } =={ str (version )} " ),
650633 comes_from = comes_from ,
651- link = Link (_url_from_path_and_url ( package_dist . path , package_dist . url )),
634+ link = Link (package_sdist_url ( comes_from , package_dist )),
652635 hash_options = _pylock_hashes_to_hash_options (package_dist .hashes ),
653636 )
654637 ireq .original_link = None # not a direct URL
@@ -661,7 +644,7 @@ def install_req_from_pylock_package(
661644 ireq = InstallRequirement (
662645 req = Requirement (f"{ package .name } =={ str (version )} " ),
663646 comes_from = comes_from ,
664- link = Link (_url_from_path_and_url ( package_dist . path , package_dist . url )),
647+ link = Link (package_wheel_url ( comes_from , package_dist )),
665648 hash_options = _pylock_hashes_to_hash_options (package_dist .hashes ),
666649 )
667650 ireq .original_link = None # not a direct URL
0 commit comments