Repository navigation
Expand file tree
/
Copy pathscan.py
More file actions
236 lines (186 loc) · 8.32 KB
/
Copy pathscan.py
File metadata and controls
236 lines (186 loc) · 8.32 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# file: scan.py
"""Enhanced project scanning with better error handling, progress tracking, and file filtering."""
from __future__ import annotations
import datetime
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
from typing import Tuple, Optional, Callable
from report import (
CODE_EXTENSIONS,
EXCLUDED_DIRS,
MAX_THREADS_FALLBACK,
FileInfo,
ProjectReport,
now_stamp,
)
# Additional file extensions to consider for content reading
READABLE_EXTENSIONS = CODE_EXTENSIONS | {
".css", ".js", ".html", ".xml", ".csv", ".ini", ".cfg", ".conf",
".log", ".sh", ".bat", ".ps1", ".sql", ".dockerfile", ".gitignore",
".env", ".properties", ".toml", ".rst", ".tex", ".makefile"
}
# Maximum file size to read (5MB)
MAX_FILE_SIZE = 5 * 1024 * 1024
def _is_text_file(file_path: Path) -> bool:
"""Check if a file is likely to be text-based"""
ext = file_path.suffix.lower()
if ext in READABLE_EXTENSIONS:
return True
# Check for files without extensions that might be text
if not ext and file_path.name.lower() in {
'readme', 'license', 'changelog', 'makefile', 'dockerfile'
}:
return True
return False
def is_content_readable(file_path: Path) -> bool:
"""Public helper used by the GUI to determine if a file's content can be captured."""
return _is_text_file(file_path)
def _read_file_counts(file_path: Path) -> Tuple[Path, int | str, int | str, str]:
"""Read file and count lines/words with enhanced error handling"""
try:
# Check file size before reading
stat_result = file_path.stat()
if stat_result.st_size > MAX_FILE_SIZE:
return file_path, "large", "large", ""
# Try to read as text
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
content = f.read()
if not content.strip(): # Empty file
return file_path, 0, 0, ""
lines = content.count("\n") + 1 if content else 0
words = len(content.split()) if content else 0
return file_path, lines, words, content
except (PermissionError, OSError):
return file_path, "denied", "denied", ""
except UnicodeDecodeError:
# File is likely binary
return file_path, "binary", "binary", ""
except Exception:
return file_path, "error", "error", ""
def _is_hidden_or_excluded(name: str) -> bool:
"""Check if a file/directory should be excluded"""
if name.startswith('.') or name in EXCLUDED_DIRS or name.lower() in {'thumbs.db', 'desktop.ini', '.ds_store'}:
return True
from analysis.languages import get_all_excluded_dirs
return name in get_all_excluded_dirs()
def scan_project(root_path: Path, progress_callback: Optional[Callable[[str], None]] = None) -> ProjectReport:
"""
Scan a project directory and build a comprehensive report.
Args:
root_path: Root directory to scan
progress_callback: Optional callback for progress updates
Returns:
ProjectReport with file information and content
"""
if progress_callback:
progress_callback("Starting project scan...")
files: list[FileInfo] = []
readable_paths: list[Path] = []
total_size = 0
# Walk directory tree
try:
for root, dirs, filenames in os.walk(root_path, followlinks=False):
# Filter directories to skip hidden/excluded ones
dirs[:] = [d for d in dirs if not _is_hidden_or_excluded(d)]
for filename in filenames:
if _is_hidden_or_excluded(filename):
continue
file_path = Path(root) / filename
try:
stat_result = file_path.stat(follow_symlinks=False)
# Skip if it's a symbolic link
if file_path.is_symlink():
continue
# Skip very large files (>100MB) entirely
if stat_result.st_size > 100 * 1024 * 1024:
continue
mtime = datetime.datetime.fromtimestamp(stat_result.st_mtime).strftime("%Y-%m-%d %H:%M")
relative_path = file_path.relative_to(root_path)
fi = FileInfo(
path=str(relative_path),
size_bytes=stat_result.st_size,
mtime_iso=mtime,
lines="?",
words="?",
content=None,
)
files.append(fi)
total_size += stat_result.st_size
# Add to readable files if it's a text file
if _is_text_file(file_path):
readable_paths.append(file_path)
except (PermissionError, OSError, ValueError):
# Skip files we can't access or process
continue
except (PermissionError, OSError) as e:
raise RuntimeError(f"Cannot access project directory: {e}")
if progress_callback:
progress_callback(f"Found {len(files)} files, reading {len(readable_paths)} text files...")
# Read file contents in parallel for text files
if readable_paths:
max_threads = min(len(readable_paths), os.cpu_count() or MAX_THREADS_FALLBACK)
counts_map: dict[Path, tuple[int | str, int | str, str]] = {}
try:
with ThreadPoolExecutor(max_workers=max_threads) as executor:
# Submit all tasks
future_to_path = {
executor.submit(_read_file_counts, path): path
for path in readable_paths
}
completed = 0
for future in as_completed(future_to_path):
path, lines, words, content = future.result()
counts_map[path] = (lines, words, content)
completed += 1
if progress_callback and completed % 10 == 0:
progress_callback(f"Read {completed}/{len(readable_paths)} files...")
except Exception as e:
if progress_callback:
progress_callback(f"Warning: Error reading some files: {e}")
# Update file info with counts and content
for fi in files:
full_path = root_path / fi.path
if full_path in counts_map:
lines, words, content = counts_map[full_path]
fi.lines = lines
fi.words = words
fi.content = content
# Sort files by path for consistent output
files.sort(key=lambda f: f.path.lower())
if progress_callback:
progress_callback("Scan complete!")
return ProjectReport(
root=str(root_path),
generated_at=now_stamp(),
files=files
)
def get_project_stats(root_path: Path) -> dict:
"""Get quick stats about a project without full scanning"""
stats = {
'total_files': 0,
'text_files': 0,
'total_size': 0,
'directories': 0
}
try:
for root, dirs, filenames in os.walk(root_path):
dirs[:] = [d for d in dirs if not _is_hidden_or_excluded(d)]
stats['directories'] += len(dirs)
for filename in filenames:
if _is_hidden_or_excluded(filename):
continue
file_path = Path(root) / filename
try:
if file_path.is_symlink():
continue
size = file_path.stat().st_size
stats['total_files'] += 1
stats['total_size'] += size
if _is_text_file(file_path):
stats['text_files'] += 1
except (PermissionError, OSError):
continue
except (PermissionError, OSError):
pass
return stats