-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathbatch_optimizer.py
More file actions
152 lines (124 loc) · 5.36 KB
/
batch_optimizer.py
File metadata and controls
152 lines (124 loc) · 5.36 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
#!/usr/bin/env python3
"""
Batch image optimization script
Does everything automatically - converts images, updates templates, creates backups
Usage:
python batch_optimizer.py
python batch_optimizer.py --no-backup
"""
import os
import sys
import argparse
import subprocess
from pathlib import Path
class BatchOptimizer:
def __init__(self, project_root):
self.project_root = Path(project_root).resolve()
# If invoked from inside website dir, avoid duplicating path
if (self.project_root / "website").exists():
self.website_dir = (self.project_root / "website").resolve()
else:
self.website_dir = self.project_root
self.static_dir = self.website_dir / "custom_static" / "assets" / "img"
self.templates_dir = self.website_dir / "home" / "templates"
def run_command(self, command, description):
"""Runs a command and handles any errors"""
print(f"{description}")
print(f" Command: {' '.join(command)}")
print("-" * 50)
try:
result = subprocess.run(command, cwd=self.website_dir, check=True,
capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print("Warnings/Info:", result.stderr)
return True
except subprocess.CalledProcessError as e:
print(f"Error: {e}")
print(f" stdout: {e.stdout}")
print(f" stderr: {e.stderr}")
return False
def install_dependencies(self):
"""Installs the Python packages we need"""
print("Installing dependencies...")
return self.run_command([
sys.executable, "-m", "pip", "install", "-r", "requirements.txt"
], "Installing all project dependencies")
def backup_original_files(self):
"""Makes a Git backup of the original files"""
print("Creating Git backup...")
# Check if we're in a git repository
if not (self.project_root / ".git").exists():
print("Not in a Git repository. Skipping backup.")
return True
# Create backup branch
commands = [
["git", "checkout", "-b", "image-optimization-backup"],
["git", "add", "custom_static/assets/img/"],
["git", "commit", "-m", "Backup: Original images before optimization"],
["git", "checkout", "main"]
]
for cmd in commands:
if not self.run_command(cmd, f"Running: {' '.join(cmd)}"):
return False
return True
def optimize_all_images(self):
"""Converts all JPG/PNG images to WebP format"""
print("Optimizing all images...")
if not self.static_dir.exists():
print(f"Static directory not found: {self.static_dir}")
return False
return self.run_command([
sys.executable, "image_optimizer.py",
"--convert-webp",
"--input-dir", str(self.static_dir),
"--quality-photos", "80",
"--quality-graphics", "90"
], "Converting all images to WebP")
def minify_all_svg(self):
"""Makes all SVG files smaller"""
print("Minifying SVG files...")
return self.run_command([
sys.executable, "image_optimizer.py",
"--minify-svg",
"--input-dir", str(self.static_dir)
], "Minifying all SVG files")
def update_templates(self):
"""Updates Django templates to use the new WebP images"""
print("Updating template references...")
return self.run_command([
sys.executable, "template_updater.py",
"--templates-dir", str(self.templates_dir),
"--static-dir", str(self.static_dir)
], "Updating Django template references")
def run_optimization(self, skip_backup=False):
"""Runs the complete image optimization process"""
print("Running complete image optimization...")
print("=" * 60)
steps = [
("Installing dependencies", self.install_dependencies),
("Creating backup", self.backup_original_files) if not skip_backup else ("Skipping backup", lambda: True),
("Converting images to WebP", self.optimize_all_images),
("Minifying SVG files", self.minify_all_svg),
("Updating templates", self.update_templates)
]
for step_name, step_func in steps:
print(f"\nStep: {step_name}")
if not step_func():
print(f"Failed at step: {step_name}")
return False
print(f"Completed: {step_name}")
print("\nImage optimization completed successfully!")
return True
def main():
parser = argparse.ArgumentParser(description='Batch image optimization tool')
parser.add_argument('--no-backup', action='store_true', help='Skip creating backup')
parser.add_argument('--project-root', default='.', help='Project root directory')
args = parser.parse_args()
# Create optimizer instance
optimizer = BatchOptimizer(args.project_root)
# Run optimization
success = optimizer.run_optimization(skip_backup=args.no_backup)
return 0 if success else 1
if __name__ == '__main__':
sys.exit(main())