forked from getavalon/setup
-
Notifications
You must be signed in to change notification settings - Fork 2
/
avalon.py
314 lines (243 loc) · 9.14 KB
/
avalon.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
"""Avalon Command-line Interface
This module contains a CLI towards Avalon and all of what
is bundled together in this distribution.
- https://github.com/getavalon/setup
dependencies:
- Python 2.6+ or 3.6+
- PyQt5
example:
$ python avalon.py --help
overrides:
avalon.py takes into account dependencies bundled
together with this distribution, but these can be
overridden via environment variables.
Set any of the below to override which path to put
on your PYTHONPATH
# Database
- AVALON_MONGO=mongodb://localhost:27017
- AVALON_DB=avalon
# Dependencies
- PYBLISH_BASE=absolute/path
- PYBLISH_QML=absolute/path
- AVALON_CORE=absolute/path
- AVALON_LAUNCHER=absolute/path
- AVALON_EXAMPLES=absolute/path
# Enable additional output
- AVALON_DEBUG=True
"""
import os
import sys
import shutil
import tempfile
import platform
import contextlib
import subprocess
# Having avalon.py in the current working directory
# exposes it to Python's import mechanism which conflicts
# with the actual avalon Python package.
if os.path.basename(__file__) in os.listdir(os.getcwd()):
sys.stderr.write("Error: Please change your current "
"working directory\n%s\n" % os.getcwd())
sys.exit(1)
REPO_DIR = os.path.dirname(os.path.abspath(__file__))
AVALON_DEBUG = bool(os.getenv("AVALON_DEBUG"))
init = """\
from avalon import api, shell
api.install(shell)
"""
@contextlib.contextmanager
def install():
tempdir = tempfile.mkdtemp()
usercustomize = os.path.join(tempdir, "usercustomize.py")
with open(usercustomize, "w") as f:
f.write(init)
os.environ["PYTHONVERBOSE"] = "True"
os.environ["PYTHONPATH"] = os.pathsep.join([
tempdir, os.environ["PYTHONPATH"]
])
try:
yield
finally:
shutil.rmtree(tempdir)
def _install(root=None):
missing_dependencies = list()
for dependency in ("PyQt5",):
try:
__import__(dependency)
except ImportError:
missing_dependencies.append(dependency)
if missing_dependencies:
print("Sorry, there are some dependencies missing from your system.\n")
print("\n".join(" - %s" % d for d in missing_dependencies) + "\n")
print("See https://getavalon.github.io/2.0/howto/#install "
"for more details.")
sys.exit(1)
# Enable overriding from local environment
for dependency, name in (("PYBLISH_BASE", "pyblish-base"),
("PYBLISH_QML", "pyblish-qml"),
("AVALON_CORE", "avalon-core"),
("AVALON_LAUNCHER", "avalon-launcher"),
("AVALON_EXAMPLES", "avalon-examples")):
if dependency not in os.environ:
os.environ[dependency] = os.path.join(REPO_DIR, "git", name)
os.environ["PATH"] = os.pathsep.join([
# Expose "avalon", overriding existing
os.path.join(REPO_DIR),
os.environ["PATH"],
# Add generic binaries
os.path.join(REPO_DIR, "bin"),
# Add OS-level dependencies
os.path.join(REPO_DIR, "bin", platform.system().lower()),
])
os.environ["PYTHONPATH"] = os.pathsep.join(
# Append to PYTHONPATH
os.getenv("PYTHONPATH", "").split(os.pathsep) + [
# Third-party dependencies for Avalon
os.path.join(REPO_DIR, "bin", "pythonpath"),
# Default config and dependency
os.getenv("PYBLISH_BASE"),
os.getenv("PYBLISH_QML"),
# The Launcher itself
os.getenv("AVALON_LAUNCHER"),
os.getenv("AVALON_CORE"),
]
)
# Override default configuration by setting this value.
if "AVALON_CONFIG" not in os.environ:
os.environ["AVALON_CONFIG"] = "polly"
os.environ["PYTHONPATH"] += os.pathsep + os.path.join(
REPO_DIR, "git", "mindbender-config")
if root is not None:
os.environ["AVALON_PROJECTS"] = root
else:
try:
root = os.environ["AVALON_PROJECTS"]
except KeyError:
root = os.path.join(os.environ["AVALON_EXAMPLES"], "projects")
os.environ["AVALON_PROJECTS"] = root
try:
config = os.environ["AVALON_CONFIG"]
except KeyError:
config = "polly"
os.environ["AVALON_CONFIG"] = config
if subprocess.call([sys.executable, "-c", "import %s" % config]) != 0:
print("ERROR: config not found, check your PYTHONPATH.")
sys.exit(1)
def forward(args, silent=False, cwd=None):
"""Pass `args` to the Avalon CLI, within the Avalon Setup environment
Arguments:
args (list): Command-line arguments to run
within the active environment
"""
if AVALON_DEBUG:
print("avalon.py: Forwarding '%s'.." % " ".join(args))
popen = subprocess.Popen(
args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
bufsize=1,
cwd=cwd
)
# Blocks until finished
while True:
line = popen.stdout.readline()
if line != '':
if not silent or AVALON_DEBUG:
sys.stdout.write(line)
else:
break
if AVALON_DEBUG:
print("avalon.py: Finishing up..")
popen.wait()
return popen.returncode
def update(cd):
"""Update Avalon to the latest version"""
script = (
# Discard any ad-hoc changes
("Resetting..", ["git", "reset", "--hard"]),
("Downloading..", ["git", "pull", "origin", "master"]),
# In case there are new submodules since last pull
("Looking for submodules..", ["git", "submodule", "init"]),
("Updating submodules..",
["git", "submodule", "update", "--recursive"]),
)
for message, args in script:
print(message)
returncode = forward(args, silent=True, cwd=cd)
if returncode != 0:
sys.stderr.write("Could not update, try running "
"it again with AVALON_DEBUG=True\n")
return returncode
print("All done")
def main():
import argparse
parser = argparse.ArgumentParser(usage=__doc__)
parser.add_argument("--root", help="Projects directory")
parser.add_argument("--import", dest="import_", action="store_true",
help="Import an example project into the database")
parser.add_argument("--export", action="store_true",
help="Export a project from the database")
parser.add_argument("--build", action="store_true",
help="Build one of the bundled example projects")
parser.add_argument("--update", action="store_true",
help="Update Avalon Setup to the latest version")
parser.add_argument("--init", action="store_true",
help="Establish a new project in the "
"current working directory")
parser.add_argument("--load", action="store_true",
help="Load project at the current working directory")
parser.add_argument("--save", action="store_true",
help="Save project from the current working directory")
parser.add_argument("--forward",
help="Run arbitrary command from setup environment")
parser.add_argument("--publish", action="store_true",
help="Publish from current working directory, "
"or supplied --root")
kwargs, args = parser.parse_known_args()
_install(root=kwargs.root)
cd = os.path.dirname(os.path.abspath(__file__))
examplesdir = os.getenv("AVALON_EXAMPLES",
os.path.join(cd, "git", "avalon-examples"))
if kwargs.import_:
fname = os.path.join(examplesdir, "import.py")
returncode = forward(
[sys.executable, "-u", fname] + args)
elif kwargs.export:
fname = os.path.join(examplesdir, "export.py")
returncode = forward(
[sys.executable, "-u", fname] + args)
elif kwargs.build:
fname = os.path.join(examplesdir, "build.py")
returncode = forward(
[sys.executable, "-u", fname] + args)
elif kwargs.init:
returncode = forward([
sys.executable, "-u", "-m",
"avalon.inventory", "--init"])
elif kwargs.load:
returncode = forward([
sys.executable, "-u", "-m",
"avalon.inventory", "--load"])
elif kwargs.save:
returncode = forward([
sys.executable, "-u", "-m",
"avalon.inventory", "--save"])
elif kwargs.update:
returncode = update(cd)
elif kwargs.forward:
returncode = forward(kwargs.forward.split())
elif kwargs.publish:
os.environ["PYBLISH_HOSTS"] = "shell"
with install():
returncode = forward([
sys.executable, "-u", "-m", "pyblish", "gui"
] + args, silent=True)
else:
root = os.environ["AVALON_PROJECTS"]
returncode = forward([
sys.executable, "-u", "-m", "launcher", "--root", root
] + args)
sys.exit(returncode)
if __name__ == '__main__':
main()