-
Notifications
You must be signed in to change notification settings - Fork 241
/
directory_dependency.py
95 lines (73 loc) · 2.61 KB
/
directory_dependency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from __future__ import annotations
import functools
from pathlib import Path
from typing import Iterable
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.utils.utils import is_python_project
from poetry.core.packages.utils.utils import path_to_url
from poetry.core.pyproject.toml import PyProjectTOML
class DirectoryDependency(Dependency):
def __init__(
self,
name: str,
path: Path,
groups: Iterable[str] | None = None,
optional: bool = False,
base: Path | None = None,
develop: bool = False,
extras: Iterable[str] | None = None,
) -> None:
self._path = path
self._base = base or Path.cwd()
self._full_path = path
if not self._path.is_absolute():
try:
self._full_path = self._base.joinpath(self._path).resolve()
except FileNotFoundError:
raise ValueError(f"Directory {self._path} does not exist")
self._develop = develop
if not self._full_path.exists():
raise ValueError(f"Directory {self._path} does not exist")
if self._full_path.is_file():
raise ValueError(f"{self._path} is a file, expected a directory")
if not is_python_project(self._full_path):
raise ValueError(
f"Directory {self._full_path} does not seem to be a Python package"
)
super().__init__(
name,
"*",
groups=groups,
optional=optional,
allows_prereleases=True,
source_type="directory",
source_url=self._full_path.as_posix(),
extras=extras,
)
# cache this function to avoid multiple IO reads and parsing
self.supports_poetry = functools.lru_cache(maxsize=1)(self._supports_poetry)
@property
def path(self) -> Path:
return self._path
@property
def full_path(self) -> Path:
return self._full_path
@property
def base(self) -> Path:
return self._base
@property
def develop(self) -> bool:
return self._develop
def _supports_poetry(self) -> bool:
return PyProjectTOML(self._full_path / "pyproject.toml").is_poetry_project()
def is_directory(self) -> bool:
return True
@property
def base_pep_508_name(self) -> str:
requirement = self.pretty_name
if self.extras:
extras = ",".join(sorted(self.extras))
requirement += f"[{extras}]"
path = path_to_url(self.full_path)
requirement += f" @ {path}"
return requirement