Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
python-version: '3.12'

- name: Install system dependencies
run: |
Expand All @@ -40,4 +40,4 @@ jobs:

- name: Run unit tests
run: |
python -m unittest discover
python -u -m unittest discover -v
10 changes: 8 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
# Standard hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
rev: v6.0.0
hooks:
- id: check-added-large-files
- id: check-merge-conflict
Expand All @@ -16,12 +16,18 @@ repos:

# Python hooks
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.7
rev: v0.12.8
hooks:
- id: ruff-check
args: [ --fix ]
- id: ruff-format

- repo: https://github.com/asottile/pyupgrade
rev: v3.20.0
hooks:
- id: pyupgrade
args: [--py39-plus]

# Spellcheck
- repo: https://github.com/crate-ci/typos
rev: v1.34.0
Expand Down
21 changes: 11 additions & 10 deletions package_xml_validation/helpers/cmake_parsers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from pathlib import Path
import re
from typing import List


def remove_comments(lines: List[str]) -> list[str]:
def remove_comments(lines: list[str]) -> list[str]:
"""Removes comments from a list of lines."""
return [line.split("#", 1)[0].strip() for line in lines]


def read_cmake_lines_with_parens_joined(raw_lines: List[str]) -> list[str]:
def read_cmake_lines_with_parens_joined(raw_lines: list[str]) -> list[str]:
"""
Reads a CMake file and joins lines that have an opening '(' without a matching ')'
until the closing ')' is found. Returns a list of logically complete lines.
Expand Down Expand Up @@ -41,7 +40,7 @@ def has_balanced_parens(s: str) -> bool:
return lines


def resolve_for_each(raw_lines: List[str]) -> List[str]:
def resolve_for_each(raw_lines: list[str]) -> list[str]:
"""Expands CMake's foreach() loops in a list of lines."""
foreach_stack = []

Expand Down Expand Up @@ -97,11 +96,11 @@ def resolve_for_each(raw_lines: List[str]) -> List[str]:
return lines


def retrieve_cmake_dependencies(lines: List[str]) -> List[str]:
def retrieve_cmake_dependencies(lines: list[str]) -> tuple[list[str], list[str]]:
if isinstance(lines, Path):
lines = read_cmake_file(lines)
main_deps = []
test_deps = []
main_deps: list[str] = []
test_deps: list[str] = []

# We'll track blocks of 'if(BUILD_TESTING)' with a small stack
if_stack = []
Expand Down Expand Up @@ -176,14 +175,14 @@ def add_deps(dep_list: list[str], is_test: bool):
return main_deps, test_deps


def read_cmake_file(file_path: Path) -> List[str]:
def read_cmake_file(file_path: Path) -> list[str]:
"""Reads a CMake file and returns a list of lines."""
if isinstance(file_path, str):
file_path = Path(file_path)
if not file_path.exists():
print(f"File not found: {file_path}")
return []
with open(file_path, "r") as f:
with open(file_path) as f:
raw_lines = f.readlines()
lines = remove_comments(raw_lines)
lines = read_cmake_lines_with_parens_joined(lines)
Expand All @@ -193,8 +192,10 @@ def read_cmake_file(file_path: Path) -> List[str]:
return lines


def read_deps_from_cmake_file(file_path: Path) -> List[str]:
def read_deps_from_cmake_file(file_path: Path | str) -> tuple[list[str], list[str]]:
"""Reads a CMake file and returns a list of dependencies."""
if isinstance(file_path, str):
file_path = Path(file_path)
lines = read_cmake_file(file_path)
try:
main_deps, test_deps = retrieve_cmake_dependencies(lines)
Expand Down
15 changes: 6 additions & 9 deletions package_xml_validation/helpers/logger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import sys


class ColoredFormatter(logging.Formatter):
Expand Down Expand Up @@ -30,28 +31,24 @@ def format(self, record):


def get_logger(name: str = __name__, level: str = "normal") -> logging.Logger:
"""
Returns a configured logger. If level=='verbose', DEBUG logs will be shown.
Otherwise, we default to INFO (as 'normal').
"""
logger = logging.getLogger(f"{name}_{level}")
logger.setLevel(logging.DEBUG) # We'll filter later using the handler level
logger.setLevel(logging.DEBUG)

ch = logging.StreamHandler(sys.stdout) # stdout is better for CI
ch.flush = sys.stdout.flush # force flush after each log

ch = logging.StreamHandler()
# Map "normal" to INFO and "verbose" to DEBUG
if level == "verbose":
ch.setLevel(logging.DEBUG)
else:
ch.setLevel(logging.INFO)

# Use our custom colored formatter
formatter = ColoredFormatter("%(message)s")
ch.setFormatter(formatter)

# Prevent adding multiple handlers if the logger already exists
if not logger.handlers:
logger.addHandler(ch)

logger.propagate = False # don't let root logger duplicate lines
return logger


Expand Down
Loading