Repository navigation
Expand file tree
/
Copy pathcheck_file_list.py
More file actions
executable file
·313 lines (258 loc) · 10.8 KB
/
Copy pathcheck_file_list.py
File metadata and controls
executable file
·313 lines (258 loc) · 10.8 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python3
"""
File List Checker Script
Checks which files from a list exist in a directory
"""
import os
import sys
import argparse
from pathlib import Path
class FileListChecker:
def __init__(self, search_path, ignore_comments=True):
"""
Initialize file list checker
Args:
search_path: Path to search for files
ignore_comments: Whether to ignore lines starting with #
"""
self.search_path = Path(search_path).resolve()
self.ignore_comments = ignore_comments
self.stats = {
'total': 0,
'found': 0,
'missing': 0,
'comments': 0,
'empty': 0
}
def read_file_list(self, list_file):
"""Read and parse the file list"""
files = []
try:
with open(list_file, 'r', encoding='utf-8') as f:
for line_num, line in enumerate(f, 1):
line = line.strip()
if not line: # Empty line
self.stats['empty'] += 1
continue
if self.ignore_comments and line.startswith('#'):
self.stats['comments'] += 1
continue
# Remove any trailing comments
if '#' in line:
line = line.split('#')[0].strip()
if line:
files.append(line)
self.stats['total'] += 1
except Exception as e:
print(f"Error reading {list_file}: {e}", file=sys.stderr)
sys.exit(1)
return files
def check_file(self, filepath):
"""Check if a file exists"""
# Handle different path formats
full_path = self.search_path / filepath.lstrip('/')
# Try without leading / if it didn't work
if not full_path.exists() and filepath.startswith('/'):
full_path = self.search_path / filepath[1:]
return full_path.exists()
def check_files(self, list_file):
"""Check all files from the list"""
files = self.read_file_list(list_file)
print(f"\n📂 Searching in: {self.search_path}")
print(f"📋 File list: {list_file}")
print(f"📊 Total files to check: {len(files)}")
print("-" * 60)
found_files = []
missing_files = []
for i, filepath in enumerate(files, 1):
exists = self.check_file(filepath)
if exists:
self.stats['found'] += 1
found_files.append(filepath)
status = "✅ FOUND"
else:
self.stats['missing'] += 1
missing_files.append(filepath)
status = "❌ MISSING"
# Show progress every 10 files or for all if less than 100
if len(files) <= 100 or i % 10 == 0 or i == len(files):
print(f"[{i}/{len(files)}] {status}: {filepath[:60]}{'...' if len(filepath) > 60 else ''}")
return found_files, missing_files
def print_summary(self):
"""Print summary statistics"""
print("\n" + "="*60)
print("SUMMARY")
print("="*60)
print(f"📊 Statistics:")
print(f" • Total entries in file: {self.stats['total']}")
print(f" • Comments ignored: {self.stats['comments']}")
print(f" • Empty lines ignored: {self.stats['empty']}")
print()
print(f"✅ Files found: {self.stats['found']}")
print(f"❌ Files missing: {self.stats['missing']}")
print(f"📈 Success rate: {self.stats['found']/self.stats['total']*100:.1f}%" if self.stats['total'] > 0 else "N/A")
print("="*60)
def save_results(self, found_file, missing_file):
"""Save results to files"""
if found_file:
with open(found_file, 'w', encoding='utf-8') as f:
for filepath in self.found_files:
f.write(f"{filepath}\n")
print(f"✅ Found files saved to: {found_file}")
if missing_file:
with open(missing_file, 'w', encoding='utf-8') as f:
for filepath in self.missing_files:
f.write(f"{filepath}\n")
print(f"📝 Missing files saved to: {missing_file}")
def verify_paths(self, list_file):
"""Verify that paths in list file are accessible"""
files = self.read_file_list(list_file)
invalid_paths = []
for filepath in files:
# Check if path has invalid characters or is empty
if not filepath or '..' in filepath or '//' in filepath:
invalid_paths.append(filepath)
if invalid_paths:
print("\n⚠️ Warning: Found potentially invalid paths:")
for path in invalid_paths[:10]:
print(f" • {path}")
if len(invalid_paths) > 10:
print(f" ... and {len(invalid_paths) - 10} more")
def interactive_check(self, list_file):
"""Interactive mode with options"""
self.found_files, self.missing_files = self.check_files(list_file)
self.print_summary()
while True:
print("\nOptions:")
print(" 1. Show all found files")
print(" 2. Show all missing files")
print(" 3. Save results to files")
print(" 4. Search for missing files (case-insensitive)")
print(" 5. Exit")
choice = input("\nSelect option (1-5): ").strip()
if choice == '1':
print("\n✅ FOUND FILES:")
for i, filepath in enumerate(self.found_files, 1):
print(f" {i}. {filepath}")
elif choice == '2':
print("\n❌ MISSING FILES:")
for i, filepath in enumerate(self.missing_files, 1):
print(f" {i}. {filepath}")
elif choice == '3':
found_out = input("Save found files to (or press Enter to skip): ").strip()
missing_out = input("Save missing files to (or press Enter to skip): ").strip()
self.save_results(found_out if found_out else None,
missing_out if missing_out else None)
elif choice == '4':
search_term = input("Enter search term: ").strip().lower()
print(f"\n🔍 Searching for '{search_term}'...")
found = []
for filepath in self.missing_files:
if search_term in filepath.lower():
found.append(filepath)
if found:
print(f"Found {len(found)} missing files matching '{search_term}':")
for filepath in found:
# Try to find actual location
filename = Path(filepath).name
matches = list(self.search_path.rglob(f"*{filename}*"))
if matches:
print(f" • {filepath}")
print(f" → Found at: {matches[0]}")
else:
print(f" • {filepath} (no matches found)")
else:
print(f"No missing files match '{search_term}'")
elif choice == '5':
break
def main():
parser = argparse.ArgumentParser(
description="Check which files from a list exist in a directory",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s proprietary-files.txt /path/to/vendor # Check files in vendor
%(prog)s -l filelist.txt /path/to/system # Check in system
%(prog)s --no-ignore filelist.txt /path # Don't ignore comments
%(prog)s -i filelist.txt /path # Interactive mode
%(prog)s -o found.txt -m missing.txt list.txt /path # Save results
"""
)
parser.add_argument(
'list_file',
help='File containing list of paths to check'
)
parser.add_argument(
'search_path',
help='Directory to search for files'
)
parser.add_argument(
'--no-ignore',
action='store_true',
help='Do not ignore lines starting with #'
)
parser.add_argument(
'-o', '--output-found',
metavar='FILE',
help='Save found files to FILE'
)
parser.add_argument(
'-m', '--output-missing',
metavar='FILE',
help='Save missing files to FILE'
)
parser.add_argument(
'-i', '--interactive',
action='store_true',
help='Interactive mode with additional options'
)
parser.add_argument(
'-v', '--verbose',
action='store_true',
help='Show detailed output for all files'
)
parser.add_argument(
'--verify',
action='store_true',
help='Verify paths for potential issues'
)
args = parser.parse_args()
# Create checker
checker = FileListChecker(
search_path=args.search_path,
ignore_comments=not args.no_ignore
)
# Verify paths if requested
if args.verify:
checker.verify_paths(args.list_file)
# Run in interactive mode or normal mode
if args.interactive:
checker.interactive_check(args.list_file)
else:
found, missing = checker.check_files(args.list_file)
checker.print_summary()
# Save results if requested
if args.output_found or args.output_missing:
checker.found_files = found
checker.missing_files = missing
checker.save_results(args.output_found, args.output_missing)
# Show all if verbose
if args.verbose:
if found:
print("\n✅ Found files:")
for f in found:
print(f" {f}")
if missing:
print("\n❌ Missing files:")
for m in missing:
print(f" {m}")
return 0
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
print("\n\n⚠️ Interrupted by user")
sys.exit(1)
except Exception as e:
print(f"\n❌ Error: {e}", file=sys.stderr)
sys.exit(1)