forked from python-poetry/poetry
-
Notifications
You must be signed in to change notification settings - Fork 2
/
new.py
96 lines (73 loc) · 2.73 KB
/
new.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
from __future__ import annotations
import sys
from contextlib import suppress
from cleo.helpers import argument
from cleo.helpers import option
from poetry.console.commands.command import Command
class NewCommand(Command):
name = "new"
description = "Creates a new Python project at <path>."
arguments = [argument("path", "The path to create the project at.")]
options = [
option("name", None, "Set the resulting package name.", flag=False),
option("src", None, "Use the src layout for the project."),
option(
"readme",
None,
"Specify the readme file format. One of md (default) or rst",
flag=False,
),
]
def handle(self) -> int:
from pathlib import Path
from poetry.core.vcs.git import GitConfig
from poetry.layouts import layout
from poetry.utils.env import SystemEnv
if self.io.input.option("directory"):
self.line_error(
"<warning>--directory only makes sense with existing projects, and will"
" be ignored. You should consider the option --path instead.</warning>"
)
if self.option("src"):
layout_cls = layout("src")
else:
layout_cls = layout("standard")
path = Path(self.argument("path"))
if not path.is_absolute():
# we do not use resolve here due to compatibility issues
# for path.resolve(strict=False)
path = Path.cwd().joinpath(path)
name = self.option("name")
if not name:
name = path.name
if path.exists() and list(path.glob("*")):
# Directory is not empty. Aborting.
raise RuntimeError(
f"Destination <fg=yellow>{path}</> exists and is not empty"
)
readme_format = self.option("readme") or "md"
config = GitConfig()
author = None
if config.get("user.name"):
author = config["user.name"]
author_email = config.get("user.email")
if author_email:
author += f" <{author_email}>"
current_env = SystemEnv(Path(sys.executable))
default_python = "^" + ".".join(str(v) for v in current_env.version_info[:2])
layout_ = layout_cls(
name,
"0.1.0",
author=author,
readme_format=readme_format,
python=default_python,
)
layout_.create(path)
path = path.resolve()
with suppress(ValueError):
path = path.relative_to(Path.cwd())
self.line(
f"Created package <info>{layout_._package_name}</> in"
f" <fg=blue>{path.as_posix()}</>"
)
return 0