2024-04-30 00:37:50 +02:00
|
|
|
#!/usr/bin/env python3
|
2024-10-02 10:23:23 +02:00
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
from collections.abc import Iterable
|
|
|
|
from fnmatch import fnmatch
|
|
|
|
from pathlib import Path
|
2024-04-30 00:37:50 +02:00
|
|
|
|
2024-10-02 10:23:23 +02:00
|
|
|
from shared.files import find_versioned_files, is_binary_file
|
2025-01-15 14:13:32 +01:00
|
|
|
from shared.linting import LintContext, lint_repo, lint_bulk_files, lint_file
|
2024-10-29 10:47:57 +01:00
|
|
|
from shared.paths import REPO_DIR
|
2024-10-02 10:23:23 +02:00
|
|
|
|
2025-02-11 22:19:01 +01:00
|
|
|
IGNORED_PATTERNS = ["*.patch", "*.bin"]
|
2024-10-02 10:23:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("path", type=Path, nargs="*")
|
|
|
|
parser.add_argument("-D", "--debug", action="store_true")
|
2025-01-15 14:13:32 +01:00
|
|
|
parser.add_argument("-a", "--all", action="store_true")
|
2024-10-02 10:23:23 +02:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def filter_files(
|
|
|
|
files: Iterable[Path], ignored_patterns: list[str] | None, debug: bool
|
|
|
|
) -> Iterable[Path]:
|
|
|
|
for path in files:
|
2025-04-22 10:34:36 +02:00
|
|
|
if not path.exists():
|
|
|
|
continue
|
2024-10-29 10:47:57 +01:00
|
|
|
if path.is_dir():
|
|
|
|
continue
|
2024-10-02 10:23:23 +02:00
|
|
|
if is_binary_file(path):
|
|
|
|
if debug:
|
|
|
|
print(f"{path} is a binary file, ignoring", file=sys.stderr)
|
|
|
|
continue
|
|
|
|
if ignored_patterns and any(
|
|
|
|
fnmatch(path.name, pattern) for pattern in ignored_patterns
|
|
|
|
):
|
|
|
|
if debug:
|
|
|
|
print(
|
|
|
|
f"{path} has a prohibited extension, ignoring",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
yield path
|
|
|
|
|
|
|
|
|
2024-10-29 10:47:57 +01:00
|
|
|
def main(root_dir: Path) -> None:
|
2024-10-02 10:23:23 +02:00
|
|
|
args = parse_args()
|
|
|
|
|
|
|
|
context = LintContext(
|
2024-10-29 10:47:57 +01:00
|
|
|
root_dir=root_dir,
|
2025-01-15 14:13:32 +01:00
|
|
|
versioned_files=list(find_versioned_files(root_dir=REPO_DIR)),
|
2024-10-02 10:23:23 +02:00
|
|
|
)
|
|
|
|
if args.path:
|
|
|
|
files = args.path
|
|
|
|
else:
|
|
|
|
files = context.versioned_files
|
|
|
|
files = list(
|
|
|
|
filter_files(
|
|
|
|
files, ignored_patterns=IGNORED_PATTERNS, debug=args.debug
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
exit_code = 0
|
|
|
|
for file in files:
|
|
|
|
if args.debug:
|
|
|
|
print(f"Checking {file}...", file=sys.stderr)
|
|
|
|
for lint_warning in lint_file(context, file):
|
|
|
|
print(str(lint_warning), file=sys.stderr)
|
|
|
|
exit_code = 1
|
|
|
|
|
|
|
|
if args.debug:
|
|
|
|
print(f"Checking files in bulk {file}...", file=sys.stderr)
|
|
|
|
for lint_warning in lint_bulk_files(context, files):
|
|
|
|
print(str(lint_warning), file=sys.stderr)
|
|
|
|
exit_code = 1
|
|
|
|
|
2025-01-15 14:13:32 +01:00
|
|
|
if args.all:
|
|
|
|
if args.debug:
|
|
|
|
print(f"Checking for repository-wide warnings...", file=sys.stderr)
|
|
|
|
for lint_warning in lint_repo(context):
|
|
|
|
print(str(lint_warning), file=sys.stderr)
|
|
|
|
exit_code = 1
|
|
|
|
|
2024-10-02 10:23:23 +02:00
|
|
|
exit(exit_code)
|
2024-10-29 10:47:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(root_dir=REPO_DIR)
|