forked from pyblish/pyblish-win
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_install.py
273 lines (222 loc) · 7.56 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
"""Pyblish Installation Program
Usage:
$ install --help
"""
import os
import sys
import time
import argparse
import datetime
import threading
import subprocess
from itertools import cycle
header = """
___________________
\ /
\ /
\ /
\ /
\ /
|
|
|
|
|
Pyblish Installation
This installation requires Python and Git
and will occupy at least 400 mb of disk space,
whereof ~300 mb will be downloaded.
The installation typically takes around 10-15
minutes to complete.
"""
def main(globally, verbose, mock, mock_error, accept_defaults):
"""Primary installation procedure
Arguments:
globally (bool): Write permanent environment variables
verbose (bool): Print a lot of output
mock (bool): Fake the download
mock_error (bool): Fake an error
accept_defaults (bool): Do not require input
"""
if not proceed(accept_defaults):
sys.exit(1)
if not has_requirements():
sys.exit(2)
print("")
print("Downloading, this may take a while..")
# Start animation
if not verbose:
data = {"finished": False}
t = threading.Thread(target=animation, args=[data])
t.daemon = True
t.start()
if mock:
popen = subprocess.Popen(
["sleep", "2"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
else:
popen = subprocess.Popen(
["git", "submodule", "update", "--init", "--recursive"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
output = list()
for line in iter(popen.stdout.readline, b""):
output.append(line)
if verbose:
sys.stdout.write(line)
popen.communicate() # Wait to finish
if not verbose:
data["finished"] = True
sys.stdout.write("\n\n")
if mock_error:
output = ["Ack!\n\r", "A mocked error\n\r", "\n\r", "Hmm\n\r"]
if (popen.returncode != 0) or mock_error:
if not accept_defaults:
print("Error: Had some trouble, here are the 4 last lines..")
print("")
print("=" * 60)
print("".join(output[-4:]))
print("=" * 60)
sys.stdout.write("\nPrint the full output? [y/N]: ")
if raw_input().lower() in ("y", "yes"):
print("")
print("".join(output))
else:
print("".join(output))
sys.exit(2)
else:
time.sleep(0.5)
print("Download complete")
time.sleep(1)
sys.stdout.write("\nWriting environment variables..")
dirname = os.path.abspath(os.path.dirname(__file__))
integrations = os.path.join(dirname, "lib", "pyblish", "integrations")
environment = {
"PYTHONPATH": ";".join([
os.path.join(dirname, "pythonpath"),
os.path.join(integrations, "maya")
]),
"HOUDINI_PATH": ";".join([
"&",
os.path.join(integrations, "houdini")
]),
"NUKE_PATH": os.path.join(integrations, "nuke"),
"HIERO_PATH": os.path.join(integrations, "hiero"),
}
for key, value in environment.iteritems():
os.environ[key] = ";".join([value, os.environ.get(key, "")])
if globally:
sys.stdout.write(" globally..\n")
for key in environment:
try:
subprocess.check_output(
["setx", key, os.environ[key]],
env=os.environ
)
except subprocess.CalledProcessError:
print("Error: Couldn't write environment variables globally")
print("See installation guide for instructions on how to ")
print("write them manually.")
print("https://github.com/pyblish/pyblish-win/wiki")
break
else:
sys.stdout.write("\n")
sys.exit(0)
def has_requirements():
try:
popen = subprocess.Popen(
["where", "git"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
popen.communicate()
assert popen.returncode == 0
except:
print("")
print("Error: Installation requires Git")
print("See installation guide for details, or use binary installer")
print("https://github.com/pyblish/pyblish-win/wiki")
return False
return True
def animation(data):
animation = cycle(['|. |',
'|. |',
'| . |',
'| . |',
'| . |',
'| .|',
'| .|',
'| . |',
'| . |',
'| . |',
'|. |'])
counter = 0
start = datetime.datetime.now()
print("")
print("| | Duration")
print("|--------|-----------")
while not data["finished"]:
delta = datetime.datetime.now() - start
d = {
"percentage": ("%.2f %%" % counter).ljust(9),
"duration": "%02d:%02d:%02ds" % (
(delta.seconds / 60 / 60) % 24,
(delta.seconds / 60) % 60,
(delta.seconds) % 60)
}
frame = (animation.next() + " {duration}").format(**d)
sys.stdout.write('\r')
sys.stdout.write(frame)
sys.stdout.flush()
time.sleep(0.1)
# Fake a percentage, to calm their minds.
counter += 0.01
if counter > 100:
counter = 0
def proceed(accept_defaults):
subprocess.call("cls", shell=True)
print(header)
if not accept_defaults:
sys.stdout.write("Install Pyblish? [Y/n]: ")
if raw_input().lower() not in ("", "y", "yes"):
return False
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-g", "--global", action="store_true", dest="globally",
help="Set global environment variables")
parser.add_argument("-v", "--verbose", action="store_true",
help="Print a lot of information")
parser.add_argument("-y", "--yes", action="store_true",
dest="accept_defaults", help="Accept defaults")
parser.add_argument("--mock", action="store_true",
help="Do not actually download anything")
parser.add_argument("--mock-error", action="store_true",
help="Mock an error")
args = parser.parse_args()
try:
main(**args.__dict__)
except KeyboardInterrupt:
print("\nCancelled")
sys.exit(0)
except SystemExit as e:
if e.code == 0:
print("Entering subshell..")
time.sleep(0.5)
# Enter subshell where local variables are present.
dirname = os.path.abspath(os.path.dirname(__file__))
try:
sys.exit(subprocess.call(
["cmd", "/K", os.path.join(dirname, "subshell.bat")],
env=os.environ))
finally:
print("Leaving Pyblish installation program")
elif e.code == 1:
print("Aborted")
else:
print("")
print("Installation failed..")
print("Try the --verbose flag for more output")
print("Or see --help for more options")