|
| 1 | +import os |
| 2 | +import time |
| 3 | +from typing import List, Dict, Any, Set, Union |
| 4 | +from pathlib import Path |
| 5 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
| 6 | +from diskcache import Cache |
| 7 | +from graphgen.utils import logger |
| 8 | + |
| 9 | +class ParallelDirScanner: |
| 10 | + def __init__(self, |
| 11 | + cache_dir: str, |
| 12 | + allowed_suffix, |
| 13 | + rescan: bool = False, |
| 14 | + max_workers: int = 4 |
| 15 | + ): |
| 16 | + self.cache = Cache(cache_dir) |
| 17 | + self.allowed_suffix = set(allowed_suffix) if allowed_suffix else None |
| 18 | + self.rescan = rescan |
| 19 | + self.max_workers = max_workers |
| 20 | + |
| 21 | + def scan(self, paths: Union[str, List[str]], recursive: bool = True) -> Dict[str, Any]: |
| 22 | + if isinstance(paths, str): |
| 23 | + paths = [paths] |
| 24 | + |
| 25 | + results = {} |
| 26 | + with ThreadPoolExecutor(max_workers=self.max_workers) as executor: |
| 27 | + future_to_path = { |
| 28 | + executor.submit(self._scan_dir, Path(p).resolve(), recursive, set()): p |
| 29 | + for p in paths if os.path.exists(p) |
| 30 | + } |
| 31 | + |
| 32 | + for future in as_completed(future_to_path): |
| 33 | + path = future_to_path[future] |
| 34 | + try: |
| 35 | + results[path] = future.result() |
| 36 | + except Exception as e: |
| 37 | + logger.error("Error scanning path %s: %s", path, e) |
| 38 | + results[path] = {'error': str(e), 'files': [], 'dirs': [], 'stats': {}} |
| 39 | + |
| 40 | + return results |
| 41 | + |
| 42 | + def _scan_dir(self, path: Path, recursive: bool, visited: Set[str]) -> Dict[str, Any]: |
| 43 | + path_str = str(path) |
| 44 | + |
| 45 | + # Avoid cycles due to symlinks |
| 46 | + if path_str in visited: |
| 47 | + logger.warning("Skipping already visited path: %s", path_str) |
| 48 | + return self._empty_result(path_str) |
| 49 | + |
| 50 | + # cache check |
| 51 | + cache_key = f"scan::{path_str}::recursive::{recursive}" |
| 52 | + cached = self.cache.get(cache_key) |
| 53 | + if cached and not self.rescan: |
| 54 | + logger.info("Using cached scan result for path: %s", path_str) |
| 55 | + return cached['data'] |
| 56 | + |
| 57 | + logger.info("Scanning path: %s", path_str) |
| 58 | + files, dirs = [], [] |
| 59 | + stats = {'total_size': 0, 'file_count': 0, 'dir_count': 0, 'errors': 0} |
| 60 | + |
| 61 | + try: |
| 62 | + with os.scandir(path_str) as entries: |
| 63 | + for entry in entries: |
| 64 | + try: |
| 65 | + entry_stat = entry.stat(follow_symlinks=False) |
| 66 | + |
| 67 | + if entry.is_dir(): |
| 68 | + dirs.append({ |
| 69 | + 'path': entry.path, |
| 70 | + 'name': entry.name, |
| 71 | + 'mtime': entry_stat.st_mtime |
| 72 | + }) |
| 73 | + stats['dir_count'] += 1 |
| 74 | + else: |
| 75 | + # allowed suffix filter |
| 76 | + if self.allowed_suffix: |
| 77 | + suffix = Path(entry.name).suffix.lower() |
| 78 | + if suffix not in self.allowed_suffix: |
| 79 | + continue |
| 80 | + |
| 81 | + files.append({ |
| 82 | + 'path': entry.path, |
| 83 | + 'name': entry.name, |
| 84 | + 'size': entry_stat.st_size, |
| 85 | + 'mtime': entry_stat.st_mtime |
| 86 | + }) |
| 87 | + stats['total_size'] += entry_stat.st_size |
| 88 | + stats['file_count'] += 1 |
| 89 | + |
| 90 | + except OSError: |
| 91 | + stats['errors'] += 1 |
| 92 | + |
| 93 | + except (PermissionError, FileNotFoundError, OSError) as e: |
| 94 | + logger.error("Failed to scan directory %s: %s", path_str, e) |
| 95 | + return {'error': str(e), 'files': [], 'dirs': [], 'stats': stats} |
| 96 | + |
| 97 | + if recursive: |
| 98 | + sub_visited = visited | {path_str} |
| 99 | + sub_results = self._scan_subdirs(dirs, sub_visited) |
| 100 | + |
| 101 | + for sub_data in sub_results.values(): |
| 102 | + files.extend(sub_data.get('files', [])) |
| 103 | + stats['total_size'] += sub_data['stats'].get('total_size', 0) |
| 104 | + stats['file_count'] += sub_data['stats'].get('file_count', 0) |
| 105 | + |
| 106 | + result = {'path': path_str, 'files': files, 'dirs': dirs, 'stats': stats} |
| 107 | + self._cache_result(cache_key, result, path) |
| 108 | + return result |
| 109 | + |
| 110 | + def _scan_subdirs(self, dir_list: List[Dict], visited: Set[str]) -> Dict[str, Any]: |
| 111 | + """ |
| 112 | + Parallel scan subdirectories |
| 113 | + :param dir_list |
| 114 | + :param visited |
| 115 | + :return: |
| 116 | + """ |
| 117 | + results = {} |
| 118 | + with ThreadPoolExecutor(max_workers=self.max_workers) as executor: |
| 119 | + futures = { |
| 120 | + executor.submit(self._scan_dir, Path(d['path']), True, visited): d['path'] |
| 121 | + for d in dir_list |
| 122 | + } |
| 123 | + |
| 124 | + for future in as_completed(futures): |
| 125 | + path = futures[future] |
| 126 | + try: |
| 127 | + results[path] = future.result() |
| 128 | + except Exception as e: |
| 129 | + logger.error("Error scanning subdirectory %s: %s", path, e) |
| 130 | + results[path] = {'error': str(e), 'files': [], 'dirs': [], 'stats': {}} |
| 131 | + |
| 132 | + return results |
| 133 | + |
| 134 | + def _cache_result(self, key: str, result: Dict, path: Path): |
| 135 | + """Cache the scan result""" |
| 136 | + try: |
| 137 | + self.cache.set(key, { |
| 138 | + 'data': result, |
| 139 | + 'dir_mtime': path.stat().st_mtime, |
| 140 | + 'cached_at': time.time() |
| 141 | + }) |
| 142 | + logger.info(f"Cached scan result for: {path}") |
| 143 | + except OSError: |
| 144 | + pass |
| 145 | + |
| 146 | + def invalidate(self, path: str): |
| 147 | + """Invalidate cache for a specific path""" |
| 148 | + path = Path(path).resolve() |
| 149 | + keys = [k for k in self.cache if k.startswith(f"scan:{path}")] |
| 150 | + for k in keys: |
| 151 | + self.cache.delete(k) |
| 152 | + logger.info(f"Invalidated cache for path: {path}") |
| 153 | + |
| 154 | + def close(self): |
| 155 | + self.cache.close() |
| 156 | + |
| 157 | + def __enter__(self): |
| 158 | + return self |
| 159 | + |
| 160 | + def __exit__(self, *args): |
| 161 | + self.close() |
| 162 | + |
| 163 | + @staticmethod |
| 164 | + def _empty_result(path: str) -> Dict[str, Any]: |
| 165 | + return { |
| 166 | + 'path': path, |
| 167 | + 'files': [], |
| 168 | + 'dirs': [], |
| 169 | + 'stats': {'total_size': 0, 'file_count': 0, 'dir_count': 0, 'errors': 0} |
| 170 | + } |
0 commit comments