Skip to content

Commit 0940d07

Browse files
authored
Merge branch 'main' into hydra
2 parents dbdd0e1 + bb3213c commit 0940d07

File tree

14 files changed

+649
-102
lines changed

14 files changed

+649
-102
lines changed

.github/workflows/pre-commit.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: pre-commit
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
jobs:
9+
pre-commit:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-python@v3
14+
- uses: pre-commit/[email protected]

commit0/__init__.py

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,3 @@
1-
__version__ = "0.0.1"
2-
3-
4-
from commit0.harness.docker_build import (
5-
build_image,
6-
build_base_images,
7-
build_repo_images,
8-
close_logger,
9-
setup_logger,
10-
)
1+
"""Commit0 Lib"""
112

12-
from commit0.harness.docker_utils import (
13-
cleanup_container,
14-
copy_to_container,
15-
copy_from_container,
16-
delete_file_from_container,
17-
exec_run_with_timeout,
18-
write_to_container,
19-
create_container,
20-
)
21-
22-
from commit0.harness.utils import (
23-
extract_test_output,
24-
)
25-
26-
__all__ = [
27-
"build_image",
28-
"build_base_images",
29-
"build_repo_images",
30-
"close_logger",
31-
"setup_logger",
32-
"cleanup_container",
33-
"copy_to_container",
34-
"copy_from_container",
35-
"delete_file_from_container",
36-
"exec_run_with_timeout",
37-
"write_to_container",
38-
"create_container",
39-
"extract_test_output",
40-
]
3+
__version__ = "0.0.1"

commit0/__main__.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import argparse
2+
import commit0.harness.run_pytest_ids
3+
import commit0.harness.build
4+
import commit0.harness.setup
5+
6+
7+
def main() -> None:
8+
parser = argparse.ArgumentParser(description="Commit0 version control system")
9+
subparsers = parser.add_subparsers(dest="command", help="Available commands")
10+
11+
commit0.harness.setup.add_init_args(subparsers.add_parser("clone"))
12+
commit0.harness.build.add_init_args(subparsers.add_parser("build"))
13+
commit0.harness.run_pytest_ids.add_init_args(subparsers.add_parser("test"))
14+
15+
args = parser.parse_args()
16+
17+
if args.command == "clone":
18+
commit0.harness.setup.run(args)
19+
elif args.command == "build":
20+
commit0.harness.build.run(args)
21+
elif args.command == "test":
22+
commit0.harness.run_pytest_ids.run(args)
23+
else:
24+
parser.print_help()
25+
26+
27+
if __name__ == "__main__":
28+
main()
29+
30+
__all__ = []

commit0/harness/build.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import argparse
2+
import logging
3+
4+
import docker
5+
from datasets import load_dataset
6+
from typing import Iterator
7+
from commit0.harness.docker_build import build_repo_images
8+
from commit0.harness.spec import make_spec
9+
10+
logging.basicConfig(
11+
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
12+
)
13+
logger = logging.getLogger(__name__)
14+
15+
16+
def main(
17+
hf_name: str,
18+
base_dir: str,
19+
config_file: str,
20+
) -> None:
21+
dataset: Iterator[RepoInstance] = load_dataset(hf_name, split="test")
22+
specs = []
23+
for example in dataset:
24+
spec = make_spec(example)
25+
specs.append(spec)
26+
27+
client = docker.from_env()
28+
build_repo_images(client, specs)
29+
logger.info("Done building docker images")
30+
31+
32+
def add_init_args(parser: argparse.ArgumentParser) -> None:
33+
parser.add_argument(
34+
"--hf_name",
35+
type=str,
36+
help="HF dataset name",
37+
default="wentingzhao/commit0_docstring",
38+
)
39+
parser.add_argument(
40+
"--base_dir",
41+
type=str,
42+
default="repos/",
43+
help="base directory to write repos to",
44+
)
45+
parser.add_argument(
46+
"--config_file",
47+
type=str,
48+
default="config.yml",
49+
help="where to write config file to",
50+
)
51+
parser.set_defaults(func=run)
52+
53+
54+
def run(args: argparse.Namespace) -> None:
55+
main(
56+
hf_name=args.hf_name,
57+
base_dir=args.base_dir,
58+
config_file=args.config_file,
59+
)
60+
61+
62+
__all__ = []

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__ = []

0 commit comments

Comments
 (0)