forked from davidlatwe/rez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
262 lines (206 loc) · 8.18 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
"""
This script uses an embedded copy of virtualenv to create a standalone,
production-ready Rez installation in the specified directory.
"""
from __future__ import print_function
import argparse
import os
import sys
import shutil
import os.path
import subprocess
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
from rez.cli._entry_points import get_specifications
from rez.backport.shutilwhich import which
from rez.vendor.distlib.scripts import ScriptMaker
from build_utils.virtualenv.virtualenv import create_environment, path_locations
def get_py_venv_executable(dest_dir):
# get virtualenv's python executable
_, _, _, venv_bin_dir = path_locations(dest_dir)
env = {
"PATH": venv_bin_dir,
"PATHEXT": os.environ.get("PATHEXT", "")
}
return venv_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):
venv_bin_path, py_executable = get_py_venv_executable(dest_dir)
specs = get_specifications()
# delete rez bin files written into virtualenv
for name in specs.keys():
filepath = os.path.join(venv_bin_path, name)
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 venv's 'python'.
dest_bin_path = os.path.join(venv_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, dirs, _ in os.walk(path):
if os.path.basename(root) == "completion":
completion_path = root
break
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):
"""Install rez into the given directory.
Args:
dest_dir (str): Full path to the install directory.
"""
print("installing rez to %s..." % dest_dir)
# create the virtualenv
create_environment(dest_dir)
# install rez from source
install_rez_from_source(dest_dir)
# patch the rez binaries
patch_rez_binaries(dest_dir)
# copy completion scripts into venv
completion_path = copy_completion_scripts(dest_dir)
# mark venv as production rez install. Do not remove - rez uses this!
_, _, _, venv_bin_dir = path_locations(dest_dir)
dest_bin_dir = os.path.join(venv_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])"
])
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)
ext = "csh" if "csh" in shell else "sh" # Basic selection logic
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):
_, py_executable = get_py_venv_executable(dest_dir)
# install via pip
run_command([py_executable, "-m", "pip", "install", "."])
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 (venv-based) rez install
tmpdir = mkdtemp(prefix="rez-install-")
install(tmpdir)
try:
# This extracts a rez package from the installation. See
# rez.utils.installer.install_as_rez_package for more details.
#
args = (
os.path.join(tmpdir, "bin", "python"), "-E", "-c",
r"from rez.utils.installer import install_as_rez_package;"
r"install_as_rez_package('%s')" % 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(
"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)