forked from AcademySoftwareFoundation/rez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
326 lines (261 loc) · 10.1 KB
/
install.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
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the Rez Project
"""
This script uses venv/virtualenv to create a standalone, production-ready Rez
installation in the specified directory.
"""
import argparse
import os
import platform
import sys
import shutil
import subprocess
USE_VIRTUALENV = False
try:
import venv
except ImportError:
USE_VIRTUALENV = True
import virtualenv
source_path = os.path.dirname(os.path.realpath(__file__))
src_path = os.path.join(source_path, "src")
sys.path.insert(0, src_path)
# Note: The following imports are carefully selected, they will work even
# though rez is not yet built.
#
from rez.utils._version import _rez_version # noqa: E402
from rez.utils.which import which # noqa: E402
from rez.cli._entry_points import get_specifications # noqa: E402
from rez.vendor.distlib.scripts import ScriptMaker # noqa: E402
def create_virtual_environment(dest_dir: str) -> None:
"""Create a virtual environment in the given directory.
Args:
dest_dir (str): Full path to the virtual environment directory.
"""
if USE_VIRTUALENV:
try:
subprocess.run(
[sys.executable, "-m", "virtualenv", dest_dir],
check=True
)
except subprocess.CalledProcessError as err:
print(f"Failed to create virtual environment: {err}")
sys.exit(1)
else:
builder = venv.EnvBuilder(with_pip=True)
builder.create(dest_dir)
def get_virtualenv_bin_dir(dest_dir: str) -> str:
"""Get the bin directory of the virtual environment.
Args:
dest_dir (str): The directory of the virtual environment.
"""
bin_dir = "Scripts" if platform.system() == "Windows" else "bin"
return os.path.join(dest_dir, bin_dir)
def get_virtualenv_py_executable(dest_dir):
# get virtualenv's python executable
bin_dir = get_virtualenv_bin_dir(dest_dir)
env = {
"PATH": bin_dir,
"PATHEXT": os.environ.get("PATHEXT", "")
}
return bin_dir, which("python", env=env)
def run_command(args, cwd=source_path):
if opts.verbose:
print("running in %s: %s" % (cwd, " ".join(args)))
return subprocess.check_output(args, cwd=source_path)
def patch_rez_binaries(dest_dir):
virtualenv_bin_path, py_executable = get_virtualenv_py_executable(dest_dir)
specs = get_specifications()
# delete rez bin files written into virtualenv
for name in specs.keys():
basepath = os.path.join(virtualenv_bin_path, name)
filepaths = [
basepath,
basepath + "-script.py",
basepath + ".exe"
]
for filepath in filepaths:
if os.path.isfile(filepath):
os.remove(filepath)
# write patched bins instead. These go into 'bin/rez' subdirectory, which
# gives us a bin dir containing only rez binaries. This is what we want -
# we don't want resolved envs accidentally getting the virtualenv's 'python'.
dest_bin_path = os.path.join(virtualenv_bin_path, "rez")
if os.path.exists(dest_bin_path):
shutil.rmtree(dest_bin_path)
os.makedirs(dest_bin_path)
maker = ScriptMaker(
# note: no filenames are referenced in any specifications, so
# source_dir is unused
source_dir=None,
target_dir=dest_bin_path
)
maker.executable = py_executable
maker.make_multiple(
specifications=specs.values(),
# the -E arg is crucial - it means rez cli tools still work within a
# rez-resolved env, even if PYTHONPATH or related env-vars would have
# otherwise changed rez's behaviour
options=dict(interpreter_args=["-E"])
)
def copy_completion_scripts(dest_dir):
# find completion dir in rez package
path = os.path.join(dest_dir, "lib")
completion_path = None
for root, _, _ in os.walk(path):
if root.endswith(os.path.sep + "rez" + os.path.sep + "completion"):
completion_path = root
break
# copy completion scripts into root of virtualenv for ease of use
if completion_path:
dest_path = os.path.join(dest_dir, "completion")
if os.path.exists(dest_path):
shutil.rmtree(dest_path)
shutil.copytree(completion_path, dest_path)
return dest_path
return None
def install(dest_dir, print_welcome=False, editable=False):
"""Install rez into the given directory.
Args:
dest_dir (str): Full path to the install directory.
"""
print("installing rez%s to %s..." % (" (editable mode)" if editable else "", dest_dir))
# create the virtualenv
create_virtual_environment(dest_dir)
# install rez from source
install_rez_from_source(dest_dir, editable=editable)
# patch the rez binaries
patch_rez_binaries(dest_dir)
# copy completion scripts into virtualenv
completion_path = copy_completion_scripts(dest_dir)
# mark virtualenv as production rez install. Do not remove - rez uses this!
virtualenv_bin_dir = get_virtualenv_bin_dir(dest_dir)
dest_bin_dir = os.path.join(virtualenv_bin_dir, "rez")
validation_file = os.path.join(dest_bin_dir, ".rez_production_install")
with open(validation_file, 'w') as f:
f.write(_rez_version)
# done
if print_welcome:
print()
print("SUCCESS!")
rez_exe = os.path.realpath(os.path.join(dest_bin_dir, "rez"))
print("Rez executable installed to: %s" % rez_exe)
try:
out = subprocess.check_output(
[
rez_exe,
"python",
"-c",
"import rez; print(rez.__path__[0])"
],
universal_newlines=True
)
pkg_path = os.path.realpath(out.strip())
print("Rez python package installed to: %s" % pkg_path)
except:
pass # just in case there's an issue with rez-python tool
print()
print("To activate Rez, add the following path to $PATH:")
print(dest_bin_dir)
if completion_path:
print('')
shell = os.getenv('SHELL')
if shell:
shell = os.path.basename(shell)
if "csh" in shell: # csh and tcsh
ext = "csh"
elif "zsh" in shell:
ext = "zsh"
else:
ext = "sh"
print("You may also want to source the completion script (for %s):" % shell)
print("source {0}/complete.{1}".format(completion_path, ext))
else:
print("You may also want to source the relevant completion script from:")
print(completion_path)
print('')
def install_rez_from_source(dest_dir, editable):
_, py_executable = get_virtualenv_py_executable(dest_dir)
# install via pip
args = [py_executable, "-m", "pip", "install"]
if editable:
args.append("-e")
args.append(".")
run_command(args)
def install_as_rez_package(repo_path):
"""Installs rez as a rez package.
Note that this can be used to install new variants of rez into an existing
rez package (you may require multiple platform installations for example).
Args:
repo_path (str): Full path to the package repository to install into.
"""
from tempfile import mkdtemp
# do a temp production (virtualenv-based) rez install
tmpdir = mkdtemp(prefix="rez-install-")
install(tmpdir)
_, py_executable = get_virtualenv_py_executable(tmpdir)
try:
# This extracts a rez package from the installation. See
# rez.utils.installer.install_as_rez_package for more details.
#
args = (
py_executable, "-E", "-c",
r"from rez.utils.installer import install_as_rez_package;"
r"install_as_rez_package(%r)" % repo_path
)
print(subprocess.check_output(args))
finally:
# cleanup temp install
try:
shutil.rmtree(tmpdir)
except:
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(
"Rez installer", description="Install rez in a production ready, "
"standalone Python virtual environment.")
parser.add_argument(
'-v', '--verbose', action='count', dest='verbose', default=0,
help="Increase verbosity.")
parser.add_argument(
'-s', '--keep-symlinks', action="store_true", default=False,
help="Don't run realpath on the passed DIR to resolve symlinks; "
"ie, the baked script locations may still contain symlinks")
parser.add_argument(
'-p', '--as-rez-package', action="store_true",
help="Install rez as a rez package. Note that this installs the API "
"only (no cli tools), and DIR is expected to be the path to a rez "
"package repository (and will default to ~/packages instead).")
parser.add_argument(
"-e", "--editable", action="store_true",
help="Make the install an editable install (pip install -e). This should "
"only be used for development purposes"
)
parser.add_argument(
"DIR", nargs='?',
help="Destination directory. If '{version}' is present, it will be "
"expanded to the rez version. Default: /opt/rez")
opts = parser.parse_args()
if " " in os.path.realpath(__file__):
parser.error(
"\nThe absolute path of install.py cannot contain spaces due to setuptools limitation.\n"
"Please move installation files to another location or rename offending folder(s).\n"
)
# determine install path
if opts.DIR:
path = opts.DIR
elif opts.as_rez_package:
path = "~/packages"
else:
path = "/opt/rez"
if opts.as_rez_package:
dest_dir = path
else:
dest_dir = path.format(version=_rez_version)
dest_dir = os.path.expanduser(dest_dir)
if not opts.keep_symlinks:
dest_dir = os.path.realpath(dest_dir)
# perform the installation
if opts.as_rez_package:
install_as_rez_package(dest_dir)
else:
install(dest_dir, print_welcome=True, editable=opts.editable)