Skip to content

Instantly share code, notes, and snippets.

@l0calh05t
Created August 15, 2021 18:32
Show Gist options
  • Save l0calh05t/3d0769dc89bc13938510c543a060722c to your computer and use it in GitHub Desktop.
Save l0calh05t/3d0769dc89bc13938510c543a060722c to your computer and use it in GitHub Desktop.
Running Rust tests/benchmarks remotely using `rsync` and `ssh` on Bela
# 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', 'root@bela.local']
SSH_INTERACTIVE = ['ssh', '-qt', 'root@bela.local']
# 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',
'.',
f'root@bela.local:.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'root@bela.local:.cargo_runner/{WORKDIR}/',
'.',
], stdout=sp.DEVNULL, stderr=sp.DEVNULL, check=True)
# exit with the code from the actual run
sys.exit(code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment