From fd62595957a73a763006f2288d8ab71c58c9a6f9 Mon Sep 17 00:00:00 2001 From: Wenting Zhao Date: Sun, 22 Sep 2024 20:07:19 +0000 Subject: [PATCH 1/2] make lint aider compatible --- commit0/cli.py | 24 ++++++++++++++++++++++-- commit0/harness/lint.py | 38 ++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/commit0/cli.py b/commit0/cli.py index 61670cd..3ad212f 100644 --- a/commit0/cli.py +++ b/commit0/cli.py @@ -1,6 +1,6 @@ import typer from pathlib import Path -from typing import Union +from typing import Union, List from typing_extensions import Annotated import commit0.harness.run_pytest_ids import commit0.harness.get_pytest_ids @@ -328,19 +328,39 @@ 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_dot_file_path: 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_dot_file(commit0_dot_file_path) - typer.echo(f"Linting repo: {highlight(str(repo_or_repo_dir), Colors.ORANGE)}") + 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"], ) diff --git a/commit0/harness/lint.py b/commit0/harness/lint.py index d073fd5..6167af4 100644 --- a/commit0/harness/lint.py +++ b/commit0/harness/lint.py @@ -3,7 +3,7 @@ import os from datasets import load_dataset from pathlib import Path -from typing import Iterator +from typing import Iterator, Union, List from commit0.harness.constants import ( RepoInstance, @@ -35,7 +35,7 @@ def main( - dataset_name: str, dataset_split: str, repo_or_repo_dir: str, base_dir: str + dataset_name: str, dataset_split: str, repo_or_repo_dir: str, files: Union[List[Path], None], base_dir: str ) -> None: dataset: Iterator[RepoInstance] = load_dataset(dataset_name, split=dataset_split) # type: ignore example = None @@ -49,22 +49,23 @@ def main( assert example is not None, "No example available" assert repo_name is not None, "No repo available" - repo_dir = os.path.join(base_dir, repo_name) - if os.path.isdir(repo_or_repo_dir): - repo = repo_or_repo_dir - elif os.path.isdir(repo_dir): - repo = repo_dir - else: - raise Exception( - f"Neither {repo_dir} nor {repo_or_repo_dir} is a valid path.\nUsage: commit0 lint {{repo_or_repo_dir}}" - ) + if files is None: + repo_dir = os.path.join(base_dir, repo_name) + if os.path.isdir(repo_or_repo_dir): + repo = repo_or_repo_dir + elif os.path.isdir(repo_dir): + repo = repo_dir + else: + raise Exception( + f"Neither {repo_dir} nor {repo_or_repo_dir} is a valid path.\nUsage: commit0 lint {{repo_or_repo_dir}}" + ) - files = [] - repo = os.path.join(repo, example["src_dir"]) - for root, dirs, fs in os.walk(repo): - for file in fs: - if file.endswith(".py"): - files.append(os.path.join(root, file)) + files = [] + repo = os.path.join(repo, example["src_dir"]) + for root, dirs, fs in os.walk(repo): + for file in fs: + if file.endswith(".py"): + files.append(os.path.join(root, file)) config_file = Path(".commit0.pre-commit-config.yaml") if not config_file.is_file(): @@ -75,7 +76,8 @@ def main( print(result.stdout) sys.exit(result.returncode) except subprocess.CalledProcessError as e: - raise Exception(f"Pre-commit checks failed\n{e.output}") + print(e.output) + sys.exit(e.returncode) except FileNotFoundError: raise FileNotFoundError("Error: pre-commit command not found. Is it installed?") except Exception as e: From 4b564317538da08f8d5b596e2ec548bcc4fd4ffe Mon Sep 17 00:00:00 2001 From: Wenting Zhao Date: Sun, 22 Sep 2024 20:08:08 +0000 Subject: [PATCH 2/2] pre-comit fixes --- commit0/harness/lint.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/commit0/harness/lint.py b/commit0/harness/lint.py index 6167af4..5591481 100644 --- a/commit0/harness/lint.py +++ b/commit0/harness/lint.py @@ -35,7 +35,11 @@ def main( - dataset_name: str, dataset_split: str, repo_or_repo_dir: str, files: Union[List[Path], None], base_dir: str + dataset_name: str, + dataset_split: str, + repo_or_repo_dir: str, + files: Union[List[Path], None], + base_dir: str, ) -> None: dataset: Iterator[RepoInstance] = load_dataset(dataset_name, split=dataset_split) # type: ignore example = None @@ -65,7 +69,7 @@ def main( for root, dirs, fs in os.walk(repo): for file in fs: if file.endswith(".py"): - files.append(os.path.join(root, file)) + files.append(Path(os.path.join(root, file))) config_file = Path(".commit0.pre-commit-config.yaml") if not config_file.is_file():