-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathinit.py
517 lines (417 loc) · 17.4 KB
/
init.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import re
import sys
from typing import Dict
from typing import List
from typing import Tuple
from typing import Union
from cleo import option
from tomlkit import inline_table
from poetry.utils._compat import OrderedDict
from poetry.utils._compat import Path
from poetry.utils._compat import urlparse
from .command import Command
from .env_command import EnvCommand
class InitCommand(Command):
name = "init"
description = (
"Creates a basic <comment>pyproject.toml</> file in the current directory."
)
options = [
option("name", None, "Name of the package.", flag=False),
option("description", None, "Description of the package.", flag=False),
option("author", None, "Author name of the package.", flag=False),
option(
"dependency",
None,
"Package to require, with an optional version constraint, "
"e.g. requests:^2.10.0 or requests=2.11.1.",
flag=False,
multiple=True,
),
option(
"dev-dependency",
None,
"Package to require for development, with an optional version constraint, "
"e.g. requests:^2.10.0 or requests=2.11.1.",
flag=False,
multiple=True,
),
option("license", "l", "License of the package.", flag=False),
]
help = """\
The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the current directory.
"""
def __init__(self):
super(InitCommand, self).__init__()
self._pool = None
def handle(self):
from poetry.layouts import layout
from poetry.utils._compat import Path
from poetry.utils.env import SystemEnv
from poetry.vcs.git import GitConfig
if (Path.cwd() / "pyproject.toml").exists():
self.line("<error>A pyproject.toml file already exists.</error>")
return 1
vcs_config = GitConfig()
self.line("")
self.line(
"This command will guide you through creating your <info>pyproject.toml</> config."
)
self.line("")
name = self.option("name")
if not name:
name = Path.cwd().name.lower()
question = self.create_question(
"Package name [<comment>{}</comment>]: ".format(name), default=name
)
name = self.ask(question)
version = "0.1.0"
question = self.create_question(
"Version [<comment>{}</comment>]: ".format(version), default=version
)
version = self.ask(question)
description = self.option("description") or ""
question = self.create_question(
"Description [<comment>{}</comment>]: ".format(description),
default=description,
)
description = self.ask(question)
author = self.option("author")
if not author and vcs_config and vcs_config.get("user.name"):
author = vcs_config["user.name"]
author_email = vcs_config.get("user.email")
if author_email:
author += " <{}>".format(author_email)
question = self.create_question(
"Author [<comment>{}</comment>, n to skip]: ".format(author), default=author
)
question.set_validator(lambda v: self._validate_author(v, author))
author = self.ask(question)
if not author:
authors = []
else:
authors = [author]
license = self.option("license") or ""
question = self.create_question(
"License [<comment>{}</comment>]: ".format(license), default=license
)
question.set_validator(self._validate_license)
license = self.ask(question)
current_env = SystemEnv(Path(sys.executable))
default_python = "^{}".format(
".".join(str(v) for v in current_env.version_info[:2])
)
question = self.create_question(
"Compatible Python versions [<comment>{}</comment>]: ".format(
default_python
),
default=default_python,
)
python = self.ask(question)
self.line("")
requirements = {}
question = "Would you like to define your main dependencies interactively?"
help_message = (
"You can specify a package in the following forms:\n"
" - A single name (<b>requests</b>)\n"
" - A name and a constraint (<b>requests ^2.23.0</b>)\n"
" - A git url (<b>git+https://github.com/python-poetry/poetry.git</b>)\n"
" - A git url with a revision (<b>git+https://github.com/python-poetry/poetry.git#develop</b>)\n"
" - A file path (<b>../my-package/my-package.whl</b>)\n"
" - A directory (<b>../my-package/</b>)\n"
" - An url (<b>https://example.com/packages/my-package-0.1.0.tar.gz</b>)\n"
)
help_displayed = False
if self.confirm(question, True):
self.line(help_message)
help_displayed = True
requirements = self._format_requirements(
self._determine_requirements(self.option("dependency"))
)
self.line("")
dev_requirements = {}
question = (
"Would you like to define your dev dependencies"
" (require-dev) interactively"
)
if self.confirm(question, True):
if not help_displayed:
self.line(help_message)
dev_requirements = self._format_requirements(
self._determine_requirements(self.option("dev-dependency"))
)
self.line("")
layout_ = layout("standard")(
name,
version,
description=description,
author=authors[0] if authors else None,
license=license,
python=python,
dependencies=requirements,
dev_dependencies=dev_requirements,
)
content = layout_.generate_poetry_content()
if self.io.is_interactive():
self.line("<info>Generated file</info>")
self.line("")
self.line(content)
self.line("")
if not self.confirm("Do you confirm generation?", True):
self.line("<error>Command aborted</error>")
return 1
with (Path.cwd() / "pyproject.toml").open("w", encoding="utf-8") as f:
f.write(content)
def _determine_requirements(
self, requires, allow_prereleases=False
): # type: (List[str], bool) -> List[Dict[str, str]]
if not requires:
requires = []
package = self.ask(
"Search for package to add (or leave blank to continue):"
)
while package is not None:
constraint = self._parse_requirements([package])[0]
if (
"git" in constraint
or "url" in constraint
or "path" in constraint
or "version" in constraint
):
self.line("Adding <info>{}</info>".format(package))
requires.append(constraint)
package = self.ask("\nAdd a package:")
continue
matches = self._get_pool().search(constraint["name"])
if not matches:
self.line("<error>Unable to find package</error>")
package = False
else:
choices = []
matches_names = [p.name for p in matches]
exact_match = constraint["name"] in matches_names
if exact_match:
choices.append(
matches[matches_names.index(constraint["name"])].pretty_name
)
for found_package in matches:
if len(choices) >= 10:
break
if found_package.name.lower() == constraint["name"].lower():
continue
choices.append(found_package.pretty_name)
self.line(
"Found <info>{}</info> packages matching <c1>{}</c1>".format(
len(matches), package
)
)
package = self.choice(
"\nEnter package # to add, or the complete package name if it is not listed",
choices,
attempts=3,
)
# no constraint yet, determine the best version automatically
if package is not False and "version" not in constraint:
question = self.create_question(
"Enter the version constraint to require "
"(or leave blank to use the latest version):"
)
question.attempts = 3
question.validator = lambda x: (x or "").strip() or False
package_constraint = self.ask(question)
if package_constraint is None:
_, package_constraint = self._find_best_version_for_package(
package
)
self.line(
"Using version <b>{}</b> for <c1>{}</c1>".format(
package_constraint, package
)
)
constraint["version"] = package_constraint
if package is not False:
requires.append(constraint)
package = self.ask("\nAdd a package:")
return requires
requires = self._parse_requirements(requires)
result = []
for requirement in requires:
if "git" in requirement or "url" in requirement or "path" in requirement:
result.append(requirement)
continue
elif "version" not in requirement:
# determine the best version automatically
name, version = self._find_best_version_for_package(
requirement["name"], allow_prereleases=allow_prereleases
)
requirement["version"] = version
requirement["name"] = name
self.line(
"Using version <b>{}</b> for <c1>{}</c1>".format(version, name)
)
else:
# check that the specified version/constraint exists
# before we proceed
name, _ = self._find_best_version_for_package(
requirement["name"],
requirement["version"],
allow_prereleases=allow_prereleases,
)
requirement["name"] = name
result.append(requirement)
return result
def _find_best_version_for_package(
self, name, required_version=None, allow_prereleases=False
): # type: (...) -> Tuple[str, str]
from poetry.version.version_selector import VersionSelector
selector = VersionSelector(self._get_pool())
package = selector.find_best_candidate(
name, required_version, allow_prereleases=allow_prereleases
)
if not package:
# TODO: find similar
raise ValueError(
"Could not find a matching version of package {}".format(name)
)
return package.pretty_name, selector.find_recommended_require_version(package)
def _parse_requirements(
self, requirements
): # type: (List[str]) -> List[Dict[str, str]]
from poetry.puzzle.provider import Provider
result = []
try:
cwd = self.poetry.file.parent
except RuntimeError:
cwd = Path.cwd()
for requirement in requirements:
requirement = requirement.strip()
extras = []
extras_m = re.search(r"\[([\w\d,-_]+)\]$", requirement)
if extras_m:
extras = [e.strip() for e in extras_m.group(1).split(",")]
requirement, _ = requirement.split("[")
url_parsed = urlparse.urlparse(requirement)
if url_parsed.scheme and url_parsed.netloc:
# Url
if url_parsed.scheme in ["git+https", "git+ssh"]:
from poetry.vcs.git import Git
from poetry.vcs.git import ParsedUrl
parsed = ParsedUrl.parse(requirement)
url = Git.normalize_url(requirement)
pair = OrderedDict([("name", parsed.name), ("git", url.url)])
if parsed.rev:
pair["rev"] = url.revision
if extras:
pair["extras"] = extras
package = Provider.get_package_from_vcs(
"git", url.url, reference=pair.get("rev")
)
pair["name"] = package.name
result.append(pair)
continue
elif url_parsed.scheme in ["http", "https"]:
package = Provider.get_package_from_url(requirement)
pair = OrderedDict(
[("name", package.name), ("url", package.source_url)]
)
if extras:
pair["extras"] = extras
result.append(pair)
continue
elif (os.path.sep in requirement or "/" in requirement) and cwd.joinpath(
requirement
).exists():
path = cwd.joinpath(requirement)
if path.is_file():
package = Provider.get_package_from_file(path.resolve())
else:
package = Provider.get_package_from_directory(path)
result.append(
OrderedDict(
[
("name", package.name),
("path", path.relative_to(cwd).as_posix()),
]
+ ([("extras", extras)] if extras else [])
)
)
continue
pair = re.sub(
"^([^@=: ]+)(?:@|==|(?<![<>~!])=|:| )(.*)$", "\\1 \\2", requirement
)
pair = pair.strip()
require = OrderedDict()
if " " in pair:
name, version = pair.split(" ", 2)
extras_m = re.search(r"\[([\w\d,-_]+)\]$", name)
if extras_m:
extras = [e.strip() for e in extras_m.group(1).split(",")]
name, _ = name.split("[")
require["name"] = name
if version != "latest":
require["version"] = version
else:
m = re.match(
r"^([^><=!: ]+)((?:>=|<=|>|<|!=|~=|~|\^).*)$", requirement.strip()
)
if m:
name, constraint = m.group(1), m.group(2)
extras_m = re.search(r"\[([\w\d,-_]+)\]$", name)
if extras_m:
extras = [e.strip() for e in extras_m.group(1).split(",")]
name, _ = name.split("[")
require["name"] = name
require["version"] = constraint
else:
extras_m = re.search(r"\[([\w\d,-_]+)\]$", pair)
if extras_m:
extras = [e.strip() for e in extras_m.group(1).split(",")]
pair, _ = pair.split("[")
require["name"] = pair
if extras:
require["extras"] = extras
result.append(require)
return result
def _format_requirements(
self, requirements
): # type: (List[Dict[str, str]]) -> Dict[str, Union[str, Dict[str, str]]]
requires = {}
for requirement in requirements:
name = requirement.pop("name")
if "version" in requirement and len(requirement) == 1:
constraint = requirement["version"]
else:
constraint = inline_table()
constraint.trivia.trail = "\n"
constraint.update(requirement)
requires[name] = constraint
return requires
def _validate_author(self, author, default):
from poetry.packages.package import AUTHOR_REGEX
author = author or default
if author in ["n", "no"]:
return
m = AUTHOR_REGEX.match(author)
if not m:
raise ValueError(
"Invalid author string. Must be in the format: "
"John Smith <john@example.com>"
)
return author
def _validate_license(self, license):
from poetry.spdx import license_by_id
if license:
license_by_id(license)
return license
def _get_pool(self):
from poetry.repositories import Pool
from poetry.repositories.pypi_repository import PyPiRepository
if isinstance(self, EnvCommand):
return self.poetry.pool
if self._pool is None:
self._pool = Pool()
self._pool.add_repository(PyPiRepository())
return self._pool