MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / run_process

Function run_process

tests/test_coverage_flush.py:101–154  ·  view source on GitHub ↗
(directory, label, argv, environment)

Source from the content-addressed store, hash-verified

99
100
101def run_process(directory, label, argv, environment):
102 record = {"argv": argv, "deadline_seconds": DEADLINE_SECONDS, "returncode": None,
103 "failure": None, "stdout": label + ".stdout.log", "stderr": label + ".stderr.log"}
104 started = time.monotonic()
105 try:
106 proc = subprocess.Popen(argv, env=environment, stdin=subprocess.DEVNULL,
107 stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0)
108 except OSError as exc:
109 record["failure"] = "spawn: " + str(exc)
110 return record
111 record["pid"] = proc.pid
112 stop = threading.Event()
113 streams = []
114 threads = []
115 started_pipes = []
116 try:
117 for kind, pipe in (("stdout", proc.stdout), ("stderr", proc.stderr)):
118 state = {"seen_bytes": 0, "saved_bytes": 0, "overflow": False, "error": None}
119 thread = threading.Thread(target=drain,
120 args=(pipe, directory / record[kind], state, stop), daemon=True)
121 thread.start()
122 streams.append(state)
123 threads.append(thread)
124 started_pipes.append(pipe)
125 while proc.poll() is None:
126 remaining = DEADLINE_SECONDS - (time.monotonic() - started)
127 if stop.is_set() or remaining <= 0:
128 record["failure"] = "log limit/read failure" if stop.is_set() else "timeout"
129 break
130 try:
131 proc.wait(timeout=min(remaining, 0.1))
132 except subprocess.TimeoutExpired:
133 pass
134 except (OSError, RuntimeError, KeyboardInterrupt) as exc:
135 record["failure"] = type(exc).__name__ + ": " + str(exc)
136 finally:
137 cleanup_error = reap(proc)
138 if cleanup_error:
139 record["cleanup_error"] = cleanup_error
140 record["failure"] = record["failure"] or cleanup_error
141 for pipe in (proc.stdout, proc.stderr):
142 if pipe not in started_pipes:
143 pipe.close()
144 join_until = time.monotonic() + REAP_SECONDS
145 for thread in threads:
146 thread.join(timeout=max(0, join_until - time.monotonic()))
147 if any(thread.is_alive() for thread in threads):
148 record["failure"] = "pipe reader did not quiesce; unexpected inherited pipe"
149 if any(item["overflow"] or item["error"] for item in streams):
150 record["failure"] = record["failure"] or "bounded log capture failed"
151 record["returncode"] = proc.poll()
152 record["elapsed_seconds"] = round(time.monotonic() - started, 3)
153 record["streams"] = dict(zip(("stdout", "stderr"), streams))
154 return record
155
156
157def regular_nonempty(path):

Callers 2

merge_profileFunction · 0.70
run_caseFunction · 0.70

Calls 9

strFunction · 0.85
zipFunction · 0.85
startMethod · 0.80
waitMethod · 0.80
reapFunction · 0.70
typeFunction · 0.50
roundFunction · 0.50
pollMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected