"""Utility helpers extracted from the original script.""" def shortname(name: str) -> str: return name.split(".")[0] def dur(sec: int) -> str: sec = int(sec) h = int(sec / 3600) m = int((sec - h * 3600) / 60) s = int((sec - h * 3600) % 60) if h > 0: return "%d:%02d:%02d" % (h, m, s) if m > 0: return "%d:%02d" % (m, s) return "0:%02d" % s def initlog(logfile: str): """Open logfile for appending; fall back to creating it or returning stderr. This mirrors the original behaviour from the monolithic script. """ try: return open(logfile, "a+") except Exception: pass try: return open(logfile, "w") except Exception as e: import sys print(f"cannot open logfile {logfile}, using STDERR: {e}") return sys.stderr