From 31257e6443fd7cd2047c68015e3531672d33472c Mon Sep 17 00:00:00 2001 From: Jonathon Reinhart Date: Sat, 30 Dec 2023 19:36:22 -0500 Subject: [PATCH 1/2] Add rust code formattting to code_format.py --- code_format.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/code_format.py b/code_format.py index d93d57ab..8c3d433f 100755 --- a/code_format.py +++ b/code_format.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import argparse import black +import subprocess def _run_black(fix: bool) -> bool: @@ -25,6 +26,27 @@ def _run_black(fix: bool) -> bool: raise Exception(f"Unexpected exit status: {status}") +def _rust_fmt(fix: bool) -> bool: + args = [ + "cargo", + "fmt", + ] + + if not fix: + # check only + args += [ + "--check", + ] + + status = subprocess.call(args, cwd="scubainit") + if status == 0: + print("Ok") + return True + if status == 1: + return False + raise Exception(f"Unexpected exit status: {status}") + + def _parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser() parser.add_argument( @@ -38,10 +60,17 @@ def _parse_args() -> argparse.Namespace: def main() -> None: args = _parse_args() - print(f"{'Fixing' if args.fix else 'Checking'} code formatting...") - ok = _run_black(args.fix) + ok = True + + print(f"\n{'Fixing' if args.fix else 'Checking'} Python code formatting...") + ok &= _run_black(args.fix) + + print(f"\n{'Fixing' if args.fix else 'Checking'} Rust code formatting...") + ok &= _rust_fmt(args.fix) + if not ok: print("\nTo fix, rerun with --fix") + raise SystemExit(1) if __name__ == "__main__": From e82f46e8d74efcd6edb25b3dd4985f7f53a8e216 Mon Sep 17 00:00:00 2001 From: Jonathon Reinhart Date: Sat, 30 Dec 2023 19:47:22 -0500 Subject: [PATCH 2/2] Allow code_format.py to run from any directory --- code_format.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/code_format.py b/code_format.py index 8c3d433f..6f7d6b88 100755 --- a/code_format.py +++ b/code_format.py @@ -2,11 +2,15 @@ import argparse import black import subprocess +from pathlib import Path + +PROJECT_DIR = Path(__file__).parent.resolve() +SCUBAINIT_DIR = PROJECT_DIR / "scubainit" def _run_black(fix: bool) -> bool: args = [ - ".", + str(PROJECT_DIR), ] if not fix: @@ -38,7 +42,7 @@ def _rust_fmt(fix: bool) -> bool: "--check", ] - status = subprocess.call(args, cwd="scubainit") + status = subprocess.call(args, cwd=SCUBAINIT_DIR) if status == 0: print("Ok") return True