File size: 3,809 Bytes
198fb2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
"""Run a command with timing and optional log file output.

Replaces the GNU `time` + `tee` shell pipeline with a pure-Python
implementation that works on both Linux and macOS without external
dependencies.

Usage:
    run_command.py [--log FILE] [--append] [--tee] -- command [args...]

Output is streamed line-by-line and flushed after every line so that
`tail -f <logfile>` works in real time.  The log path is visible in
`ps` output for discoverability (replaces `ps -Af | grep tee`).

On completion an "Elapsed time: ..." line is appended to stderr (and
the log file) in the same format that the rest of the ORFS
infrastructure expects.
"""

import argparse
import os
import resource
import subprocess
import sys
import time


def _maxrss_kb(ru_maxrss: int) -> int:
    """Normalize ru_maxrss to kilobytes (Linux reports KB, macOS reports bytes)."""
    if sys.platform == "darwin":
        return ru_maxrss // 1024
    return ru_maxrss


def _format_elapsed(seconds: float) -> str:
    """Format seconds as [H:]MM:SS.ff matching GNU time %E output."""
    h = int(seconds // 3600)
    m = int((seconds % 3600) // 60)
    s = seconds % 60
    if h:
        return f"{h}:{m:02d}:{s:05.2f}"
    return f"{m}:{s:05.2f}"


def _build_timing_line(
    wall: float,
    user: float,
    sys_: float,
    peak_kb: int,
) -> str:
    cpu_pct = int(100 * (user + sys_) / wall) if wall > 0 else 0
    return (
        f"Elapsed time: {_format_elapsed(wall)}[h:]min:sec. "
        f"CPU time: user {user:.2f} sys {sys_:.2f} ({cpu_pct}%). "
        f"Peak memory: {peak_kb}KB."
    )


def run(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(
        description="Run a command with timing, optional tee-to-log."
    )
    parser.add_argument("--log", help="Log file path")
    parser.add_argument(
        "--append", action="store_true", help="Append to log file instead of overwrite"
    )
    parser.add_argument(
        "--tee",
        action="store_true",
        help="Also write output to stdout (like tee)",
    )
    parser.add_argument("command", nargs=argparse.REMAINDER)
    args = parser.parse_args(argv)

    cmd = args.command
    if cmd and cmd[0] == "--":
        cmd = cmd[1:]
    if not cmd:
        parser.error("No command specified")

    # Snapshot children resource usage before the subprocess.
    before = resource.getrusage(resource.RUSAGE_CHILDREN)

    log_file = None
    if args.log:
        log_file = open(args.log, "a" if args.append else "w")

    wall_start = time.monotonic()
    env = os.environ.copy()
    for var in ("RUNFILES_DIR", "RUNFILES_MANIFEST_FILE", "RUNFILES_MANIFEST_ONLY"):
        env.pop(var, None)
    proc = subprocess.Popen(
        cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env
    )

    try:
        for line in iter(proc.stdout.readline, b""):
            if args.tee:
                sys.stdout.buffer.write(line)
                sys.stdout.buffer.flush()
            if log_file:
                log_file.write(line.decode(errors="replace"))
                log_file.flush()
        proc.wait()
    except BaseException:
        proc.kill()
        proc.wait()
        raise
    finally:
        wall = time.monotonic() - wall_start

        after = resource.getrusage(resource.RUSAGE_CHILDREN)
        user = after.ru_utime - before.ru_utime
        sys_ = after.ru_stime - before.ru_stime
        peak_kb = _maxrss_kb(after.ru_maxrss)

        timing_line = _build_timing_line(wall, user, sys_, peak_kb)

        sys.stderr.write(timing_line + "\n")
        sys.stderr.flush()
        if log_file:
            log_file.write(timing_line + "\n")
            log_file.flush()
            log_file.close()

    return proc.returncode


if __name__ == "__main__":
    sys.exit(run())