Skip to content
Merged

Lint #55

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions commit0/cli.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"],
)

Expand Down
42 changes: 24 additions & 18 deletions commit0/harness/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -35,7 +35,11 @@


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
Expand All @@ -49,22 +53,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(Path(os.path.join(root, file)))

config_file = Path(".commit0.pre-commit-config.yaml")
if not config_file.is_file():
Expand All @@ -75,7 +80,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:
Expand Down