-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
add.py
292 lines (238 loc) · 9.91 KB
/
add.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from __future__ import annotations
import contextlib
from typing import Any
from cleo.helpers import argument
from cleo.helpers import option
from packaging.utils import canonicalize_name
from poetry.core.packages.dependency_group import MAIN_GROUP
from tomlkit.toml_document import TOMLDocument
from poetry.console.commands.init import InitCommand
from poetry.console.commands.installer_command import InstallerCommand
class AddCommand(InstallerCommand, InitCommand):
name = "add"
description = "Adds a new dependency to <comment>pyproject.toml</>."
arguments = [argument("name", "The packages to add.", multiple=True)]
options = [
option(
"group",
"-G",
"The group to add the dependency to.",
flag=False,
default=MAIN_GROUP,
),
option(
"dev",
"D",
"Add as a development dependency. (<warning>Deprecated</warning>)",
),
option("editable", "e", "Add vcs/path dependencies as editable."),
option(
"extras",
"E",
"Extras to activate for the dependency.",
flag=False,
multiple=True,
),
option("optional", None, "Add as an optional dependency."),
option(
"python",
None,
"Python version for which the dependency must be installed.",
flag=False,
),
option(
"platform",
None,
"Platforms for which the dependency must be installed.",
flag=False,
),
option(
"source",
None,
"Name of the source to use to install the package.",
flag=False,
),
option("allow-prereleases", None, "Accept prereleases."),
option(
"dry-run",
None,
"Output the operations but do not execute anything (implicitly enables"
" --verbose).",
),
option("lock", None, "Do not perform operations (only update the lockfile)."),
]
examples = """\
If you do not specify a version constraint, poetry will choose a suitable one based on\
the available package versions.
You can specify a package in the following forms:
- A single name (<b>requests</b>)
- A name and a constraint (<b>requests@^2.23.0</b>)
- A git url (<b>git+https://github.com/python-poetry/poetry.git</b>)
- A git url with a revision\
(<b>git+https://github.com/python-poetry/poetry.git#develop</b>)
- A git SSH url (<b>git+ssh://github.com/python-poetry/poetry.git</b>)
- A git SSH url with a revision\
(<b>git+ssh://github.com/python-poetry/poetry.git#develop</b>)
- A file path (<b>../my-package/my-package.whl</b>)
- A directory (<b>../my-package/</b>)
- A url (<b>https://example.com/packages/my-package-0.1.0.tar.gz</b>)
"""
help = f"""\
The add command adds required packages to your <comment>pyproject.toml</> and installs\
them.
{examples}
"""
loggers = ["poetry.repositories.pypi_repository", "poetry.inspection.info"]
def handle(self) -> int:
from poetry.core.constraints.version import parse_constraint
from tomlkit import inline_table
from tomlkit import parse as parse_toml
from tomlkit import table
from poetry.factory import Factory
packages = self.argument("name")
if self.option("dev"):
self.line_error(
"<warning>The --dev option is deprecated, "
"use the `--group dev` notation instead.</warning>"
)
group = "dev"
else:
group = self.option("group", self.default_group or MAIN_GROUP)
if self.option("extras") and len(packages) > 1:
raise ValueError(
"You can only specify one package when using the --extras option"
)
# tomlkit types are awkward to work with, treat content as a mostly untyped
# dictionary.
content: dict[str, Any] = self.poetry.file.read()
poetry_content = content["tool"]["poetry"]
if group == MAIN_GROUP:
if "dependencies" not in poetry_content:
poetry_content["dependencies"] = table()
section = poetry_content["dependencies"]
else:
if "group" not in poetry_content:
poetry_content["group"] = table(is_super_table=True)
groups = poetry_content["group"]
if group not in groups:
dependencies_toml: dict[str, Any] = parse_toml(
f"[tool.poetry.group.{group}.dependencies]\n\n"
)
group_table = dependencies_toml["tool"]["poetry"]["group"][group]
poetry_content["group"][group] = group_table
if "dependencies" not in poetry_content["group"][group]:
poetry_content["group"][group]["dependencies"] = table()
section = poetry_content["group"][group]["dependencies"]
existing_packages = self.get_existing_packages_from_input(packages, section)
if existing_packages:
self.notify_about_existing_packages(existing_packages)
packages = [name for name in packages if name not in existing_packages]
if not packages:
self.line("Nothing to add.")
return 0
requirements = self._determine_requirements(
packages,
allow_prereleases=self.option("allow-prereleases"),
source=self.option("source"),
)
for _constraint in requirements:
version = _constraint.get("version")
if version is not None:
# Validate version constraint
assert isinstance(version, str)
parse_constraint(version)
constraint: dict[str, Any] = inline_table()
for name, value in _constraint.items():
if name == "name":
continue
constraint[name] = value
if self.option("optional"):
constraint["optional"] = True
if self.option("allow-prereleases"):
constraint["allow-prereleases"] = True
if self.option("extras"):
extras = []
for extra in self.option("extras"):
extras += extra.split()
constraint["extras"] = self.option("extras")
if self.option("editable"):
if "git" in _constraint or "path" in _constraint:
constraint["develop"] = True
else:
self.line_error(
"\n"
"<error>Failed to add packages. "
"Only vcs/path dependencies support editable installs. "
f"<c1>{_constraint['name']}</c1> is neither."
)
self.line_error("\nNo changes were applied.")
return 1
if self.option("python"):
constraint["python"] = self.option("python")
if self.option("platform"):
constraint["platform"] = self.option("platform")
if self.option("source"):
constraint["source"] = self.option("source")
if len(constraint) == 1 and "version" in constraint:
constraint = constraint["version"]
constraint_name = _constraint["name"]
assert isinstance(constraint_name, str)
for key in list(section.keys()):
if canonicalize_name(key) == canonicalize_name(constraint_name):
del section[key]
section[constraint_name] = constraint
with contextlib.suppress(ValueError):
self.poetry.package.dependency_group(group).remove_dependency(
constraint_name
)
self.poetry.package.add_dependency(
Factory.create_dependency(
constraint_name,
constraint,
groups=[group],
root_dir=self.poetry.file.parent,
)
)
# Refresh the locker
self.poetry.set_locker(
self.poetry.locker.__class__(self.poetry.locker.lock.path, poetry_content)
)
self.installer.set_locker(self.poetry.locker)
# Cosmetic new line
self.line("")
self.installer.set_package(self.poetry.package)
self.installer.dry_run(self.option("dry-run"))
self.installer.verbose(self.io.is_verbose())
self.installer.update(True)
if self.option("lock"):
self.installer.lock()
self.installer.whitelist([r["name"] for r in requirements])
status = self.installer.run()
if status == 0 and not self.option("dry-run"):
assert isinstance(content, TOMLDocument)
self.poetry.file.write(content)
return status
def get_existing_packages_from_input(
self, packages: list[str], section: dict[str, Any]
) -> list[str]:
existing_packages = []
for name in packages:
for key in section:
if canonicalize_name(key) == canonicalize_name(name):
existing_packages.append(name)
return existing_packages
@property
def _hint_update_packages(self) -> str:
return (
"\nIf you want to update it to the latest compatible version, you can use"
" `poetry update package`.\nIf you prefer to upgrade it to the latest"
" available version, you can use `poetry add package@latest`.\n"
)
def notify_about_existing_packages(self, existing_packages: list[str]) -> None:
self.line(
"The following packages are already present in the pyproject.toml and will"
" be skipped:\n"
)
for name in existing_packages:
self.line(f" • <c1>{name}</c1>")
self.line(self._hint_update_packages)