Skip to content

Commit 73f0c6a

Browse files
committed
pylock: refactor
Move conversion of install requirements to pylock to utils namespace, so model.pylock has no pip dependencies.
1 parent f6cf175 commit 73f0c6a

File tree

3 files changed

+128
-116
lines changed

3 files changed

+128
-116
lines changed

src/pip/_internal/commands/lock.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
with_cleanup,
1111
)
1212
from pip._internal.cli.status_codes import SUCCESS
13-
from pip._internal.models.pylock import Pylock, is_valid_pylock_file_name
13+
from pip._internal.models.pylock import is_valid_pylock_file_name
1414
from pip._internal.operations.build.build_tracker import get_build_tracker
1515
from pip._internal.req.req_install import (
1616
check_legacy_setup_py_options,
@@ -19,6 +19,7 @@
1919
from pip._internal.utils.misc import (
2020
get_pip_version,
2121
)
22+
from pip._internal.utils.pylock import pylock_from_install_requirements
2223
from pip._internal.utils.temp_dir import TempDirectory
2324

2425
logger = getLogger(__name__)
@@ -160,7 +161,7 @@ def run(self, options: Values, args: List[str]) -> int:
160161
output_file_path,
161162
)
162163
base_dir = output_file_path.parent
163-
pylock_toml = Pylock.from_install_requirements(
164+
pylock_toml = pylock_from_install_requirements(
164165
requirement_set.requirements.values(), base_dir=base_dir
165166
).as_toml()
166167
if options.output_file == "-":

src/pip/_internal/models/pylock.py

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
from pip._vendor.packaging.specifiers import SpecifierSet
2424
from pip._vendor.packaging.version import Version
2525

26-
from pip._internal.models.direct_url import ArchiveInfo, DirInfo, VcsInfo
27-
from pip._internal.models.link import Link
28-
from pip._internal.req.req_install import InstallRequirement
29-
from pip._internal.utils.urls import url_to_path
30-
3126
if TYPE_CHECKING:
3227
if sys.version_info >= (3, 11):
3328
from typing import Self
@@ -381,97 +376,6 @@ def from_dict(cls, d: Dict[str, Any]) -> "Self":
381376
)
382377
return package
383378

384-
@classmethod
385-
def from_install_requirement(
386-
cls, ireq: InstallRequirement, base_dir: Path
387-
) -> "Self":
388-
base_dir = base_dir.resolve()
389-
dist = ireq.get_dist()
390-
download_info = ireq.download_info
391-
assert download_info
392-
package_version = None
393-
package_vcs = None
394-
package_directory = None
395-
package_archive = None
396-
package_sdist = None
397-
package_wheels = None
398-
if ireq.is_direct:
399-
if isinstance(download_info.info, VcsInfo):
400-
package_vcs = PackageVcs(
401-
type=download_info.info.vcs,
402-
url=download_info.url,
403-
path=None,
404-
requested_revision=download_info.info.requested_revision,
405-
commit_id=download_info.info.commit_id,
406-
subdirectory=download_info.subdirectory,
407-
)
408-
elif isinstance(download_info.info, DirInfo):
409-
package_directory = PackageDirectory(
410-
path=(
411-
Path(url_to_path(download_info.url))
412-
.resolve()
413-
.relative_to(base_dir)
414-
.as_posix()
415-
),
416-
editable=(
417-
download_info.info.editable
418-
if download_info.info.editable
419-
else None
420-
),
421-
subdirectory=download_info.subdirectory,
422-
)
423-
elif isinstance(download_info.info, ArchiveInfo):
424-
if not download_info.info.hashes:
425-
raise NotImplementedError()
426-
package_archive = PackageArchive(
427-
url=download_info.url,
428-
path=None,
429-
size=None, # not supported
430-
hashes=download_info.info.hashes,
431-
subdirectory=download_info.subdirectory,
432-
)
433-
else:
434-
# should never happen
435-
raise NotImplementedError()
436-
else:
437-
package_version = dist.version
438-
if isinstance(download_info.info, ArchiveInfo):
439-
if not download_info.info.hashes:
440-
raise NotImplementedError()
441-
link = Link(download_info.url)
442-
if link.is_wheel:
443-
package_wheels = [
444-
PackageWheel(
445-
name=link.filename,
446-
url=download_info.url,
447-
path=None,
448-
size=None, # not supported
449-
hashes=download_info.info.hashes,
450-
)
451-
]
452-
else:
453-
package_sdist = PackageSdist(
454-
name=link.filename,
455-
url=download_info.url,
456-
path=None,
457-
size=None, # not supported
458-
hashes=download_info.info.hashes,
459-
)
460-
else:
461-
# should never happen
462-
raise NotImplementedError()
463-
return cls(
464-
name=dist.canonical_name,
465-
version=package_version,
466-
marker=None, # not supported
467-
requires_python=None, # not supported
468-
vcs=package_vcs,
469-
directory=package_directory,
470-
archive=package_archive,
471-
sdist=package_sdist,
472-
wheels=package_wheels,
473-
)
474-
475379

476380
@dataclass
477381
class Pylock:
@@ -509,21 +413,3 @@ def from_dict(cls, d: Dict[str, Any]) -> "Self":
509413
requires_python=_get_as(d, str, SpecifierSet, "requires-python"),
510414
packages=_get_required_list_of_objects(d, Package, "packages"),
511415
)
512-
513-
@classmethod
514-
def from_install_requirements(
515-
cls, install_requirements: Iterable[InstallRequirement], base_dir: Path
516-
) -> "Self":
517-
return cls(
518-
lock_version=Version("1.0"),
519-
environments=None, # not supported
520-
requires_python=None, # not supported
521-
created_by="pip",
522-
packages=sorted(
523-
(
524-
Package.from_install_requirement(ireq, base_dir)
525-
for ireq in install_requirements
526-
),
527-
key=lambda p: p.name,
528-
),
529-
)

src/pip/_internal/utils/pylock.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
from pathlib import Path
2+
from typing import Iterable
3+
4+
from pip._vendor.packaging.version import Version
5+
6+
from pip._internal.models.direct_url import ArchiveInfo, DirInfo, VcsInfo
7+
from pip._internal.models.link import Link
8+
from pip._internal.models.pylock import (
9+
Package,
10+
PackageArchive,
11+
PackageDirectory,
12+
PackageSdist,
13+
PackageVcs,
14+
PackageWheel,
15+
Pylock,
16+
)
17+
from pip._internal.req.req_install import InstallRequirement
18+
from pip._internal.utils.urls import url_to_path
19+
20+
21+
def _pylock_package_from_install_requirement(
22+
ireq: InstallRequirement, base_dir: Path
23+
) -> Package:
24+
base_dir = base_dir.resolve()
25+
dist = ireq.get_dist()
26+
download_info = ireq.download_info
27+
assert download_info
28+
package_version = None
29+
package_vcs = None
30+
package_directory = None
31+
package_archive = None
32+
package_sdist = None
33+
package_wheels = None
34+
if ireq.is_direct:
35+
if isinstance(download_info.info, VcsInfo):
36+
package_vcs = PackageVcs(
37+
type=download_info.info.vcs,
38+
url=download_info.url,
39+
path=None,
40+
requested_revision=download_info.info.requested_revision,
41+
commit_id=download_info.info.commit_id,
42+
subdirectory=download_info.subdirectory,
43+
)
44+
elif isinstance(download_info.info, DirInfo):
45+
package_directory = PackageDirectory(
46+
path=(
47+
Path(url_to_path(download_info.url))
48+
.resolve()
49+
.relative_to(base_dir)
50+
.as_posix()
51+
),
52+
editable=(
53+
download_info.info.editable if download_info.info.editable else None
54+
),
55+
subdirectory=download_info.subdirectory,
56+
)
57+
elif isinstance(download_info.info, ArchiveInfo):
58+
if not download_info.info.hashes:
59+
raise NotImplementedError()
60+
package_archive = PackageArchive(
61+
url=download_info.url,
62+
path=None,
63+
size=None, # not supported
64+
hashes=download_info.info.hashes,
65+
subdirectory=download_info.subdirectory,
66+
)
67+
else:
68+
# should never happen
69+
raise NotImplementedError()
70+
else:
71+
package_version = dist.version
72+
if isinstance(download_info.info, ArchiveInfo):
73+
if not download_info.info.hashes:
74+
raise NotImplementedError()
75+
link = Link(download_info.url)
76+
if link.is_wheel:
77+
package_wheels = [
78+
PackageWheel(
79+
name=link.filename,
80+
url=download_info.url,
81+
path=None,
82+
size=None, # not supported
83+
hashes=download_info.info.hashes,
84+
)
85+
]
86+
else:
87+
package_sdist = PackageSdist(
88+
name=link.filename,
89+
url=download_info.url,
90+
path=None,
91+
size=None, # not supported
92+
hashes=download_info.info.hashes,
93+
)
94+
else:
95+
# should never happen
96+
raise NotImplementedError()
97+
return Package(
98+
name=dist.canonical_name,
99+
version=package_version,
100+
marker=None, # not supported
101+
requires_python=None, # not supported
102+
vcs=package_vcs,
103+
directory=package_directory,
104+
archive=package_archive,
105+
sdist=package_sdist,
106+
wheels=package_wheels,
107+
)
108+
109+
110+
def pylock_from_install_requirements(
111+
install_requirements: Iterable[InstallRequirement], base_dir: Path
112+
) -> Pylock:
113+
return Pylock(
114+
lock_version=Version("1.0"),
115+
environments=None, # not supported
116+
requires_python=None, # not supported
117+
created_by="pip",
118+
packages=sorted(
119+
(
120+
_pylock_package_from_install_requirement(ireq, base_dir)
121+
for ireq in install_requirements
122+
),
123+
key=lambda p: p.name,
124+
),
125+
)

0 commit comments

Comments
 (0)