forked from AcademySoftwareFoundation/rez
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.py
171 lines (138 loc) · 5.69 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
"""
This script uses an embedded copy of virtualenv to create a standalone,
production-ready Rez installation in the specified directory.
"""
import os
import sys
import shutil
import os.path
import textwrap
import subprocess
from optparse import OptionParser
source_path = os.path.dirname(os.path.realpath(__file__))
bin_path = os.path.join(source_path, "bin")
src_path = os.path.join(source_path, "src")
sys.path.insert(0, src_path)
from rez.utils._version import _rez_version
from rez.backport.shutilwhich import which
from build_utils.virtualenv.virtualenv import Logger, create_environment, \
path_locations
from build_utils.distlib.scripts import ScriptMaker
class fake_entry(object):
code_template = textwrap.dedent(
"""
from rez.cli.{module} import run
run({target})
""").strip() + '\n'
def __init__(self, name):
self.name = name
def get_script_text(self):
module = "_main"
target = ""
if self.name == "bez":
module = "_bez"
elif self.name == "_rez_fwd": # TODO rename this binary
target = "'forward'"
elif self.name not in ("rez", "rezolve"):
target = "'%s'" % self.name.split('-', 1)[-1]
return self.code_template.format(module=module, target=target)
class _ScriptMaker(ScriptMaker):
def __init__(self, *nargs, **kwargs):
super(_ScriptMaker, self).__init__(*nargs, **kwargs)
self.variants = set(('',))
def _get_script_text(self, entry):
return entry.get_script_text()
def patch_rez_binaries(dest_dir):
bin_names = os.listdir(bin_path)
_, _, _, venv_bin_path = path_locations(dest_dir)
venv_py_executable = which("python", env={"PATH":venv_bin_path,
"PATHEXT":os.environ.get("PATHEXT", "")})
# delete rez bin files written by setuptools
for name in bin_names:
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(bin_path, dest_bin_path)
maker.executable = venv_py_executable
options = dict(interpreter_args=["-E"])
for name in bin_names:
entry = fake_entry(name)
maker._make_script(entry, [], options=options)
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
if __name__ == "__main__":
usage = ("usage: %prog [options] DEST_DIR ('{version}' in DEST_DIR will "
"expand to Rez version)")
parser = OptionParser(usage=usage)
parser.add_option(
'-v', '--verbose', action='count', dest='verbose', default=0,
help="Increase verbosity.")
parser.add_option(
'-s', '--keep-symlinks', action="store_true", default=False,
help="Don't run realpath on the passed DEST_DIR to resolve symlinks; "
"ie, the baked script locations may still contain symlinks")
opts, args = parser.parse_args()
if " " in os.path.realpath(__file__):
err_str = "\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"
parser.error(err_str)
# determine install path
if len(args) != 1:
parser.error("expected DEST_DIR")
dest_dir = args[0].format(version=_rez_version)
dest_dir = os.path.expanduser(dest_dir)
if not opts.keep_symlinks:
dest_dir = os.path.realpath(dest_dir)
print "installing rez to %s..." % dest_dir
# make virtualenv verbose
log_level = Logger.level_for_integer(2 - opts.verbose)
logger = Logger([(log_level, sys.stdout)])
# create the virtualenv
create_environment(dest_dir)
# install rez from source
_, _, _, venv_bin_dir = path_locations(dest_dir)
py_executable = which("python", env={"PATH":venv_bin_dir,
"PATHEXT":os.environ.get("PATHEXT",
"")})
args = [py_executable, "setup.py", "install"]
if opts.verbose:
print "running in %s: %s" % (source_path, " ".join(args))
p = subprocess.Popen(args, cwd=source_path)
p.wait()
# 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!
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
print
print "SUCCESS! To activate Rez, add the following path to $PATH:"
print dest_bin_dir
if completion_path:
print "You may also want to source the relevant completion script from:"
print completion_path
print