summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2023-12-27 09:20:16 +0200
committerLars Wirzenius <liw@liw.fi>2023-12-27 09:20:16 +0200
commit7af3f054bc49bb1ac7ab1a90047c8602fca2703b (patch)
tree0fb614a2d06e8a29e3b70960051e6b7c9ce37e64
parent955ca58b5cfdd1b84f810cbf5405b412dc5f7dac (diff)
downloadwumpus-hunter-7af3f054bc49bb1ac7ab1a90047c8602fca2703b.tar.gz
feat: improve logging, polish things
Signed-off-by: Lars Wirzenius <liw@liw.fi>
-rwxr-xr-xwumpus-hunter51
1 files changed, 34 insertions, 17 deletions
diff --git a/wumpus-hunter b/wumpus-hunter
index 887871a..305346f 100755
--- a/wumpus-hunter
+++ b/wumpus-hunter
@@ -10,6 +10,7 @@ HEARTWOOD_URL = "https://seed.radicle.xyz/z3gqcJUoA1n9HaHKufZs5FCSGazv5.git"
HEARTWOOD_REF = "master"
CLONE_DIR = os.path.expanduser("~/src")
TEST_CMD = "timeout 600s cargo test"
+LOG_FILE = os.path.expanduser("~/log.txt")
def parse_args():
@@ -22,11 +23,17 @@ def parse_args():
"--dir", default=CLONE_DIR, help="Directory where to clone the repository"
)
p.add_argument("--test", default=TEST_CMD, help="Command to run tests")
+ p.add_argument("--log", default=LOG_FILE, help="Write log file")
return p.parse_args()
-def setup_logging():
- logging.basicConfig(level=logging.DEBUG)
+def setup_logging(log):
+ logging.basicConfig(
+ filename=log,
+ datefmt="%Y-$%m-%d %H:%M:%S",
+ format="%(asctime)s %(levelname)s %(message)s",
+ level=logging.DEBUG,
+ )
def get_code(url, ref, dirname):
@@ -72,36 +79,46 @@ def git_show_head(dirname):
def git(*args, cwd=None):
- p = subprocess.run(["git"] + list(args), check=True, cwd=cwd)
+ run(["git"] + list(args), cwd=cwd)
+
+
+def run(argv, cwd=None):
+ logging.debug(f"running command {argv} in {cwd}")
+ p = subprocess.run(argv, cwd=cwd, capture_output=True)
+ log_output("stdout", p.stdout)
+ log_output("stderr", p.stderr)
+ logging.info(f"exit code {p.returncode}")
if p.returncode != 0:
- return Exception(f"git {args} failed")
+ return Exception(f"command {argv} failed")
+
+
+def log_output(stream, output):
+ if output:
+ logging.info(f"{stream}\n{output.decode()}")
+ else:
+ logging.info(f"{stream} is empty")
def build(dirname):
logging.info(f"build code in {dirname}")
- p = subprocess.run(
- ["cargo", "build", "--workspace", "--all-targets"], check=True, cwd=dirname
- )
- if p.returncode != 0:
- return Exception(f"cargo build failed")
+ run(["cargo", "build", "--workspace", "--all-targets"], cwd=dirname)
def run_tests(dirname, cmd):
logging.info(f"run tests in {dirname}: {cmd}")
- p = subprocess.run(["bash", "-c", cmd], check=True, cwd=dirname)
- return p.returncode == 0
+ run(["bash", "-c", cmd], cwd=dirname)
def main():
args = parse_args()
- print(args)
- setup_logging()
+ setup_logging(args.log)
+ logging.debug(f"url: {args.url}")
+ logging.debug(f"ref: {args.ref}")
+ logging.debug(f"dir: {args.dir}")
get_code(args.url, args.ref, args.dir)
build(args.dir)
- if run_tests(args.dir, args.test):
- print("RESULT: OK")
- else:
- print("RESULT: TEST-FAIL")
+ run_tests(args.dir, args.test)
+ print("RESULT: ALL OK")
main()