forked from OpenBMB/ChatDev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace_scanner.py
More file actions
executable file
·71 lines (61 loc) · 2.1 KB
/
workspace_scanner.py
File metadata and controls
executable file
·71 lines (61 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Utilities for scanning nested code_workspace directories."""
from dataclasses import dataclass
from pathlib import Path
from typing import Iterator, List, Optional
@dataclass
class WorkspaceEntry:
"""Metadata about a workspace file or directory."""
path: str # relative path from workspace root
type: str # "file" | "directory"
size: Optional[int]
modified_ts: Optional[float]
depth: int
def iter_workspace_entries(
root: Path | str,
*,
recursive: bool = True,
max_depth: int = 5,
include_hidden: bool = False,
) -> Iterator[WorkspaceEntry]:
"""Yield entries under the workspace root respecting depth/hidden filters."""
base = Path(root).resolve()
if not base.exists():
return
stack: List[tuple[Path, int]] = [(base, 0)]
while stack:
current, depth = stack.pop()
try:
children = sorted(current.iterdir(), key=lambda p: p.name.lower())
except FileNotFoundError:
continue
except PermissionError:
continue
for child in children:
try:
rel = child.relative_to(base)
except ValueError:
continue
if not include_hidden and _is_hidden(rel):
continue
entry_type = "directory" if child.is_dir() else "file"
size = None
modified = None
try:
stat = child.stat()
modified = stat.st_mtime
if child.is_file():
size = stat.st_size
except (FileNotFoundError, PermissionError, OSError):
pass
child_depth = depth + 1
yield WorkspaceEntry(
path=str(rel),
type=entry_type,
size=size,
modified_ts=modified,
depth=child_depth,
)
if recursive and child.is_dir() and child_depth < max_depth:
stack.append((child, child_depth))
def _is_hidden(relative_path: Path) -> bool:
return any(part.startswith(".") for part in relative_path.parts)