forked from plaidml/plaidml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote
executable file
·51 lines (43 loc) · 1.26 KB
/
remote
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
#!/usr/bin/env python3
import argparse
import os
import pathlib
import subprocess
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--host', default='lab-13')
args, remainder = parser.parse_known_args()
# 1. compute target directory based on CWD
cwd = pathlib.Path(os.getcwd())
home = pathlib.Path('~').expanduser()
tgt_dir = cwd.relative_to(home)
base_dir = tgt_dir.parent
if base_dir:
subprocess.check_call([
'ssh', '-q', args.host, 'mkdir -p {}'.format(base_dir.as_posix())
])
# 2. upload CWD to remote using rsync
exclude_from = cwd / '.exclude'
spec = '{}:{}'.format(args.host, tgt_dir.as_posix())
subprocess.check_call([
'rsync',
'-av',
'-F',
'--delete',
'--exclude-from',
str(exclude_from),
'.',
spec,
])
# 3. run remote command from remote CWD
# if '--' in remainder:
# remainder.remove('--')
remote_cmd = ' '.join(remainder)
bash_cmd = "'cd {}; {}'".format(tgt_dir.as_posix(), remote_cmd)
subprocess.check_call(
['ssh', '-q', '-t', args.host, "bash -l -c {}".format(bash_cmd)])
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass