Skip to content

Commit d94052f

Browse files
committed
.
1 parent 4661dbd commit d94052f

File tree

11 files changed

+118
-54
lines changed

11 files changed

+118
-54
lines changed

commit0/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
"""Commit0 Lib"""
2+
13
__version__ = "0.0.1"

commit0/harness/build.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import docker
55
from datasets import load_dataset
6-
6+
from typing import Iterator
77
from commit0.harness.docker_build import build_repo_images
88
from commit0.harness.spec import make_spec
99

@@ -18,7 +18,7 @@ def main(
1818
base_dir: str,
1919
config_file: str,
2020
) -> None:
21-
dataset = load_dataset(hf_name, split="test")
21+
dataset: Iterator[RepoInstance] = load_dataset(hf_name, split="test")
2222
specs = []
2323
for example in dataset:
2424
spec = make_spec(example)
@@ -30,7 +30,12 @@ def main(
3030

3131

3232
def add_init_args(parser: argparse.ArgumentParser) -> None:
33-
parser.add_argument("--hf_name", type=str, help="HF dataset name")
33+
parser.add_argument(
34+
"--hf_name",
35+
type=str,
36+
help="HF dataset name",
37+
default="wentingzhao/commit0_docstring",
38+
)
3439
parser.add_argument(
3540
"--base_dir",
3641
type=str,

commit0/harness/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class RepoInstance(TypedDict):
88
base_commit: str
99
reference_commit: str
1010
setup: dict
11+
test: str
1112

1213

1314
# Constants - Evaluation Log Directories

commit0/harness/docker_build.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import traceback
44
import docker
5+
import docker.errors
56
from tqdm import tqdm
67
from concurrent.futures import ThreadPoolExecutor, as_completed
78
from pathlib import Path
@@ -296,3 +297,6 @@ def build_repo_images(
296297

297298
# Return the list of (un)successfuly built images
298299
return successful, failed
300+
301+
302+
__all__ = []

commit0/harness/docker_utils.py

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pathlib import Path
1212
from io import BytesIO
1313
from typing import Optional, List, Union
14+
import docker.errors
1415

1516
from docker.models.containers import Container
1617

@@ -107,7 +108,7 @@ def safe_extract(
107108

108109
tar.extractall(path, members, numeric_owner=numeric_owner)
109110

110-
safe_extract(tar, path=dst.parent)
111+
safe_extract(tar, path=str(dst.parent))
111112

112113
# Move the extracted file to desired dst path if tar extraction gives src.name
113114
extracted_file_path = dst.parent / src.name
@@ -192,7 +193,7 @@ def write_to_container(container: Container, data: str, dst: Path) -> None:
192193
def cleanup_container(
193194
client: docker.DockerClient,
194195
container: docker.Container,
195-
logger: Union[str, logging.Logger],
196+
logger: Union[None, str, logging.Logger],
196197
) -> None:
197198
"""Stop and remove a Docker container.
198199
Performs this forcefully if the container cannot be stopped with the python API.
@@ -211,8 +212,12 @@ def cleanup_container(
211212

212213
if not logger:
213214
# if logger is None, print to stdout
214-
log_error = print
215-
log_info = print
215+
def log_error(x: str) -> None:
216+
print(x)
217+
218+
def log_info(x: str) -> None:
219+
print(x)
220+
216221
raise_error = True
217222
elif logger == "quiet":
218223
# if logger is "quiet", don't print anything
@@ -224,9 +229,15 @@ def log_error(x: str) -> None:
224229

225230
raise_error = True
226231
else:
232+
assert isinstance(logger, logging.Logger)
233+
227234
# if logger is a logger object, use it
228-
log_error = logger.info
229-
log_info = logger.info
235+
def log_error(x: str) -> None:
236+
logger.info(x)
237+
238+
def log_info(x: str) -> None:
239+
logger.info(x)
240+
230241
raise_error = False
231242

232243
# Attempt to stop the container
@@ -276,11 +287,11 @@ def log_error(x: str) -> None:
276287
def create_container(
277288
client: docker.DockerClient,
278289
image_name: str,
279-
container_name: str = None,
280-
user: str = None,
281-
command: str = None,
282-
nano_cpus: int = None,
283-
logger: Union[str, logging.Logger] = None,
290+
container_name: Optional[str] = None,
291+
user: Optional[str] = None,
292+
command: Optional[str] = None,
293+
nano_cpus: Optional[int] = None,
294+
logger: Optional[Union[str, logging.Logger]] = None,
284295
) -> Container:
285296
"""Start a Docker container using the specified image.
286297
@@ -312,19 +323,33 @@ def create_container(
312323

313324
if not logger:
314325
# if logger is None, print to stdout
315-
log_error = print
316-
log_info = print
326+
def log_error(x: str) -> None:
327+
print(x)
328+
329+
def log_info(x: str) -> None:
330+
print(x)
331+
332+
raise_error = True
317333
elif logger == "quiet":
318334
# if logger is "quiet", don't print anything
319335
def log_info(x: str) -> None:
320336
return None
321337

322338
def log_error(x: str) -> None:
323339
return None
340+
341+
raise_error = True
324342
else:
343+
assert isinstance(logger, logging.Logger)
344+
325345
# if logger is a logger object, use it
326-
log_error = logger.info
327-
log_info = logger.info
346+
def log_error(x: str) -> None:
347+
logger.info(x)
348+
349+
def log_info(x: str) -> None:
350+
logger.info(x)
351+
352+
raise_error = False
328353

329354
container = None
330355
try:
@@ -349,7 +374,7 @@ def log_error(x: str) -> None:
349374

350375
def exec_run_with_timeout(
351376
container: Container, cmd: str, timeout: Optional[int] = 60
352-
) -> None:
377+
) -> tuple[str, bool, float]:
353378
"""Run a command in a container with a timeout.
354379
355380
Args:
@@ -369,6 +394,7 @@ def exec_run_with_timeout(
369394
def run_command() -> None:
370395
nonlocal exec_result, exec_id, exception
371396
try:
397+
assert container.client is not None, "Client did not load"
372398
exec_id = container.client.api.exec_create(container.id, cmd)["Id"]
373399
exec_stream = container.client.api.exec_start(exec_id, stream=True)
374400
for chunk in exec_stream:
@@ -393,3 +419,6 @@ def run_command() -> None:
393419
timed_out = True
394420
end_time = time.time()
395421
return exec_result, timed_out, end_time - start_time
422+
423+
424+
__all__ = []

commit0/harness/dockerfiles.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@
5959
"""
6060

6161

62-
def get_dockerfile_base(platform):
62+
def get_dockerfile_base(platform: str) -> str:
6363
return _DOCKERFILE_BASE.format(platform=platform)
6464

6565

66-
def get_dockerfile_repo(platform):
66+
def get_dockerfile_repo(platform: str) -> str:
6767
return _DOCKERFILE_REPO.format(platform=platform)
68+
69+
70+
__all__ = []

commit0/harness/run_pytest_ids.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import traceback
66
import yaml
77
from pathlib import Path
8+
import logging
89

910
from commit0.harness.constants import RUN_PYTEST_LOG_DIR
1011
from commit0.harness.docker_build import (
@@ -20,7 +21,7 @@
2021
delete_file_from_container,
2122
exec_run_with_timeout,
2223
)
23-
from commit0.harness.spec import make_spec
24+
from commit0.harness.spec import Spec, make_spec
2425
from commit0.harness.utils import (
2526
EvaluationError,
2627
extract_test_output,
@@ -35,7 +36,9 @@ class ExecutionBackend(StrEnum):
3536
MODAL = auto()
3637

3738

38-
def run_docker(spec, logger, eval_file, timeout, log_dir):
39+
def run_docker(
40+
spec: Spec, logger: logging.Logger, eval_file: Path, timeout: int, log_dir: Path
41+
) -> None:
3942
client = docker.from_env()
4043
container = None
4144
try:
@@ -68,7 +71,7 @@ def run_docker(spec, logger, eval_file, timeout, log_dir):
6871
if timed_out:
6972
f.write(f"\n\nTimeout error: {timeout} seconds exceeded.")
7073
raise EvaluationError(
71-
repo,
74+
spec.repo,
7275
f"Test timed out after {timeout} seconds.",
7376
logger,
7477
)
@@ -88,7 +91,7 @@ def run_docker(spec, logger, eval_file, timeout, log_dir):
8891
print(e)
8992
except Exception as e:
9093
error_msg = (
91-
f"Error in running pytest for {repo}: {e}\n"
94+
f"Error in running pytest for {spec.repo}: {e}\n"
9295
f"{traceback.format_exc()}\n"
9396
f"Check ({logger.log_file}) for more information."
9497
)
@@ -99,7 +102,9 @@ def run_docker(spec, logger, eval_file, timeout, log_dir):
99102
close_logger(logger)
100103

101104

102-
def run_modal(spec, logger, eval_file, timeout, log_dir):
105+
def run_modal(
106+
spec: Spec, logger: logging.Logger, eval_file: Path, timeout: int, log_dir: Path
107+
) -> None:
103108
# get image name to pull from dockerhub
104109
# spec.repo_image_key
105110
import modal
@@ -164,15 +169,15 @@ def run_modal(spec, logger, eval_file, timeout, log_dir):
164169
output = []
165170
for line in process.stderr:
166171
output.append(line)
167-
output = "".join(line)
168-
logger.info(output)
169-
print(output)
172+
output_s = "".join(line)
173+
logger.info(output_s)
174+
print(output_s)
170175

171176
timed_out = False
172177
total_runtime = 1
173178

174179
test_output = extract_test_output(
175-
output, "--json-report --json-report-file=report.json"
180+
output_s, "--json-report --json-report-file=report.json"
176181
)
177182

178183
# stdout might be more straightforward
@@ -183,23 +188,23 @@ def run_modal(spec, logger, eval_file, timeout, log_dir):
183188
if timed_out:
184189
f.write(f"\n\nTimeout error: {timeout} seconds exceeded.")
185190
raise EvaluationError(
186-
repo,
191+
spec.repo,
187192
f"Test timed out after {timeout} seconds.",
188193
logger,
189194
)
190195

191196

192197
def main(
193198
repo: str,
194-
test_ids: list[str],
199+
test_ids_ls: list[str],
195200
timeout: int,
196201
branch_name: str,
197202
backend: ExecutionBackend,
198203
) -> None:
199204
with open("config.yml", "r") as file:
200205
data = yaml.safe_load(file)
201206
spec = make_spec(data["repos"][repo])
202-
test_ids = " ".join(test_ids)
207+
test_ids = " ".join(test_ids_ls)
203208
hashed_test_ids = get_hash_string(test_ids)
204209

205210
# set up logging
@@ -250,7 +255,7 @@ def add_init_args(parser: argparse.ArgumentParser) -> None:
250255
def run(args: argparse.Namespace) -> None:
251256
main(
252257
repo=args.repo,
253-
test_ids=args.test_ids,
258+
test_ids_ls=args.test_ids,
254259
timeout=args.timeout,
255260
branch_name=args.branch_name,
256261
backend=args.backend,

commit0/harness/setup.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import docker
66
import yaml
77
from datasets import load_dataset
8-
8+
from typing import Iterator
99
from commit0.harness.utils import clone_repo
10-
from commit0.harness.constants import REPO_IMAGE_BUILD_DIR
10+
from commit0.harness.constants import REPO_IMAGE_BUILD_DIR, RepoInstance
1111
from commit0.harness.docker_build import build_repo_images
1212
from commit0.harness.spec import make_spec
1313

@@ -22,7 +22,7 @@ def main(
2222
base_dir: str,
2323
config_file: str,
2424
) -> None:
25-
dataset = load_dataset(hf_name, split="test")
25+
dataset: Iterator[RepoInstance] = load_dataset(hf_name, split="test") # type: ignore
2626
out = dict()
2727
specs = []
2828
for example in dataset:
@@ -34,7 +34,9 @@ def main(
3434
os.path.join(base_dir, repo_name)
3535
)
3636
clone_url = f"https://github.com/{example['repo']}.git"
37-
clone_repo(clone_url, out[repo_name]["local_path"], example["base_commit"])
37+
clone_repo(
38+
clone_url, out[repo_name]["local_path"], example["base_commit"], logger
39+
)
3840

3941
config_file = os.path.abspath(config_file)
4042
with open(config_file, "w") as f:

0 commit comments

Comments
 (0)