-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
67 lines (56 loc) · 2.32 KB
/
runner.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
# no shebang-line as Python scripts aren't really executable on Windows
# use runner = ["python", "runner.py"] in your Cargo config instead
from hashlib import sha3_224
from shlex import quote
import os
import pathlib
import platform
import sys
import subprocess as sp
# "parse" command line
EXECUTABLE = sys.argv[1]
ARGUMENTS = sys.argv[2:]
# get relative part of executable path and convert to POSIX (as host may be Windows)
EXECUTABLE_RELATIVE = pathlib.Path(os.path.relpath(EXECUTABLE)).as_posix()
# create a (statistically) unique name for the remote working directory copy
WORKDIR = sha3_224((platform.node() + ':' + os.getcwd()).encode('utf-8')).hexdigest()
# the target hardware (Bela.io) has passwordless root login
# for normal systems you'll need to handle user authentication in a smarter manner
SSH_NONINTERACTIVE = ['ssh', '-qTo', 'BatchMode yes', '[email protected]']
SSH_INTERACTIVE = ['ssh', '-qt', '[email protected]']
# use rsync via WSL when on Windows
RSYNC = (['wsl'] if platform.system() == 'Windows' else []) + ['rsync']
# ensure base directory exists
sp.run(SSH_NONINTERACTIVE + [
'mkdir', '-p', '.cargo_runner'
], stdout=sp.DEVNULL, stderr=sp.DEVNULL, check=True)
# synchronize working directory to remote
sp.run(RSYNC + [
'-rlptz',
# prevent syncing the .git folder
'--exclude', '.git',
# the following files can be very large and are usually not required on the Bela
'--exclude', '*.d',
'--exclude', '*.rlib',
'--exclude', '*.rmeta',
'--exclude', '*.exe',
'--exclude', '*.exp',
'--exclude', '*.lib',
'--exclude', '*.pdb',
# delete old files (otherwise they'll get copied back later)
'--delete',
'.',
f'[email protected]:.cargo_runner/{WORKDIR}/'
], stdout=sp.DEVNULL, stderr=sp.DEVNULL, check=True)
# run executable remotely, explicitly without checking, as partial results should still be copied
code = sp.run(SSH_INTERACTIVE + [
f'cd .cargo_runner/{WORKDIR} && {quote(EXECUTABLE_RELATIVE)} {" ".join(map(quote, ARGUMENTS))}'
]).returncode
# synchronize working directory from remote (for Criterion reports etc.)
sp.run(RSYNC + [
'-rlptz',
f'[email protected]:.cargo_runner/{WORKDIR}/',
'.',
], stdout=sp.DEVNULL, stderr=sp.DEVNULL, check=True)
# exit with the code from the actual run
sys.exit(code)