Source code for cc4q.pipeline.files

"""Filesystem discovery helpers for repository checkouts."""

import os
from typing import List, Optional

__all__ = ["get_files_from_dir"]


[docs] def get_files_from_dir( directory: str, include_dirs: Optional[List[str]] = None ) -> List[str]: """.py files under ``directory``, restricted to ``include_dirs``. Paths in ``include_dirs`` are relative to ``directory``. Empty or ``None`` ``include_dirs`` means the whole tree. Matching uses an exact-or-path-boundary rule, so a package dir like ``qiskit_aer`` does not swallow a sibling ``qiskit_aer_extra``. Results are sorted for deterministic file order. Args: directory: Repository checkout root. include_dirs: Optional subdirectories to include. Returns: Absolute paths to matching ``.py`` files. """ directory = os.path.abspath(directory) if include_dirs: bases = [ os.path.abspath(os.path.join(directory, d)) for d in include_dirs ] else: bases = None files_path = [] for root, dirnames, filenames in os.walk(directory): dirnames.sort() if bases is not None and not any( root == b or root.startswith(b + os.sep) for b in bases ): continue for file in sorted(filenames): if file.endswith(".py"): files_path.append(os.path.join(root, file)) return files_path