-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup.py
More file actions
75 lines (60 loc) · 2.77 KB
/
cleanup.py
File metadata and controls
75 lines (60 loc) · 2.77 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
import os
import re
from pathlib import Path
from dotenv import get_key
from datetime import datetime
# Step 1: Retrieve configuration variables using `dotenv.get_key`
config_file = "config.env"
absolute_path = get_key(config_file, "ABSOLUTE_PATH").lower() == "true"
delete_by_date = get_key(config_file, "DELETE_BY_DATE_MODIFIED").lower() == "true"
base_path = get_key(config_file, "PATH")
files_to_keep = int(get_key(config_file, "FILES_TO_KEEP"))
files_pattern = get_key(config_file, "FILES_PATTERN")
log_absolute_path = get_key(config_file, "LOG_ABSOLUTE_PATH").lower() == "true"
log_file_base = get_key(config_file, "LOG_FILE")
# Determine the directory path
if not absolute_path:
base_path = os.path.join(os.getcwd(), base_path)
base_path = Path(base_path)
# Step 2: Validate the directory
if not base_path.exists() or not base_path.is_dir():
print(f"Error: Directory '{base_path}' does not exist.")
exit(1)
# Step 3: Determine log file name and path
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
log_file_name = f"{log_file_base}_{current_time}.log"
if not log_absolute_path:
log_file_name = os.path.join(os.getcwd(), log_file_name)
log_file_path = Path(log_file_name)
log_file_path.parent.mkdir(parents=True, exist_ok=True) # Ensure log directory exists
# Step 4: Find files matching the pattern
pattern = re.compile(files_pattern)
matching_files = [f for f in base_path.iterdir() if f.is_file() and pattern.match(f.name)]
# Step 5: Sort files by modification time
if delete_by_date:
matching_files.sort(key=lambda x: x.stat().st_mtime) # Sort by modified time
else:
matching_files.sort(key=lambda x: x.name) #Sort by Name
# Step 6: Determine files to delete
files_to_delete = matching_files[:-files_to_keep] # Keep the newest files_to_keep files
# Step 7: Delete the oldest files and log the actions
with open(log_file_path, "w") as log_file:
log_file.write(f"Log created on: {datetime.now()}\n")
log_file.write(f"Directory: {base_path}\n")
log_file.write(f"Files to keep: {files_to_keep}\n")
log_file.write(f"Matching pattern: {files_pattern}\n")
log_file.write(f"Sorted by : {'Date Modified' if delete_by_date else 'Name'}\n")
log_file.write("\nDeleted files:\n")
# Log deleted files
for file in files_to_delete:
log_file.write(f"Deleting: {file}\n")
print(f"Deleting: {file}")
file.unlink()
log_file.write("\nKept files:\n")
# Log kept files
files_kept = matching_files[-files_to_keep:] # Files that are kept
for file in files_kept:
log_file.write(f"Kept: {file}\n")
print(f"Kept: {file}")
log_file.write("\nFinished. Kept the latest files.\n")
print(f"Finished. Kept the latest {files_to_keep} files.")