#!/usr/bin/env python3

"""
Utilities for code checker scripts.

Copyright Forschungszentrum Jülich GmbH 2025.
License:  Public Domain
"""

import re, subprocess, sys

def split_fname(fn):
    """
    return stem, suffix
    """
    m = re.match(r'(.*)\.(\w+)$', fn)
    if m:
        return (m.group(1), m.group(2))
    return (fn, "")

import os, re, sys

def all_files(root='.'):
    excluded_dirs = {
        './auto', './devtools', './build', './debug',
        './cover', './tidy', './qbuild'
    }
    excluded_patterns = re.compile(r'.*(ThirdParty|3rdparty).*')
    included_extensions = {'.cpp', '.c', '.h'}

    result_files = []
    for dirpath, dirnames, filenames in os.walk(root):
        # Normalize path
        dirpath = os.path.normpath(dirpath)

        # Skip excluded directories
        if any(os.path.commonpath([dirpath, os.path.normpath(ex)]) == os.path.normpath(ex) for ex in excluded_dirs):
            dirnames[:] = []  # Don't descend into this directory
            continue

        for filename in filenames:
            filepath = os.path.join(dirpath, filename)
            if excluded_patterns.search(filepath):
                continue
            if os.path.splitext(filename)[1] in included_extensions:
                result_files.append(filepath)

    return result_files
