import argparse
import os
import subprocess
import sys
from argparse import Namespace
def run_process(args: list[str], env=None) -> bool:
process = subprocess.run(args, env=env)
return process.returncode == 0
def run_iteration(windows: bool, strict: bool, script: list[str]) -> bool:
build = ['cargo', 'build', '--release']
test = ['cargo', 'test', '--lib', '--quiet']
rebase = ['git', 'rebase', '--continue']
env = os.environ
if windows:
build.append('--target=x86_64-pc-windows-gnu')
test.append('--target=x86_64-pc-windows-gnu')
test.append('--no-run')
if strict:
env['RUSTFLAGS'] = '-D warnings'
if script and not run_process(script):
return False
if not run_process(build, env):
return False
if not run_process(test, env):
return False
if not run_process(rebase):
return False
return True
def parse_args() -> Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('script', nargs='*', default=[], help='run script with args')
parser.add_argument('-w', '--windows', action='store_true', help='build for Windows')
parser.add_argument('-s', '--strict', action='store_true', help='warnings as errors')
parser.add_argument('-l', '--loop', action='store_true', help='loop until failure')
return parser.parse_args()
def run_main():
settings = parse_args()
if settings.loop:
while run_iteration(settings.windows, settings.strict, settings.script):
pass
else:
run_iteration(settings.windows, settings.strict, settings.script)
try:
run_main()
except KeyboardInterrupt as error:
print(error, file=sys.stderr)