-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcli.py
More file actions
468 lines (418 loc) · 15.5 KB
/
cli.py
File metadata and controls
468 lines (418 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import typer
from pathlib import Path
from typing import Union, List
from typing_extensions import Annotated
import commit0.harness.batch_run_pytest_ids
import commit0.harness.run_pytest_ids
import commit0.harness.get_pytest_ids
import commit0.harness.build
import commit0.harness.setup
import commit0.harness.evaluate
import commit0.harness.lint
import commit0.harness.save
from commit0.harness.constants import SPLIT, SPLIT_ALL
from commit0.harness.utils import get_active_branch
import subprocess
import yaml
import os
import sys
commit0_app = typer.Typer(
no_args_is_help=True,
add_completion=False,
context_settings={"help_option_names": ["-h", "--help"]},
help="""
Commit-0 is a real-world AI coding challenge. Can your agent generate a working library from commit 0?
See the website at https://commit-0.github.io/ for documentation and more information about Commit-0.
""",
)
class Colors:
RESET = "\033[0m"
RED = "\033[91m"
YELLOW = "\033[93m"
CYAN = "\033[96m"
ORANGE = "\033[95m"
def check_commit0_path() -> None:
"""Code adapted from https://github.com/modal-labs/modal-client/blob/a8ddd418f8c65b7e168a9125451eeb70da2b6203/modal/cli/entry_point.py#L55
Checks whether the `commit0` executable is on the path and usable.
"""
url = "https://commit-0.github.io/setup/"
try:
subprocess.run(["commit0", "--help"], capture_output=True)
# TODO(erikbern): check returncode?
return
except FileNotFoundError:
typer.echo(
typer.style(
"The `commit0` command was not found on your path!", fg=typer.colors.RED
)
+ "\n"
+ typer.style(
"You may need to add it to your path or use `python -m commit0` as a workaround.",
fg=typer.colors.RED,
)
)
except PermissionError:
typer.echo(
typer.style("The `commit0` command is not executable!", fg=typer.colors.RED)
+ "\n"
+ typer.style(
"You may need to give it permissions or use `python -m commit0` as a workaround.",
fg=typer.colors.RED,
)
)
typer.echo(f"See more information here:\n\n{url}")
typer.echo("─" * 80) # Simple rule to separate content
def highlight(text: str, color: str) -> str:
"""Highlight text with a color."""
return f"{color}{text}{Colors.RESET}"
def check_valid(one: str, total: Union[list[str], dict[str, list[str]]]) -> None:
if isinstance(total, dict):
total = list(total.keys())
if one not in total:
valid = ", ".join([highlight(key, Colors.ORANGE) for key in total])
raise typer.BadParameter(
f"Invalid {highlight('REPO_OR_REPO_SPLIT', Colors.RED)}. Must be one of: {valid}",
param_hint="REPO or REPO_SPLIT",
)
def write_commit0_config_file(dot_file_path: str, config: dict) -> None:
with open(dot_file_path, "w") as f:
yaml.dump(config, f, default_flow_style=False)
def read_commit0_config_file(dot_file_path: str) -> dict:
# Check if the file exists before attempting to read it
if not os.path.exists(dot_file_path):
raise FileNotFoundError(
f"The commit0 dot file '{dot_file_path}' does not exist."
)
with open(dot_file_path, "r") as f:
return yaml.load(f, Loader=yaml.FullLoader)
@commit0_app.command()
def setup(
repo_split: str = typer.Argument(
...,
help=f"Split of repositories, one of: {', '.join([highlight(key, Colors.ORANGE) for key in SPLIT.keys()])}",
),
dataset_name: str = typer.Option(
"wentingzhao/commit0_combined", help="Name of the Huggingface dataset"
),
dataset_split: str = typer.Option("test", help="Split of the Huggingface dataset"),
base_dir: str = typer.Option("repos/", help="Base directory to clone repos to"),
commit0_config_file: str = typer.Option(
".commit0.yaml", help="Storing path for stateful commit0 configs"
),
) -> None:
"""Commit0 clone a repo split."""
check_commit0_path()
if "commit0" in dataset_name.split("/")[-1].lower():
check_valid(repo_split, SPLIT)
base_dir = str(Path(base_dir).resolve())
typer.echo(f"Cloning repository for split: {highlight(repo_split, Colors.ORANGE)}")
typer.echo(f"Dataset name: {highlight(dataset_name, Colors.ORANGE)}")
typer.echo(f"Dataset split: {highlight(dataset_split, Colors.ORANGE)}")
typer.echo(f"Base directory: {highlight(base_dir, Colors.ORANGE)}")
typer.echo(
f"Commit0 dot file path: {highlight(commit0_config_file, Colors.ORANGE)}"
)
commit0.harness.setup.main(
dataset_name,
dataset_split,
repo_split,
base_dir,
)
# after successfully setup, write the commit0 dot file
write_commit0_config_file(
commit0_config_file,
{
"dataset_name": dataset_name,
"dataset_split": dataset_split,
"repo_split": repo_split,
"base_dir": base_dir,
},
)
@commit0_app.command()
def build(
num_workers: int = typer.Option(8, help="Number of workers"),
commit0_config_file: str = typer.Option(
".commit0.yaml",
help="Path to the commit0 dot file, where the setup config is stored",
),
verbose: int = typer.Option(
1,
"--verbose",
"-v",
help="Set this to 2 for more logging information",
count=True,
),
) -> None:
"""Build Commit0 split you choose in Setup Stage."""
check_commit0_path()
commit0_config = read_commit0_config_file(commit0_config_file)
if "commit0" in commit0_config["dataset_name"].split("/")[-1].lower():
check_valid(commit0_config["repo_split"], SPLIT)
typer.echo(
f"Building repository for split: {highlight(commit0_config['repo_split'], Colors.ORANGE)}"
)
typer.echo(
f"Dataset name: {highlight(commit0_config['dataset_name'], Colors.ORANGE)}"
)
typer.echo(
f"Dataset split: {highlight(commit0_config['dataset_split'], Colors.ORANGE)}"
)
typer.echo(f"Number of workers: {highlight(str(num_workers), Colors.ORANGE)}")
commit0.harness.build.main(
commit0_config["dataset_name"],
commit0_config["dataset_split"],
commit0_config["repo_split"],
num_workers,
verbose,
)
@commit0_app.command()
def get_tests(
repo_name: str = typer.Argument(
...,
help=f"Name of the repository to get tests for, one of: {', '.join(highlight(key, Colors.ORANGE) for key in SPLIT_ALL)}",
),
) -> None:
"""Get tests for a Commit0 repository."""
check_commit0_path()
commit0.harness.get_pytest_ids.main(repo_name, verbose=1)
@commit0_app.command()
def test(
repo_or_repo_path: str = typer.Argument(
..., help="Directory of the repository to test"
),
test_ids: str = typer.Argument(
None,
help='All ways pytest supports to run and select tests. Please provide a single string. Example: "test_mod.py", "testing/", "test_mod.py::test_func", "-k \'MyClass and not method\'"',
),
branch: Union[str, None] = typer.Option(
None, help="Branch to test (branch MUST be provided or use --reference)"
),
backend: str = typer.Option("modal", help="Backend to use for testing"),
timeout: int = typer.Option(1800, help="Timeout for tests in seconds"),
num_cpus: int = typer.Option(1, help="Number of CPUs to use"),
reference: Annotated[
bool, typer.Option("--reference", help="Test the reference commit")
] = False,
coverage: Annotated[
bool, typer.Option("--coverage", help="Whether to get coverage information")
] = False,
rebuild: bool = typer.Option(
False, "--rebuild", help="Whether to rebuild an image"
),
commit0_config_file: str = typer.Option(
".commit0.yaml",
help="Path to the commit0 dot file, where the setup config is stored",
),
verbose: int = typer.Option(
1,
"--verbose",
"-v",
help="Set this to 2 for more logging information",
count=True,
),
stdin: bool = typer.Option(
False,
"--stdin",
help="Read test names from stdin. Example: `echo 'test_mod.py' | commit0 test REPO --branch BRANCH`",
),
) -> None:
"""Run tests on a Commit0 repository."""
check_commit0_path()
commit0_config = read_commit0_config_file(commit0_config_file)
if repo_or_repo_path.endswith("/"):
repo_or_repo_path = repo_or_repo_path[:-1]
if "commit0" in commit0_config["dataset_name"].split("/")[-1].lower():
check_valid(repo_or_repo_path.split("/")[-1], SPLIT)
if reference:
branch = "reference"
else:
dataset_name = commit0_config["dataset_name"].lower()
if (
"humaneval" in dataset_name
or "mbpp" in dataset_name
or "bigcodebench" in dataset_name
or "codecontests" in dataset_name
):
branch = repo_or_repo_path
else:
if branch is None and not reference:
git_path = os.path.join(
commit0_config["base_dir"], repo_or_repo_path.split("/")[-1]
)
branch = get_active_branch(git_path)
if stdin:
# Read test names from stdin
test_ids = sys.stdin.read()
elif test_ids is None:
typer.echo("Error: test_ids must be provided or use --stdin option", err=True)
raise typer.Exit(code=1)
if verbose == 2:
typer.echo(f"Running tests for repository: {repo_or_repo_path}")
typer.echo(f"Branch: {branch}")
typer.echo(f"Test IDs: {test_ids}")
commit0.harness.run_pytest_ids.main(
commit0_config["dataset_name"],
commit0_config["dataset_split"],
commit0_config["base_dir"],
repo_or_repo_path,
branch, # type: ignore
test_ids,
coverage,
backend,
timeout,
num_cpus,
rebuild,
verbose,
)
@commit0_app.command()
def batch_test(
test_ids: str = typer.Argument(
None,
help='All ways pytest supports to run and select tests. Please provide a single string. Example: "test_mod.py", "testing/", "test_mod.py::test_func", "-k \'MyClass and not method\'"',
),
backend: str = typer.Option("modal", help="Backend to use for testing"),
timeout: int = typer.Option(1800, help="Timeout for tests in seconds"),
num_cpus: int = typer.Option(1, help="Number of CPUs to use"),
reference: Annotated[
bool, typer.Option("--reference", help="Test the reference commit")
] = False,
coverage: Annotated[
bool, typer.Option("--coverage", help="Whether to get coverage information")
] = False,
rebuild: bool = typer.Option(
False, "--rebuild", help="Whether to rebuild an image"
),
commit0_config_file: str = typer.Option(
".commit0.yaml",
help="Path to the commit0 dot file, where the setup config is stored",
),
verbose: int = typer.Option(
1,
"--verbose",
"-v",
help="Set this to 2 for more logging information",
count=True,
),
) -> None:
"""Run tests on a Commit0 repository."""
commit0.harness.batch_run_pytest_ids.main(
test_ids,
reference,
coverage,
backend,
timeout,
num_cpus,
rebuild,
verbose,
)
@commit0_app.command()
def evaluate(
branch: Union[str, None] = typer.Option(
None, help="Branch to evaluate (branch MUST be provided or use --reference)"
),
backend: str = typer.Option("modal", help="Backend to use for evaluation"),
timeout: int = typer.Option(1800, help="Timeout for evaluation in seconds"),
num_cpus: int = typer.Option(1, help="Number of CPUs to use"),
num_workers: int = typer.Option(8, help="Number of workers to use"),
reference: Annotated[
bool, typer.Option("--reference", help="Evaluate the reference commit.")
] = False,
coverage: Annotated[
bool, typer.Option("--coverage", help="Whether to get coverage information")
] = False,
commit0_config_file: str = typer.Option(
".commit0.yaml",
help="Path to the commit0 dot file, where the setup config is stored",
),
rebuild: bool = typer.Option(False, "--rebuild", help="Whether to rebuild images"),
) -> None:
"""Evaluate Commit0 split you choose in Setup Stage."""
check_commit0_path()
if reference:
branch = "reference"
commit0_config = read_commit0_config_file(commit0_config_file)
if "commit0" in commit0_config["dataset_name"].split("/")[-1].lower():
check_valid(commit0_config["repo_split"], SPLIT)
typer.echo(f"Evaluating repository split: {commit0_config['repo_split']}")
typer.echo(f"Branch: {branch}")
commit0.harness.evaluate.main(
commit0_config["dataset_name"],
commit0_config["dataset_split"],
commit0_config["repo_split"],
commit0_config["base_dir"],
branch,
coverage,
backend,
timeout,
num_cpus,
num_workers,
rebuild,
)
@commit0_app.command()
def lint(
repo_or_repo_dir: str = typer.Argument(
..., help="Directory of the repository to test"
),
files: Union[List[Path], None] = typer.Option(
None, help="Files to lint. If not provided, all files will be linted."
),
commit0_config_file: str = typer.Option(
".commit0.yaml",
help="Path to the commit0 dot file, where the setup config is stored",
),
verbose: int = typer.Option(
1,
"--verbose",
"-v",
help="Set this to 2 for more logging information",
count=True,
),
) -> None:
"""Lint given files if provided, otherwise lint all files in the base directory."""
check_commit0_path()
commit0_config = read_commit0_config_file(commit0_config_file)
appended_files = None
if files is not None:
appended_files = []
for path in files:
path = Path(commit0_config["base_dir"]) / Path(repo_or_repo_dir) / path
if not path.is_file():
raise FileNotFoundError(f"File not found: {str(path)}")
appended_files.append(path)
if verbose == 2:
typer.echo(f"Linting repo: {highlight(str(repo_or_repo_dir), Colors.ORANGE)}")
commit0.harness.lint.main(
commit0_config["dataset_name"],
commit0_config["dataset_split"],
repo_or_repo_dir,
appended_files,
commit0_config["base_dir"],
)
@commit0_app.command()
def save(
owner: str = typer.Argument(..., help="Owner of the repository"),
branch: str = typer.Argument(..., help="Branch to save"),
github_token: str = typer.Option(None, help="GitHub token for authentication"),
commit0_config_file: str = typer.Option(
".commit0.yaml",
help="Path to the commit0 dot file, where the setup config is stored",
),
) -> None:
"""Save Commit0 split you choose in Setup Stage to GitHub."""
check_commit0_path()
commit0_config = read_commit0_config_file(commit0_config_file)
if "commit0" in commit0_config["dataset_name"].split("/")[-1].lower():
check_valid(commit0_config["repo_split"], SPLIT)
typer.echo(f"Saving repository split: {commit0_config['repo_split']}")
typer.echo(f"Owner: {owner}")
typer.echo(f"Branch: {branch}")
commit0.harness.save.main(
commit0_config["dataset_name"],
commit0_config["dataset_split"],
commit0_config["repo_split"],
commit0_config["base_dir"],
owner,
branch,
github_token,
)
__all__ = []