forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
noxfile.py
160 lines (137 loc) · 4.84 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
# 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 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)