forked from dolphin-emu/sadm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.py
316 lines (248 loc) · 9.74 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
#! /usr/bin/env python3
"""
install.py -- Dolphin SADM installer.
Think of it as Makefiles written in Python. Files called "INSTALL.py" anywhere
in the repository are loaded by this script to find possible actions (declared
as functions).
"""
import argparse
import collections
import functools
import getpass
import pathlib
import shutil
import subprocess
import sys
import textwrap
try:
import paramiko
except ImportError:
paramiko = None
MAIN_REPO = 'https://github.com/dolphin-emu/sadm'
Component = collections.namedtuple('Component', 'name func doc')
class State:
def __init__(self, parent=None):
self.parent = parent
def __getattr__(self, name):
if name in self.__dict__:
return self.__dict__['name']
if self.parent is None:
raise AttributeError('No attribute %r in state.' % name)
return getattr(self.parent, name)
state = State()
deferred_handlers = []
def builtin_path(p):
return pathlib.Path(p)
def builtin_public_component(func):
"""Public components are exposed through CLI."""
func.is_public_component = True
return func
def builtin_utility(func):
"""Utility functions can be called from other modules without resetting
context-dependent attributes (e.g. the source path)."""
func.is_utility = True
return func
def builtin_print(*args, **kwargs):
return print(*args, **kwargs)
def builtin_pwd():
try:
return state.pwd
except AttributeError:
# Default to source directory.
return state.source_dir
def builtin_source_dir():
return state.source_dir
def builtin_run(cmd, stdin='', capture=False):
shell = isinstance(cmd, str)
stdout = subprocess.PIPE if capture else None
stderr = subprocess.PIPE if capture else None
if not capture:
print('[+] Running: %s' % cmd)
popen = subprocess.Popen(cmd,
shell=shell,
cwd=str(builtin_pwd()),
stdin=subprocess.PIPE,
stdout=stdout,
stderr=stderr)
return popen.communicate(stdin)
builtin_chown = shutil.chown
def builtin_defer(func, *args, **kwargs):
"""Enqueues an action to run after everything has been processed."""
deferred_handlers.append(functools.partial(func, *args, **kwargs))
def state_decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
global state
state = State(state)
try:
return f(*args, **kwargs)
finally:
state = state.parent
return wrapper
def source_dir_decorator(install_file):
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
global state
state.source_dir = install_file.parent
return f(*args, **kwargs)
return wrapper
return decorator
def repo_path():
return pathlib.Path('.')
def enumerate_install_files():
return repo_path().glob('**/INSTALL.py')
def get_new_config_namespace():
return {name[len('builtin_'):]: func
for (name, func) in globals().items()
if callable(func) and name.startswith('builtin_')}
def postprocess_install_function(install_file, func):
decorators = [functools.lru_cache(maxsize=None)]
if not getattr(func, 'is_utility', False):
decorators.append(source_dir_decorator(install_file))
decorators.append(state_decorator)
for decorator in decorators:
func = decorator(func)
return func
def load_all_actions():
namespace = get_new_config_namespace()
builtins = set(namespace.keys())
what_defined_where = {} # Name -> (object, install file).
for install_file in enumerate_install_files():
with install_file.open() as f:
code = compile(f.read(), str(install_file), 'exec')
exec(code, namespace, namespace)
# Try to find redefinitions (which should not happen).
for name, (rule, original_install) in what_defined_where.items():
if namespace.get(name, rule) is not rule:
raise RuntimeError(
'Rule %r (defined in %s) redefined by %s' %
(name, original_install, install_file))
# Process the namespace in place. This is required for __globals__
# in each function to point to the postprocessed version.
for name, rule in namespace.items():
if name not in what_defined_where and name not in builtins:
if callable(rule):
rule = namespace[name] = postprocess_install_function(
install_file, rule)
what_defined_where[name] = (rule, install_file)
return namespace
def get_components(actions):
for name, action in actions.items():
if not getattr(action, 'is_public_component', False):
continue
yield Component(name, action, action.__doc__)
def run_action(actions, func):
actions['__to_run'] = func
exec('__to_run()', actions, actions)
def tree_is_clean():
if (repo_path() / '.git').is_dir():
out = subprocess.run(['git', 'status', '--porcelain'],
stdout=subprocess.PIPE)
return out.returncode == 0 and out.stdout == b''
return True
def ssh_connect(host):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
try:
client.connect(host, username='root')
except paramiko.ssh_exception.PasswordRequiredException:
client.connect(host, username='root', password=getpass.getpass())
return client
def ssh_run(remote, command, data=b''):
stdin, stdout, stderr = remote.exec_command(command)
stdin.write(data)
stdin.channel.shutdown_write()
stdin.close()
return stdout.read(), stderr.read()
def ssh_interactive(chan):
"""Stolen from paramiko.demos.interactive."""
import select
import socket
import termios
import tty
oldtty = termios.tcgetattr(sys.stdin)
try:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
chan.settimeout(0.0)
while True:
r, w, e = select.select([chan, sys.stdin], [], [])
if chan in r:
try:
x = chan.recv(1024).decode('utf-8')
if len(x) == 0:
sys.stdout.write('\r\n*** EOF\r\n')
break
sys.stdout.write(x)
sys.stdout.flush()
except socket.timeout:
pass
if sys.stdin in r:
x = sys.stdin.read(1)
if len(x) == 0:
break
chan.send(x)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--local_config',
action='store_true',
help='Deploys local modifications instead of HEAD.')
parser.add_argument('--host', help='Host to deploy against.')
parser.add_argument('--deploy_here',
action='store_true',
help='Deploy on the machine running this script.')
parser.add_argument('--list', action='store_true', help='List components.')
parser.add_argument('component', nargs='*', help='Components to deploy.')
args = parser.parse_args()
actions = load_all_actions()
if args.list:
wrapper = textwrap.TextWrapper(initial_indent='\t')
for component in sorted(get_components(actions)):
doc = component.doc or 'Undocumented.'
print(' - %r:\n%s' % (component.name, wrapper.fill(doc)))
sys.exit(0)
if args.host:
if paramiko is None:
raise ImportError("Please install the Python 'paramiko' module.")
remote = ssh_connect(args.host)
tmpdir, _= ssh_run(remote, 'mktemp -td sadm_install.XXXXXXXX')
tmpdir = tmpdir.rstrip()
try:
ssh_run(remote, b'cd %s && git clone %s repo' %
(tmpdir, MAIN_REPO.encode('utf-8')))
if args.local_config:
head, _ = ssh_run(remote,
b'cd %s/repo && git rev-parse HEAD' % tmpdir)
out = subprocess.run(['git', 'diff', head.rstrip()],
stdout=subprocess.PIPE)
if out.returncode != 0:
raise RuntimeError('git diff returned %d' % out.returncode)
ssh_run(remote, b'cd %s/repo && patch -Np1' % tmpdir,
out.stdout)
chan = remote.invoke_shell()
chan.send(
b'cd %s/repo && exec python3 install.py %s --deploy_here %s\n'
% (tmpdir, b'--local_config' if args.local_config else b'',
b' '.join(s.encode('utf-8') for s in args.component)))
ssh_interactive(chan)
finally:
ssh_run(remote, b'rm -rf %s' % tmpdir)
sys.exit(0)
if args.deploy_here:
if not tree_is_clean() and not args.local_config:
raise RuntimeError(
'Local deployment requested, but tree is unclean. '
'Do you need --local_config?')
for component in args.component:
if component not in set(c.name for c in get_components(actions)):
raise ValueError('%r is not a component. Use --list.' %
component)
for component in args.component:
run_action(actions, actions[component])
for action in deferred_handlers:
run_action(actions, action)
sys.exit(0)