Skip to content
Snippets Groups Projects
Commit 9e567357 authored by chrg's avatar chrg
Browse files

Improve logging and failure handeling

parent c5128291
No related branches found
No related tags found
No related merge requests found
from contextlib import contextmanager
from dataclasses import dataclass, field from dataclasses import dataclass, field
from pathlib import Path from pathlib import Path
from typing import Callable from typing import Callable
...@@ -63,6 +62,7 @@ class BlobHandler: ...@@ -63,6 +62,7 @@ class BlobHandler:
change.blob_id = self.blobs_handled[change.blob_id] change.blob_id = self.blobs_handled[change.blob_id]
def read_blob(self, blob_id: bytes): def read_blob(self, blob_id: bytes):
log.debug("Reading blob %s", blob_id)
assert self.gitcat is not None assert self.gitcat is not None
# To get the typecheck to pass # To get the typecheck to pass
stdin, stdout = self.gitcat.stdin, self.gitcat.stdout stdin, stdout = self.gitcat.stdin, self.gitcat.stdout
...@@ -135,18 +135,16 @@ def regit( ...@@ -135,18 +135,16 @@ def regit(
with tempfile.TemporaryDirectory() as folder: with tempfile.TemporaryDirectory() as folder:
folder = Path(folder) folder = Path(folder)
def transformer(file: Path, content: bytes): def transformer(file: Path, content: bytes) -> bytes:
pargs = list(args) pargs = list(args)
try: try:
ix = pargs.index("{}") ix = pargs.index("{}")
except ValueError: except ValueError:
return utils.run( return utils.run_stdout([program] + pargs, input=content)
[program] + pargs, input=content, capture_output=True, check=True
).stdout
else: else:
with utils.tfile(folder, file.name, content) as tmp_file: with utils.tfile(folder, file.name, content) as tmp_file:
pargs[ix] = str(tmp_file) pargs[ix] = str(tmp_file)
utils.run([program] + pargs, check=True) utils.run([program] + pargs)
with open(tmp_file, "rb") as f: with open(tmp_file, "rb") as f:
return f.read() return f.read()
...@@ -169,6 +167,7 @@ def regit( ...@@ -169,6 +167,7 @@ def regit(
log.debug("Finished git filter") log.debug("Finished git filter")
if mapping: if mapping:
log.debug("Writing mapping to %s", mapping)
writer = csv.writer(mapping) writer = csv.writer(mapping)
writer.writerow(["from", "to"]) writer.writerow(["from", "to"])
for fm, to in filter._commit_renames.items(): for fm, to in filter._commit_renames.items():
......
from contextlib import contextmanager from contextlib import contextmanager
from pathlib import Path from pathlib import Path
import logging import logging
import time
import os import os
import subprocess import subprocess
import uuid
log = logging.getLogger("regit") log = logging.getLogger("regit")
def run(cmd, **kwargs): @contextmanager
def timeit(name):
start = time.time()
log.debug("Started %s", name)
yield
end = time.time()
log.debug("Done running %s in %s", end - start)
def handle_results(
uid, x: subprocess.CompletedProcess[bytes], input: None | bytes = None
):
if x.returncode == 0:
log.debug("Command %s succeded!")
return
f = Path(str(uid)).absolute()
log.debug("Command %s failed! writing inputs to files %s", x.args, f)
if input is not None:
with open(f.with_suffix(".stdin"), "bw") as o:
o.write(input)
if x.stdout is not None:
with open(f.with_suffix(".stdout"), "bw") as o:
o.write(x.stdout)
x.check_returncode()
def run(cmd) -> None:
"""A wrapper around subprocess.run that logs the command.""" """A wrapper around subprocess.run that logs the command."""
log.debug("Running cmd %s", cmd) uid = uuid.uuid4()
return subprocess.run(cmd, **kwargs) log.debug("Running cmd %s with id %s", cmd, uid)
with timeit(uid):
x = subprocess.run(
cmd,
capture_output=True,
universal_newlines=False,
text=False,
)
handle_results(uid, x)
def run_stdout(cmd, input: bytes) -> bytes:
"""A wrapper around subprocess.run that logs the command."""
uid = uuid.uuid4()
log.debug("Running cmd %s with id %s", cmd, uid)
with timeit(uid):
x = subprocess.run(
cmd,
input=input,
capture_output=True,
universal_newlines=False,
text=False,
)
handle_results(uid, x, input=input)
return x.stdout
def popen(cmd, **kwargs): def popen(cmd, **kwargs):
...@@ -20,7 +72,7 @@ def popen(cmd, **kwargs): ...@@ -20,7 +72,7 @@ def popen(cmd, **kwargs):
@contextmanager @contextmanager
def chdir(path: Path): def chdir(path: os.PathLike[str] | str | Path):
"""Change the current working directory to the given path and back when done.""" """Change the current working directory to the given path and back when done."""
old = Path.cwd() old = Path.cwd()
log.debug("Changing dir from %s to %s", old, path) log.debug("Changing dir from %s to %s", old, path)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment