forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_venv.py
More file actions
167 lines (129 loc) · 4.61 KB
/
create_venv.py
File metadata and controls
167 lines (129 loc) · 4.61 KB
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import argparse
import importlib.util as import_util
import os
import pathlib
import subprocess
import sys
from typing import List, Optional, Sequence, Union
VENV_NAME = ".venv"
CWD = pathlib.PurePath(os.getcwd())
class VenvError(Exception):
pass
def parse_args(argv: Sequence[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"--requirements",
action="append",
default=[],
help="Install additional dependencies into the virtual environment.",
)
parser.add_argument(
"--toml",
action="store",
default=None,
help="Install additional dependencies from sources like `pyproject.toml` into the virtual environment.",
)
parser.add_argument(
"--extras",
action="append",
default=[],
help="Install specific package groups from `pyproject.toml` into the virtual environment.",
)
parser.add_argument(
"--git-ignore",
action="store_true",
default=False,
help="Add .gitignore to the newly created virtual environment.",
)
parser.add_argument(
"--name",
default=VENV_NAME,
type=str,
help="Name of the virtual environment.",
metavar="NAME",
action="store",
)
return parser.parse_args(argv)
def is_installed(module: str) -> bool:
return import_util.find_spec(module) is not None
def file_exists(path: Union[str, pathlib.PurePath]) -> bool:
return os.path.exists(path)
def venv_exists(name: str) -> bool:
return os.path.exists(CWD / name) and file_exists(get_venv_path(name))
def run_process(args: Sequence[str], error_message: str) -> None:
try:
print("Running: " + " ".join(args))
subprocess.run(args, cwd=os.getcwd(), check=True)
except subprocess.CalledProcessError:
raise VenvError(error_message)
def get_venv_path(name: str) -> str:
# See `venv` doc here for more details on binary location:
# https://docs.python.org/3/library/venv.html#creating-virtual-environments
if sys.platform == "win32":
return os.fspath(CWD / name / "Scripts" / "python.exe")
else:
return os.fspath(CWD / name / "bin" / "python")
def install_requirements(venv_path: str, requirements: List[str]) -> None:
if not requirements:
return
print(f"VENV_INSTALLING_REQUIREMENTS: {requirements}")
args = []
for requirement in requirements:
args += ["-r", requirement]
run_process(
[venv_path, "-m", "pip", "install"] + args,
"CREATE_VENV.PIP_FAILED_INSTALL_REQUIREMENTS",
)
print("CREATE_VENV.PIP_INSTALLED_REQUIREMENTS")
def install_toml(venv_path: str, extras: List[str]) -> None:
args = "." if len(extras) == 0 else f".[{','.join(extras)}]"
run_process(
[venv_path, "-m", "pip", "install", "-e", args],
"CREATE_VENV.PIP_FAILED_INSTALL_PYPROJECT",
)
print("CREATE_VENV.PIP_INSTALLED_PYPROJECT")
def upgrade_pip(venv_path: str) -> None:
run_process(
[venv_path, "-m", "pip", "install", "--upgrade", "pip"],
"CREATE_VENV.PIP_UPGRADE_FAILED",
)
def add_gitignore(name: str) -> None:
git_ignore = CWD / name / ".gitignore"
if not file_exists(git_ignore):
print("Creating: " + os.fspath(git_ignore))
with open(git_ignore, "w") as f:
f.write("*")
def main(argv: Optional[Sequence[str]] = None) -> None:
if argv is None:
argv = []
args = parse_args(argv)
if not is_installed("venv"):
raise VenvError("CREATE_VENV.VENV_NOT_FOUND")
pip_installed = is_installed("pip")
deps_needed = args.requirements or args.extras or args.toml
if deps_needed and not pip_installed:
raise VenvError("CREATE_VENV.PIP_NOT_FOUND")
if venv_exists(args.name):
venv_path = get_venv_path(args.name)
print(f"EXISTING_VENV:{venv_path}")
else:
run_process(
[sys.executable, "-m", "venv", args.name],
"CREATE_VENV.VENV_FAILED_CREATION",
)
venv_path = get_venv_path(args.name)
print(f"CREATED_VENV:{venv_path}")
if args.git_ignore:
add_gitignore(args.name)
if pip_installed:
upgrade_pip(venv_path)
if args.toml:
print(f"VENV_INSTALLING_PYPROJECT: {args.toml}")
install_toml(venv_path, args.extras)
if args.requirements:
print(f"VENV_INSTALLING_REQUIREMENTS: {args.requirements}")
install_requirements(venv_path, args.requirements)
if __name__ == "__main__":
main(sys.argv[1:])