You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.8 KiB
58 lines
1.8 KiB
#!/usr/bin/env python3 |
|
import os |
|
import gzip |
|
import shutil |
|
from pathlib import Path |
|
|
|
def compress_file(src_path, dst_path): |
|
"""Compress a file using gzip.""" |
|
try: |
|
with open(src_path, 'rb') as f_in: |
|
with gzip.open(dst_path, 'wb', compresslevel=9) as f_out: |
|
shutil.copyfileobj(f_in, f_out) |
|
print(f"✓ Compressed: {src_path} -> {dst_path}") |
|
return True |
|
except Exception as e: |
|
print(f"✗ Error compressing {src_path}: {e}") |
|
return False |
|
|
|
def compress_data_directory(data_dir): |
|
"""Compress CSS, JS, and HTML files in data directory.""" |
|
data_path = Path(data_dir) |
|
|
|
if not data_path.exists(): |
|
print(f"Data directory not found: {data_dir}") |
|
return |
|
|
|
files_to_compress = [] |
|
|
|
# Find all CSS files |
|
files_to_compress.extend(data_path.glob("**/*.css")) |
|
|
|
# Find all JS files |
|
files_to_compress.extend(data_path.glob("**/*.js")) |
|
|
|
# Find all HTML files (excluding .gz files) |
|
files_to_compress.extend(data_path.glob("**/*.html")) |
|
|
|
# Filter out files that are already .gz |
|
files_to_compress = [f for f in files_to_compress if not str(f).endswith('.gz')] |
|
|
|
if not files_to_compress: |
|
print("No files to compress found.") |
|
return |
|
|
|
print(f"Compressing {len(files_to_compress)} files...") |
|
|
|
compressed_count = 0 |
|
for src_file in files_to_compress: |
|
dst_file = Path(str(src_file) + ".gz") |
|
if compress_file(src_file, dst_file): |
|
compressed_count += 1 |
|
|
|
print(f"\n✓ Successfully compressed {compressed_count}/{len(files_to_compress)} files") |
|
|
|
if __name__ == "__main__": |
|
import sys |
|
data_dir = sys.argv[1] if len(sys.argv) > 1 else "data" |
|
compress_data_directory(data_dir)
|
|
|