forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
232 lines (198 loc) · 7.55 KB
/
noxfile.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os
import pathlib
import nox
import shutil
import sys
import sysconfig
import uuid
EXT_ROOT = pathlib.Path(__file__).parent
def delete_dir(path: pathlib.Path, ignore_errors=None):
attempt = 0
known = []
while attempt < 5:
try:
shutil.rmtree(os.fspath(path), ignore_errors=ignore_errors)
return
except PermissionError as pe:
if os.fspath(pe.filename) in known:
break
print(f"Changing permissions on {pe.filename}")
os.chmod(pe.filename, 0o666)
shutil.rmtree(os.fspath(path))
@nox.session()
def install_python_libs(session: nox.Session):
requirements = [
("./python_files/lib/python", "./requirements.txt"),
(
"./python_files/lib/jedilsp",
"./python_files/jedilsp_requirements/requirements.txt",
),
]
for target, file in requirements:
session.install(
"-t",
target,
"--no-cache-dir",
"--implementation",
"py",
"--no-deps",
"--require-hashes",
"--only-binary",
":all:",
"-r",
file,
)
session.install("packaging")
# Download get-pip script
session.run(
"python",
"./python_files/download_get_pip.py",
env={"PYTHONPATH": "./python_files/lib/temp"},
)
if pathlib.Path("./python_files/lib/temp").exists():
shutil.rmtree("./python_files/lib/temp")
@nox.session()
def azure_pet_checkout(session: nox.Session):
branch = os.getenv("PYTHON_ENV_TOOLS_REF", "main")
# dest dir should be <vscode-python repo root>/python-env-tools
dest_dir = (pathlib.Path(os.getenv("PYTHON_ENV_TOOLS_DEST")) / "python-env-tools").resolve()
# temp dir should be <agent temp dir>
temp_dir = (pathlib.Path(os.getenv("PYTHON_ENV_TOOLS_TEMP")) / "python-env-tools").resolve()
session.log(f"Cloning python-environment-tools to {temp_dir}")
temp_dir.mkdir(0o766, parents=True, exist_ok=True)
try:
with session.cd(temp_dir):
session.run("git", "init", external=True)
session.run(
"git",
"remote",
"add",
"origin",
"https://github.com/microsoft/python-environment-tools",
external=True,
)
session.run("git", "fetch", "origin", branch, external=True)
session.run(
"git", "checkout", "--force", "-B", branch, f"origin/{branch}", external=True
)
delete_dir(temp_dir / ".git")
delete_dir(temp_dir / ".github")
delete_dir(temp_dir / ".vscode")
(temp_dir / "CODE_OF_CONDUCT.md").unlink()
shutil.move(os.fspath(temp_dir), os.fspath(dest_dir))
except PermissionError as e:
print(f"Permission error: {e}")
if not dest_dir.exists():
raise
finally:
delete_dir(temp_dir, ignore_errors=True)
@nox.session()
def azure_pet_build_before(session: nox.Session):
source_dir = pathlib.Path(pathlib.Path.cwd() / "python-env-tools").resolve()
config_toml_disabled = source_dir / ".cargo" / "config.toml.disabled"
config_toml = source_dir / ".cargo" / "config.toml"
if config_toml_disabled.exists() and not config_toml.exists():
config_toml.write_bytes(config_toml_disabled.read_bytes())
@nox.session()
def azure_pet_build_after(session: nox.Session):
source_dir = pathlib.Path(pathlib.Path.cwd() / "python-env-tools").resolve()
ext = sysconfig.get_config_var("EXE") or ""
bin_name = f"pet{ext}"
abs_bin_path = None
for root, _, files in os.walk(os.fspath(source_dir / "target")):
bin_path = pathlib.Path(root) / "release" / bin_name
if bin_path.exists():
abs_bin_path = bin_path.absolute()
break
assert abs_bin_path
dest_dir = pathlib.Path(pathlib.Path.cwd() / "python-env-tools").resolve()
if not pathlib.Path(dest_dir / "bin").exists():
pathlib.Path(dest_dir / "bin").mkdir()
bin_dest = dest_dir / "bin" / bin_name
shutil.copyfile(abs_bin_path, bin_dest)
if sys.platform != "win32":
os.chmod(os.fspath(bin_dest), 0o755)
@nox.session()
def native_build(session: nox.Session):
source_dir = pathlib.Path(pathlib.Path.cwd() / "python-env-tools").resolve()
dest_dir = pathlib.Path(pathlib.Path.cwd() / "python-env-tools").resolve()
with session.cd(source_dir):
if not pathlib.Path(dest_dir / "bin").exists():
pathlib.Path(dest_dir / "bin").mkdir()
if not pathlib.Path(dest_dir / "bin" / ".gitignore").exists():
pathlib.Path(dest_dir / "bin" / ".gitignore").write_text(
"*\n", encoding="utf-8"
)
ext = sysconfig.get_config_var("EXE") or ""
target = os.environ.get("CARGO_TARGET", None)
session.run("cargo", "fetch", external=True)
if target:
session.run(
"cargo",
"build",
"--frozen",
"--release",
"--target",
target,
external=True,
)
source = source_dir / "target" / target / "release" / f"pet{ext}"
else:
session.run(
"cargo",
"build",
"--frozen",
"--release",
external=True,
)
source = source_dir / "target" / "release" / f"pet{ext}"
dest = dest_dir / "bin" / f"pet{ext}"
shutil.copy(source, dest)
# Remove python-env-tools/bin exclusion from .vscodeignore
vscode_ignore = EXT_ROOT / ".vscodeignore"
remove_patterns = ("python-env-tools/bin/**",)
lines = vscode_ignore.read_text(encoding="utf-8").splitlines()
filtered_lines = [line for line in lines if not line.startswith(remove_patterns)]
vscode_ignore.write_text("\n".join(filtered_lines) + "\n", encoding="utf-8")
@nox.session()
def checkout_native(session: nox.Session):
dest = (pathlib.Path.cwd() / "python-env-tools").resolve()
if dest.exists():
shutil.rmtree(os.fspath(dest))
temp_dir = os.getenv("TEMP") or os.getenv("TMP") or "/tmp"
temp_dir = pathlib.Path(temp_dir) / str(uuid.uuid4()) / "python-env-tools"
temp_dir.mkdir(0o766, parents=True)
session.log(f"Cloning python-environment-tools to {temp_dir}")
try:
with session.cd(temp_dir):
session.run("git", "init", external=True)
session.run(
"git",
"remote",
"add",
"origin",
"https://github.com/microsoft/python-environment-tools",
external=True,
)
session.run("git", "fetch", "origin", "main", external=True)
session.run(
"git", "checkout", "--force", "-B", "main", "origin/main", external=True
)
delete_dir(temp_dir / ".git")
delete_dir(temp_dir / ".github")
delete_dir(temp_dir / ".vscode")
(temp_dir / "CODE_OF_CONDUCT.md").unlink()
shutil.move(os.fspath(temp_dir), os.fspath(dest))
except PermissionError as e:
print(f"Permission error: {e}")
if not dest.exists():
raise
finally:
delete_dir(temp_dir.parent, ignore_errors=True)
@nox.session()
def setup_repo(session: nox.Session):
install_python_libs(session)
checkout_native(session)
native_build(session)