-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_files.py
422 lines (382 loc) · 17.3 KB
/
rename_files.py
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import os
import re
import shutil
import asyncio
import sys
import json
import subprocess
import uuid
from collections import Counter
from datetime import datetime
from pathlib import Path
from typing import Any, Callable, Union
from ollama import AsyncClient, Client
import logging
import argparse
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger(__name__)
class InteractiveFileRenamer:
def __init__(
self,
base_dir: Union[str, Path] = None,
auto_mode: bool = False,
model_name: str = "qwen2.5-coder:7b",
context_length: int = 2048,
filename_prefix: str = "new",
filename_extension: str = ".txt",
max_file_name_length: int = 128,
backup_dir: Union[str, Path] = None,
):
self.base_dir: Path | None = Path(base_dir) if base_dir else None
self.auto_mode: bool = auto_mode
self.model_name: str = model_name
self.context_length: int = context_length
self.filename_prefix: str = filename_prefix
self.filename_extension: str = filename_extension
self.max_file_name_length: int = max_file_name_length
self.backup_dir: Path = backup_dir
self.ollama_client: Client = Client()
def create_backup_directory(self, base_dir: Path) -> Path:
"""
Create a backup directory within the base directory.
Ensures unique backup folder names using timestamp or incremental numbering.
"""
backup_dir = os.path.join(base_dir, f"backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}")
os.makedirs(backup_dir, exist_ok=True)
return Path(backup_dir)
@staticmethod
def backup_file(
file_path: Path,
backup_dir: Path
) -> bool:
"""
Copy a file to the backup directory before renaming.
Preserves original filename in the backup.
"""
try:
shutil.copy2(file_path, backup_dir)
return True
except Exception as e:
logger.error(f"Backup failed for {file_path}: {e}")
return False
def _discover_file_patterns(self) -> list[tuple[tuple[str, str], int]]:
"""Discover potential file patterns in the directory."""
if not self.base_dir or not os.path.isdir(self.base_dir):
raise ValueError("Invalid directory")
patterns = Counter()
for filename in os.listdir(self.base_dir):
if os.path.isfile(os.path.join(self.base_dir, filename)):
prefix_match = re.match(r"^([^.\w]{1,5}|\w{1,5})", filename)
if prefix_match:
prefix = prefix_match.group(1)
ext = os.path.splitext(filename)[1].lower()
patterns[(prefix, ext)] += 1
return patterns.most_common(5)
def _interactive_directory_selection(self) -> Path:
"""Interactively select the directory for file renaming."""
while True:
dir_path_str = input("Enter the directory path (press Enter for current directory): ").strip()
dir_path = Path(dir_path_str) if dir_path_str else Path.cwd()
if dir_path.is_dir():
return dir_path
else:
print(f"Error: '{dir_path}' is not a valid directory. Please try again.")
def _interactive_file_pattern_selection(
self,
patterns: list[tuple[tuple[Any, str], int]]
) -> tuple[str, str]:
"""Interactively select file patterns for renaming."""
print("\nChoose pattern of names for renaming:")
for i, ((prefix, ext), count) in enumerate(patterns, 1):
print(f"{i}. `{prefix}` - {count} files, {ext[1:]}")
print(f"{len(patterns) + 1}. Write your pattern")
while True:
try:
choice = input('Press number of chosen variant: ').strip()
if not choice:
if patterns:
return patterns[0][0]
else:
return "new", ".txt"
choice = int(choice)
if 1 <= choice <= len(patterns):
return patterns[choice - 1][0]
elif choice == len(patterns) + 1:
prefix = input('Enter file prefix: ').strip()
ext = input('Enter file extension (with dot): ').strip().lower()
return prefix, ext
else:
print('Invalid selection. Please try again.')
except ValueError:
print('Please enter a valid number.')
def _get_available_ollama_models(self) -> list[str]:
"""Retrieve available Ollama models."""
try:
result = subprocess.run(
['curl', 'http://localhost:11434/api/tags'],
capture_output=True,
text=True,
timeout=5
)
models_data = json.loads(result.stdout)
return [model['name'] for model in models_data.get('models', [])]
except Exception as e:
print(f"Error retrieving Ollama models: {e}")
return []
def _interactive_model_selection(
self,
available_models: list[str]
) -> str:
"""Interactively select an Ollama model for renaming."""
if not available_models:
print("No Ollama models found. Using default model.")
return 'qwen2.5-coder:7b'
print("\nAvailable Ollama models:")
for i, model in enumerate(available_models, 1):
logger.info(f"{i}. {model}")
print(f"{len(available_models) + 1}. Enter custom model name")
while True:
try:
choice = input("Select model number (or press Enter for first model): ").strip()
if not choice:
return available_models[0]
choice = int(choice)
if 1 <= choice <= len(available_models):
return available_models[choice - 1]
elif choice == len(available_models) + 1:
return input("Enter full model name: ").strip()
else:
print("Invalid selection. Please try again.")
except ValueError:
print("Please enter a valid number.")
def _get_model_max_context(
self,
model_name: str
) -> int:
"""Get the maximum context length for a given model."""
try:
model_info = self.ollama_client.show(model_name)
if model_info and model_info.modelinfo:
context_length = None
for key, value in model_info.modelinfo.items():
if key.endswith(".context_length"):
context_length = int(value)
break
if context_length:
print(f"Current context length for {model_name} is: {context_length}")
return context_length
print(f"Could not determine context length for {model_name} (key not found)")
return 2048
except Exception as e:
print(f"Could not retrieve model info: {e}")
return 2048
def interactive_configuration(self) -> None:
"""Orchestrate interactive configuration."""
if self.auto_mode:
return
self.base_dir = self._interactive_directory_selection()
patterns = self._discover_file_patterns()
self.filename_prefix, self.filename_extension = self._interactive_file_pattern_selection(patterns)
available_models = self._get_available_ollama_models()
self.model_name = self._interactive_model_selection(available_models)
max_context = self._get_model_max_context(self.model_name)
while True:
context_choice = input(
f"\nModel max context: {max_context} characters. Enter custom length, `500` will work (or press Enter for max): ").strip()
if not context_choice:
self.context_length = max_context
break
elif context_choice.isdigit() and int(context_choice) <= max_context:
self.context_length = int(context_choice)
break
else:
print(f"Invalid context length. Please enter a number less than or equal to {max_context}.")
print("\n--- Configuration Summary ---")
print(f"Directory: {self.base_dir}")
print(f"File Pattern: '{self.filename_prefix}*{self.filename_extension}'")
print(f"Model: {self.model_name}")
print(f"Context Length: {self.context_length} characters")
def _sanitize_filename(
self,
suggested_name: str,
original_ext: str
) -> str:
suggested_name = suggested_name.replace('.', '')
suggested_name = suggested_name.replace('`', '').replace("'", "")
forbidden_chars = r'[<>/\\\|\?\*\:\"\+\%\!\@]'
suggested_name = re.sub(forbidden_chars, "_", suggested_name)
suggested_name = re.sub(r"\s+", "-", suggested_name)
suggested_name = re.sub(r"-{2,}", "-", suggested_name)
suggested_name = re.sub(r"^[^a-zA-Z]+", "", suggested_name)
suggested_name = re.sub(r"[^a-zA-Z0-9]+$", "", suggested_name)
if not suggested_name:
suggested_name = f"untitled-document {uuid.uuid4()}"
suggested_name = f"{suggested_name}{original_ext}"
return suggested_name
async def analyze_and_rename(
self,
file_path: Path,
filename: str
) -> tuple[bool, str, str]:
if self.backup_dir is None:
self.backup_dir = self.create_backup_directory(self.base_dir)
backup_success = self.backup_file(file_path, self.backup_dir)
if not backup_success:
logger.warning(f"Could not backup {filename} before renaming")
return False, filename, "Backup failed"
if not os.path.exists(file_path):
print(f"File {file_path} no longer exists.")
return False, filename, "File does not exist"
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read(self.context_length) # Use specified context length
except Exception as e:
print(f"Error reading file {filename}: {e}")
return False, filename, str(e)
prompt = f"""
The following text is from a file in a software development project:
```
{content}
```
Suggest a file name using the following pattern:
`<Type> - <Action> - <Component> - <Short Description>.<Extension>`
Where:
* **Type:** task, issue, doc, snippet, review, idea
* **Action:** create, fix, update, review, discuss, complete
* **Component:** backend, frontend, database, api, auth, testing, or a more specific component name
* **Short Description:** A concise summary of the file's content.
* **Extension:** Based on the content (e.g., .md, .txt, .py). If unsure, use .md
Provide only the file name as output, maximum 128 characters, lowercase.
"""
try:
client = AsyncClient()
response = await client.generate(
self.model_name,
prompt,
options={
"num_predict": self.max_file_name_length,
}
)
suggested_name = response["response"].strip().lower()
except Exception as e:
print(f"Error generating new filename for {filename}: {e}")
return False, filename, str(e)
original_ext = os.path.splitext(filename)[1]
suggested_name = re.sub(r"\.\w+$", "", suggested_name)
new_filename = self._sanitize_filename(suggested_name, original_ext)
try:
new_file_path = self.base_dir / new_filename
os.rename(file_path, new_file_path)
print(f"Renamed: {filename} -> {new_filename}")
return True, new_filename, "Success"
except Exception as e:
print(f"Error renaming {filename}: {e}")
return False, filename, str(e)
def collect_files_to_rename(self) -> list[Path]:
return [
file_path
for file_path in self.base_dir.iterdir()
if file_path.name.startswith(self.filename_prefix) and file_path.name.endswith(self.filename_extension)
]
async def _sequential_rename(
self,
files_to_rename: list[Path]
) -> tuple[int, int, list[tuple[str, str]]]:
renamed_count = 0
failed_count = 0
failed_files = []
for i, file_path in enumerate(files_to_rename, 1):
filename = os.path.basename(file_path)
success, result_filename, message = await self.analyze_and_rename(file_path,
filename) # TODO: fix Expected type 'Path', got 'str' instead
if success:
renamed_count += 1
else:
failed_count += 1
failed_files.append((filename, message))
if i % 10 == 0 or i == len(files_to_rename):
print(f"Progress: {renamed_count} renamed, {failed_count} failed")
return renamed_count, failed_count, failed_files
async def _parallel_rename(
self,
files_to_rename: list[Path],
parallel_count: int
) -> tuple[int, int, list[tuple[str, str]]]:
semaphore = asyncio.Semaphore(parallel_count)
async def rename_with_semaphore(file_path: Path):
async with semaphore:
filename = os.path.basename(file_path)
return await self.analyze_and_rename(file_path, filename)
results = await asyncio.gather(*[rename_with_semaphore(file_path) for file_path in files_to_rename])
renamed_count = sum(1 for success, _, _ in results if success)
failed_count = sum(1 for success, _, _ in results if not success)
failed_files = [(filename, message) for success, filename, message in zip(
[r[0] for r in results],
[os.path.basename(f) for f in files_to_rename],
[r[2] for r in results]
) if not success]
return renamed_count, failed_count, failed_files
async def rename_files(
self,
parallel_count: int = 0
) -> tuple[int, int, list[tuple[str, str]]]:
files_to_rename = self.collect_files_to_rename()
if not files_to_rename:
print("No files found to rename.")
return 0, 0, []
print(f"Found {len(files_to_rename)} files to rename")
if parallel_count > 0:
return await self._parallel_rename(files_to_rename, parallel_count)
else:
return await self._sequential_rename(files_to_rename)
async def main():
parser = argparse.ArgumentParser(description="Interactive File Renaming Tool")
parser.add_argument("-a", "--auto", action="store_true",
help="Run in auto mode without interactive prompts")
parser.add_argument("-d", "--directory",
help="Specify directory non-interactively")
parser.add_argument('-m', '--model', help='Specify the Ollama model')
parser.add_argument('-c', '--context', type=int, help='Specify the context length')
parser.add_argument('--prefix', help="Specify the file prefix")
parser.add_argument('--ext', help="Specify the file extension")
parser.add_argument('-p', '--parallel', type=int, default=0,
help='Number of parallel rename operations (default: sequential)')
args = parser.parse_args()
try:
renamer = InteractiveFileRenamer(
base_dir=args.directory,
auto_mode=args.auto,
model_name=args.model,
context_length=args.context,
filename_prefix=args.prefix,
filename_extension=args.ext,
)
renamer.interactive_configuration()
try:
renamed_count, failed_count, failed_files = await renamer.rename_files(args.parallel)
print("\n--- Renaming Operation Summary ---")
print(f"Total files processed: {renamed_count + failed_count}")
print(f"Successfully renamed: {renamed_count}")
print(f"Failed to rename: {failed_count}")
if failed_files:
print("\nFiles that failed to rename:")
for file, error in failed_files:
print(f" - {file}: {error}")
except KeyboardInterrupt:
print("\nOperation interrupted by user.")
except Exception as e:
if "ollama" in str(e).lower():
logger.critical(f"A fatal error occurred during model interaction: {e}. Aborting.")
sys.exit(1)
else:
logger.error(f"An unexpected error occurred: {e}")
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
asyncio.run(main())