Skip to content

Commit

Permalink
Run new ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
hofbi committed Mar 28, 2024
1 parent e0fff0a commit 5518d92
Show file tree
Hide file tree
Showing 53 changed files with 321 additions and 470 deletions.
18 changes: 9 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ repos:
args: [--mapping=2, --sequence=4, --offset=2, --implicit_start, --preserve-quotes]
exclude: (?x)( .gitlab-ci.yml | .clang-format | .clang-tidy )
- repo: https://github.com/adrienverge/yamllint
rev: v1.33.0
rev: v1.35.1
hooks:
- id: yamllint
args: [--strict]
Expand All @@ -83,7 +83,7 @@ repos:
- id: docformatter
additional_dependencies: [tomli]
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v17.0.6
rev: v18.1.2
hooks:
- id: clang-format
- repo: https://github.com/cpplint/cpplint
Expand All @@ -99,7 +99,7 @@ repos:
additional_dependencies: [cmakelang]
exclude: cmake/.*
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
rev: v0.3.4
hooks:
- id: ruff
- id: ruff-format
Expand All @@ -112,28 +112,28 @@ repos:
- flake8-bugbear # Ruff does not implement all of bugbear
- flake8-requirements # Not yet implemented https://github.com/astral-sh/ruff/issues/4100
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
rev: v1.9.0
hooks:
- id: mypy
args: [--python-version, '3.10']
- repo: https://github.com/PyCQA/pylint
rev: v3.0.3
rev: v3.1.0
hooks:
- id: pylint
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
rev: v3.15.2
hooks:
- id: pyupgrade
args: [--py310-plus, --keep-runtime-typing]
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.9.0.6
rev: v0.10.0.1
hooks:
- id: shellcheck
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.27.4
rev: 0.28.0
hooks:
- id: check-github-workflows
- repo: https://github.com/crate-ci/typos
rev: v1.18.1
rev: v1.19.0
hooks:
- id: typos
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ disable = [
[tool.ruff]
fix = true
target-version = "py310"
line-length = 120

[tool.ruff.lint]
select = ["ALL"]
ignore = [
"A",
Expand All @@ -67,6 +70,7 @@ ignore = [
"FBT",
"FIX",
"INP",
"ISC001", # Done via formatter
"PERF203",
"PT", # We don't use pytest here
"S",
Expand Down
7 changes: 4 additions & 3 deletions tools/create_gitlab_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from argparse import Namespace

from sel_tools.config import REPO_DIR
from sel_tools.file_export.solutions_check import check_code_for_solutions_code
from sel_tools.gitlab_api.create_repo import (
create_repos,
store_student_repo_info_to_config_file,
Expand All @@ -20,23 +21,23 @@ def parse_arguments(arguments: list[str]) -> Namespace:
factory.add_source_folder(REPO_DIR / "export" / "homework")
factory.add_number_of_repos()
factory.add_gitlab_token()
factory.add_publish_solutions()

return factory.parser.parse_args(arguments[1:])


def main() -> None:
"""main."""
arguments = parse_arguments(sys.argv)
check_code_for_solutions_code(arguments.source_path, arguments.publish_solutions)
student_repos, group_name = create_repos(
arguments.source_path,
arguments.repo_base_name,
arguments.group_id,
arguments.number_of_repos,
arguments.gitlab_token,
)
store_student_repo_info_to_config_file(
arguments.repo_info_dir, group_name, student_repos
)
store_student_repo_info_to_config_file(arguments.repo_info_dir, group_name, student_repos)


if __name__ == "__main__":
Expand Down
28 changes: 7 additions & 21 deletions tools/gitlab_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,15 @@ def edit_comment_issue(args: Namespace) -> None:

def edit_fetch_code(args: Namespace) -> None:
"""Default action for fetch_code subcommand."""
fetch_repos(
args.workspace, Path(args.student_repo_info_file.name), args.gitlab_token
)
fetch_repos(args.workspace, Path(args.student_repo_info_file.name), args.gitlab_token)


def edit_evaluate_code(args: Namespace) -> None:
"""Default action for evaluate_code subcommand."""
gitlab_projects = fetch_repos(
args.workspace, Path(args.student_repo_info_file.name), args.gitlab_token
)
gitlab_projects = fetch_repos(args.workspace, Path(args.student_repo_info_file.name), args.gitlab_token)
factory = EvaluationJobFactory.load_factory_from_file(args.job_factory)
evaluation_reports = evaluate_code(
factory, gitlab_projects, args.homework_number, args.evaluation_date
)
write_evaluation_reports(
evaluation_reports, f"homework-{args.homework_number}-report"
)
evaluation_reports = evaluate_code(factory, gitlab_projects, args.homework_number, args.evaluation_date)
write_evaluation_reports(evaluation_reports, f"homework-{args.homework_number}-report")
diff_reports = create_diff(
[project.local_path for project in gitlab_projects],
args.date_last_homework,
Expand All @@ -63,16 +55,12 @@ def edit_evaluate_code(args: Namespace) -> None:

def edit_upload_files(args: Namespace) -> None:
"""Default action for upload_files subcommand."""
upload_files(
args.source_path, Path(args.student_repo_info_file.name), args.gitlab_token
)
upload_files(args.source_path, Path(args.student_repo_info_file.name), args.gitlab_token)


def edit_commit_changes(args: Namespace) -> None:
"""Default action for commit_changes subcommand."""
gitlab_projects = fetch_repos(
args.workspace, Path(args.student_repo_info_file.name), args.gitlab_token
)
gitlab_projects = fetch_repos(args.workspace, Path(args.student_repo_info_file.name), args.gitlab_token)
student_repos = [project.local_path for project in gitlab_projects]
export_items(args.source_path, student_repos, args.keep_solutions)
commit_changes(student_repos, args.message)
Expand All @@ -92,9 +80,7 @@ def parse_arguments(arguments: list[str]) -> Namespace:
# pylint: disable=too-many-locals

parser = ArgumentParserFactory.default_parser(__doc__).parser
subparsers = parser.add_subparsers(
title="actions", dest="actions", help="sub-command help", required=True
)
subparsers = parser.add_subparsers(title="actions", dest="actions", help="sub-command help", required=True)

# Common arguments
factory = ArgumentParserFactory.parent_parser()
Expand Down
18 changes: 4 additions & 14 deletions tools/sel_tools/code_evaluation/evaluate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ def evaluate_code(
evaluation_jobs = eval_job_factory.create(gitlab_projects, homework_number)
return [
CodeEvaluator(evaluation_jobs, gitlab_project).evaluate(evaluation_date)
for gitlab_project in tqdm(
gitlab_projects, desc=f"Evaluating Homework {homework_number}"
)
for gitlab_project in tqdm(gitlab_projects, desc=f"Evaluating Homework {homework_number}")
]


class CodeEvaluator:
"""Code evaluator class."""

def __init__(
self, jobs: list[EvaluationJob], gitlab_project: GitlabProject
) -> None:
def __init__(self, jobs: list[EvaluationJob], gitlab_project: GitlabProject) -> None:
# Perform a deepcopy to avoid artifact of old job runs
self.__jobs = copy.deepcopy(jobs)
self.__gitlab_project = gitlab_project
Expand All @@ -46,21 +42,15 @@ def evaluate(self, evaluation_date: date | None) -> EvaluationReport:
self.__checkout_last_commit_before_eval_date(evaluation_date)
return EvaluationReport(
self.__gitlab_project,
list(
itertools.chain(
*[job.run(self.__gitlab_project.local_path) for job in self.__jobs]
)
),
list(itertools.chain(*[job.run(self.__gitlab_project.local_path) for job in self.__jobs])),
)

def __clean_repo(self) -> None:
self.__repo.git.restore(".")
self.__repo.git.clean("-xdf")

def __checkout_last_commit_before_eval_date(self, evaluation_date: date) -> None:
commits_before_eval_date = list(
self.__repo.iter_commits(before=evaluation_date)
)
commits_before_eval_date = list(self.__repo.iter_commits(before=evaluation_date))
if commits_before_eval_date:
self.__repo.git.checkout(commits_before_eval_date[0])
self.__clean_repo()
10 changes: 3 additions & 7 deletions tools/sel_tools/code_evaluation/jobs/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ class CodeCoverageTestJob(EvaluationJob):
"""Job for checking the code coverage."""

name = "Code Coverage"
dependencies: ClassVar[list[EvaluationJob]] = [
CMakeBuildJob(cmake_options="-DCMAKE_BUILD_TYPE=Debug")
]
dependencies: ClassVar[list[EvaluationJob]] = [CMakeBuildJob(cmake_options="-DCMAKE_BUILD_TYPE=Debug")]

def __init__(self, weight: int = 1, min_coverage: int = 75) -> None:
super().__init__(weight)
Expand All @@ -87,7 +85,7 @@ def parse_total_coverage(coverage_file: Path) -> int:
return int(coverage.group(1)) if coverage else 0

def _run(self, repo_path: Path) -> int:
coverage_file = repo_path / HW_BUILD_FOLDER / "report.txt"
coverage_file = repo_path.resolve() / HW_BUILD_FOLDER / "report.txt"
score = run_shell_command(f"gcovr -o {coverage_file}", repo_path)
if score == 0:
self._comment = "Coverage failed"
Expand Down Expand Up @@ -175,9 +173,7 @@ def visit_file(self, file: Path) -> None:
self.__is_clean = self.__is_clean and (
file.name not in self.__dirty_file_names
and file.suffix not in self.__dirty_suffixes
and all(
directory not in str(file) for directory in self.__dirty_directories
)
and all(directory not in str(file) for directory in self.__dirty_directories)
)

class SourceFilesCountVisitor(FileVisitor):
Expand Down
9 changes: 2 additions & 7 deletions tools/sel_tools/code_evaluation/jobs/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ class EvaluationJobFactory:

@staticmethod
@abstractmethod
def create(
gitlab_projects: list[GitlabProject], homework_number: int
) -> list[EvaluationJob]:
def create(gitlab_projects: list[GitlabProject], homework_number: int) -> list[EvaluationJob]:
msg = "Don't call me, I'm abstract."
raise NotImplementedError(msg)

Expand All @@ -52,10 +50,7 @@ def load_factory_from_file(module_path: Path) -> type["EvaluationJobFactory"]:
# Feel free to adapt the below conditions to your use case
if name.startswith("_") or not inspect.isclass(attribute):
continue
if (
issubclass(attribute, EvaluationJobFactory)
and name != "EvaluationJobFactory"
):
if issubclass(attribute, EvaluationJobFactory) and name != "EvaluationJobFactory":
return attribute
msg = f"No subclass of EvaluationJobFactory in {module_path}"
raise ModuleNotFoundError(msg)
5 changes: 1 addition & 4 deletions tools/sel_tools/code_evaluation/jobs/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ def __init__(
weight: int = 1,
) -> None:
super().__init__(weight)
self.__gitlab_projects = {
project.local_path.stem: project.gitlab_project
for project in gitlab_projects
}
self.__gitlab_projects = {project.local_path.stem: project.gitlab_project for project in gitlab_projects}
self.__branch = branch

def _run(self, repo_path: Path) -> int:
Expand Down
8 changes: 2 additions & 6 deletions tools/sel_tools/code_evaluation/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class EvaluationResult:
class EvaluationReport:
"""Evaluation report."""

def __init__(
self, gitlab_project: GitlabProject, results: list[EvaluationResult]
) -> None:
def __init__(self, gitlab_project: GitlabProject, results: list[EvaluationResult]) -> None:
self.repo_path = gitlab_project.local_path
self.url = gitlab_project.gitlab_project.web_url
self.score = sum(result.score for result in set(results))
Expand All @@ -59,9 +57,7 @@ def to_md(self) -> str:
return MD_EVALUATION_REPORT % (self.url, self.to_json())


def write_evaluation_reports(
reports: list[EvaluationReport], report_base_name: str
) -> None:
def write_evaluation_reports(reports: list[EvaluationReport], report_base_name: str) -> None:
"""Write evaluation reports to disk."""
for report in reports:
report_path = report.repo_path / report_base_name
Expand Down
12 changes: 3 additions & 9 deletions tools/sel_tools/diff_creation/create_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def create_diff(
if date_last_homework is None
else [
DiffCreator(repo_path, "build").create(date_last_homework, evaluation_date)
for repo_path in tqdm(
repo_paths, desc=f"Create diff since {date_last_homework}"
)
for repo_path in tqdm(repo_paths, desc=f"Create diff since {date_last_homework}")
]
)

Expand All @@ -36,13 +34,9 @@ def __init__(self, repo_path: Path, exclude: str | None = None) -> None:
self.__repo = git.Repo(self.__path)
self.__exclude = exclude

def create(
self, date_last_homework: date, evaluation_date: date | None
) -> DiffReport:
def create(self, date_last_homework: date, evaluation_date: date | None) -> DiffReport:
commits_since_last_homework = list(
self.__repo.iter_commits(
since=date_last_homework, before=evaluation_date, no_merges=True
)
self.__repo.iter_commits(since=date_last_homework, before=evaluation_date, no_merges=True)
)
diffs = (
[
Expand Down
8 changes: 2 additions & 6 deletions tools/sel_tools/diff_creation/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ def write_diff_patches(self) -> None:

@staticmethod
def highlight_diff(patch: str) -> str:
return str(
highlight(patch, DiffLexer(), HtmlFormatter(full=True, style="manni"))
)
return str(highlight(patch, DiffLexer(), HtmlFormatter(full=True, style="manni")))


def write_diff_reports(reports: list[DiffReport], report_base_name: str) -> None:
"""Write diff reports to disk."""
for report in reports:
report.generate_overview_table().to_csv(
report.repo_path.joinpath(report_base_name).with_suffix(".csv")
)
report.generate_overview_table().to_csv(report.repo_path.joinpath(report_base_name).with_suffix(".csv"))
report.write_diff_patches()
12 changes: 2 additions & 10 deletions tools/sel_tools/file_export/copy_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ def get_ignored_items_from(folder: Path) -> set[Path]:
ignore_file_lines = ignore_file.read_text().splitlines()
non_empty_lines = list(filter(None, ignore_file_lines))
non_empty_lines.append(f"**/{EXPORT_IGNORE}")
ignore_list = [
file
for ignore_file_line in non_empty_lines
for file in folder.glob(ignore_file_line)
]
ignore_list = [file for ignore_file_line in non_empty_lines for file in folder.glob(ignore_file_line)]

return {item.resolve() for item in ignore_list}

Expand All @@ -45,10 +41,6 @@ def ignore_files(ignore_set: set[Path]) -> Callable:

def ignore_callable(directory: str, contents: list[str]) -> list[str]:
"""Callable for ignoring files with shutil.copytree."""
return [
item
for item in contents
if Path(directory).joinpath(item).resolve() in ignore_set
]
return [item for item in contents if Path(directory).joinpath(item).resolve() in ignore_set]

return ignore_callable
Loading

0 comments on commit 5518d92

Please sign in to comment.