56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
"""Path utilities."""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
from typing import List
|
|
|
|
try:
|
|
import pathspec
|
|
|
|
PATHSPEC_AVAILABLE = True
|
|
except ImportError:
|
|
PATHSPEC_AVAILABLE = False
|
|
|
|
|
|
def glob_patterns(patterns: List[str], base_path: Path) -> List[Path]:
|
|
"""Find files matching glob patterns."""
|
|
if not PATHSPEC_AVAILABLE:
|
|
return _glob_patterns_fallback(patterns, base_path)
|
|
|
|
spec = pathspec.PathSpec.from_lines("gitwildmatch", patterns)
|
|
matched = []
|
|
|
|
for root, _dirs, files in os.walk(base_path):
|
|
root_path = Path(root)
|
|
|
|
for filename in files:
|
|
file_path = root_path / filename
|
|
relative_path = file_path.relative_to(base_path)
|
|
|
|
if spec.match_file(str(relative_path)):
|
|
matched.append(file_path)
|
|
|
|
return matched
|
|
|
|
|
|
def _glob_patterns_fallback(patterns: List[str], base_path: Path) -> List[Path]:
|
|
"""Fallback glob implementation using Path.glob."""
|
|
matched = []
|
|
|
|
for pattern in patterns:
|
|
matched.extend(base_path.rglob(pattern))
|
|
|
|
return sorted(set(matched))
|
|
|
|
|
|
def is_ignored(path: Path, ignore_patterns: List[str]) -> bool:
|
|
"""Check if a path should be ignored."""
|
|
if not PATHSPEC_AVAILABLE:
|
|
for pattern in ignore_patterns:
|
|
if path.match(pattern):
|
|
return True
|
|
return False
|
|
|
|
spec = pathspec.PathSpec.from_lines("gitwildmatch", ignore_patterns)
|
|
return spec.match_file(str(path))
|