Recursively scan directory
(path: Path, current_depth: int = 0)
| 1305 | return json.dumps(result, ensure_ascii=False, indent=2) |
| 1306 | |
| 1307 | def scan_directory(path: Path, current_depth: int = 0) -> Dict[str, Any]: |
| 1308 | """Recursively scan directory""" |
| 1309 | if current_depth >= max_depth: |
| 1310 | return {"type": "directory", "name": path.name, "truncated": True} |
| 1311 | |
| 1312 | items = [] |
| 1313 | try: |
| 1314 | for item in sorted(path.iterdir()): |
| 1315 | relative_path = os.path.relpath(item, WORKSPACE_DIR) |
| 1316 | |
| 1317 | if item.is_file(): |
| 1318 | file_info = { |
| 1319 | "type": "file", |
| 1320 | "name": item.name, |
| 1321 | "path": relative_path, |
| 1322 | "size_bytes": item.stat().st_size, |
| 1323 | "extension": item.suffix, |
| 1324 | } |
| 1325 | items.append(file_info) |
| 1326 | elif item.is_dir() and not item.name.startswith("."): |
| 1327 | dir_info = scan_directory(item, current_depth + 1) |
| 1328 | dir_info["path"] = relative_path |
| 1329 | items.append(dir_info) |
| 1330 | except PermissionError: |
| 1331 | pass |
| 1332 | |
| 1333 | return { |
| 1334 | "type": "directory", |
| 1335 | "name": path.name, |
| 1336 | "items": items, |
| 1337 | "item_count": len(items), |
| 1338 | } |
| 1339 | |
| 1340 | structure = scan_directory(target_dir) |
| 1341 |
no outgoing calls
no test coverage detected