import os
import shutil
import signal
import subprocess
from pathlib import Path
MEASUREMENT_TIMEOUT = 1200
def run_measurement(command, timeout=MEASUREMENT_TIMEOUT, grace=0, **kwargs):
with subprocess.Popen(command, start_new_session=True, **kwargs) as process:
try:
status = process.wait(timeout=timeout)
if status:
raise subprocess.CalledProcessError(status, command)
except BaseException:
try:
if grace:
os.killpg(process.pid, signal.SIGINT)
try:
process.wait(timeout=grace)
except subprocess.TimeoutExpired:
pass
os.killpg(process.pid, signal.SIGKILL)
except ProcessLookupError:
pass
process.wait()
raise
def tool_path(binary):
found = Path(shutil.which(binary) or binary).absolute()
if found.is_symlink() and Path(os.path.realpath(found)).stem == "mise":
real = subprocess.run(
["mise", "which", binary], capture_output=True, text=True, check=False
)
target = real.stdout.strip()
if real.returncode == 0 and target:
return str(Path(target).absolute())
return str(found)
return str(found.resolve())
def installed(binary):
return Path(binary).is_file() or shutil.which(binary) is not None