-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathfile_dependency.py
124 lines (95 loc) · 3.29 KB
/
file_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import hashlib
import io
from pathlib import Path
from typing import TYPE_CHECKING
from typing import FrozenSet
from typing import List
from typing import Optional
from typing import Union
from poetry.core.packages.utils.utils import path_to_url
from .dependency import Dependency
if TYPE_CHECKING:
from .constraints import BaseConstraint
class FileDependency(Dependency):
def __init__(
self,
name: str,
path: Path,
groups: Optional[List[str]] = None,
optional: bool = False,
base: Optional[Path] = None,
extras: Optional[Union[List[str], FrozenSet[str]]] = 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("Directory {} does not exist".format(self._path))
if not self._full_path.exists():
raise ValueError("File {} does not exist".format(self._path))
if self._full_path.is_dir():
raise ValueError("{} is a directory, expected a file".format(self._path))
super(FileDependency, self).__init__(
name,
"*",
groups=groups,
optional=optional,
allows_prereleases=True,
source_type="file",
source_url=self._full_path.as_posix(),
extras=extras,
)
@property
def base(self) -> Path:
return self._base
@property
def path(self) -> Path:
return self._path
@property
def full_path(self) -> Path:
return self._full_path
def is_file(self) -> bool:
return True
def hash(self) -> str:
h = hashlib.sha256()
with self._full_path.open("rb") as fp:
for content in iter(lambda: fp.read(io.DEFAULT_BUFFER_SIZE), b""):
h.update(content)
return h.hexdigest()
def with_constraint(self, constraint: "BaseConstraint") -> "FileDependency":
new = FileDependency(
self.pretty_name,
path=self.path,
base=self.base,
optional=self.is_optional(),
groups=list(self._groups),
extras=self._extras,
)
new._constraint = constraint
new._pretty_constraint = str(constraint)
new.is_root = self.is_root
new.python_versions = self.python_versions
new.marker = self.marker
new.transitive_marker = self.transitive_marker
for in_extra in self.in_extras:
new.in_extras.append(in_extra)
return new
@property
def base_pep_508_name(self) -> str:
requirement = self.pretty_name
if self.extras:
requirement += "[{}]".format(",".join(self.extras))
path = path_to_url(self.path) if self.path.is_absolute() else self.path
requirement += " @ {}".format(path)
return requirement
def __str__(self) -> str:
if self.is_root:
return self._pretty_name
return "{} ({} {})".format(
self._pretty_name, self._pretty_constraint, self._path
)
def __hash__(self) -> int:
return hash((self._name, self._full_path))