|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | + |
| 9 | +def collect_wasm_env(local_env=os.environ, prefix='WASM_RUN_CHILD_'): |
| 10 | + return dict((key[len(prefix):], value) |
| 11 | + for key, value in local_env.items() if key.startswith(prefix)) |
| 12 | + |
| 13 | + |
| 14 | +class WasmtimeRunner(object): |
| 15 | + def __init__(self): |
| 16 | + pass |
| 17 | + |
| 18 | + def run(self, args): |
| 19 | + command = self.invocation(args) |
| 20 | + if args.verbose: |
| 21 | + print(' '.join(command), file=sys.stderr) |
| 22 | + |
| 23 | + if not args.dry_run: |
| 24 | + subprocess.check_call(command) |
| 25 | + |
| 26 | + def invocation(self, args): |
| 27 | + command = ["wasmtime", "run"] |
| 28 | + envs = collect_wasm_env() |
| 29 | + for key in envs: |
| 30 | + command.append("--env") |
| 31 | + command.append(f"{key}={envs[key]}") |
| 32 | + command.append("--") |
| 33 | + command.extend(args.command) |
| 34 | + return command |
| 35 | + |
| 36 | + |
| 37 | +def main(): |
| 38 | + parser = argparse.ArgumentParser() |
| 39 | + |
| 40 | + parser.add_argument('-v', '--verbose', action='store_true', dest='verbose', |
| 41 | + help='print commands as they are run') |
| 42 | + parser.add_argument('-n', '--dry-run', action='store_true', dest='dry_run', |
| 43 | + help="print the commands that would have been run, but" |
| 44 | + " don't actually run them") |
| 45 | + parser.add_argument('command', nargs=argparse.REMAINDER, |
| 46 | + help='the command to run', metavar='command...') |
| 47 | + |
| 48 | + args = parser.parse_args() |
| 49 | + runner = WasmtimeRunner() |
| 50 | + runner.run(args) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + main() |
0 commit comments