#!/usr/bin/env python3
import fcntl
import pathlib
import subprocess
import sys

lock_path = pathlib.Path(sys.argv[1])
cmd = sys.argv[2:]

lock_path.parent.mkdir(parents=True, exist_ok=True)
with lock_path.open("w") as lf:
    fcntl.flock(lf, fcntl.LOCK_EX)
    result = subprocess.run(cmd)
    fcntl.flock(lf, fcntl.LOCK_UN)
sys.exit(result.returncode)
