Skip to content

Commit 8529699

Browse files
committed
reorg
1 parent 4094617 commit 8529699

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

commit0/harness/docker_build.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
from concurrent.futures import ThreadPoolExecutor, as_completed
88
from pathlib import Path
99
from typing import Any
10-
import sys
1110

1211
from commit0.harness.constants import (
1312
BASE_IMAGE_BUILD_DIR,
1413
REPO_IMAGE_BUILD_DIR,
1514
)
1615
from commit0.harness.spec import get_specs_from_dataset
16+
from commit0.harness.utils import setup_logger, close_logger
1717

1818
ansi_escape = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]")
1919

@@ -33,32 +33,6 @@ def __str__(self):
3333
)
3434

3535

36-
def setup_logger(repo: str, log_file: Path, mode: str = "w") -> logging.Logger:
37-
"""Used for logging the build process of images and running containers.
38-
It writes logs to the log file.
39-
"""
40-
log_file.parent.mkdir(parents=True, exist_ok=True)
41-
logger = logging.getLogger(f"{repo}.{log_file.name}")
42-
handler = logging.FileHandler(log_file, mode=mode)
43-
stdout_handler = logging.StreamHandler(sys.stdout)
44-
logger.addHandler(stdout_handler)
45-
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
46-
handler.setFormatter(formatter)
47-
logger.addHandler(handler)
48-
logger.setLevel(logging.INFO)
49-
logger.propagate = False
50-
setattr(logger, "log_file", log_file)
51-
return logger
52-
53-
54-
def close_logger(logger: logging.Logger) -> None:
55-
"""Closes all handlers associated with the given logger to prevent too many open files."""
56-
# To avoid too many open files
57-
for handler in logger.handlers:
58-
handler.close()
59-
logger.removeHandler(handler)
60-
61-
6236
def build_image(
6337
image_name: str,
6438
setup_scripts: dict,

commit0/harness/run_pytest_ids.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
RUN_PYTEST_LOG_DIR,
1313
RepoInstance,
1414
)
15-
from commit0.harness.docker_build import (
16-
setup_logger,
17-
)
1815
from commit0.harness.spec import make_spec
1916
from commit0.harness.utils import (
2017
EvaluationError,
2118
get_hash_string,
2219
generate_patch_between_commits,
20+
setup_logger,
21+
close_logger,
2322
)
2423
from commit0.harness.execution_context import (
2524
ExecutionBackend,
@@ -133,6 +132,7 @@ def main(
133132
f"Test timed out after {timeout} seconds.",
134133
logger,
135134
)
135+
close_logger(logger)
136136
pytest_exit_code = Path(log_dir / "pytest_exit_code.txt").read_text().strip()
137137
sys.exit(pytest_exit_code)
138138
except EvaluationError as e:

commit0/harness/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import logging
55
import os
66
import time
7+
import sys
8+
from pathlib import Path
79
from typing import Optional
810

911
from fastcore.net import HTTP404NotFoundError, HTTP403ForbiddenError # type: ignore
@@ -25,6 +27,32 @@ def __str__(self):
2527
)
2628

2729

30+
def setup_logger(repo: str, log_file: Path, mode: str = "w") -> logging.Logger:
31+
"""Used for logging the build process of images and running containers.
32+
It writes logs to the log file.
33+
"""
34+
log_file.parent.mkdir(parents=True, exist_ok=True)
35+
logger = logging.getLogger(f"{repo}.{log_file.name}")
36+
handler = logging.FileHandler(log_file, mode=mode)
37+
stdout_handler = logging.StreamHandler(sys.stdout)
38+
logger.addHandler(stdout_handler)
39+
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
40+
handler.setFormatter(formatter)
41+
logger.addHandler(handler)
42+
logger.setLevel(logging.INFO)
43+
logger.propagate = False
44+
setattr(logger, "log_file", log_file)
45+
return logger
46+
47+
48+
def close_logger(logger: logging.Logger) -> None:
49+
"""Closes all handlers associated with the given logger to prevent too many open files."""
50+
# To avoid too many open files
51+
for handler in logger.handlers:
52+
handler.close()
53+
logger.removeHandler(handler)
54+
55+
2856
def get_hash_string(input_string: str) -> str:
2957
# Create a new SHA-256 hash object
3058
sha256 = hashlib.sha256()

0 commit comments

Comments
 (0)