Files
novafarma/scripts/restore_all_raw.py
2026-01-20 01:05:17 +01:00

134 lines
4.4 KiB
Python

import os
import shutil
import re
# Configuration
SOURCE_DIRS = [
"assets_BACKUP_20260112_064319",
"src/assets/library",
"assets",
"godot",
"MOJE_SLIKE_KONCNA" # Also include what we just sorted to be sure nothing is lost
]
DEST_ROOT = "assets/slike"
EXCLUDE_DIRS = {".git", "node_modules", ".agent", ".vscode", "assets/slike"} # Don't recurse into dest
# Mapping
MAPPING = {
"zombiji": "animations",
"zombie": "animations",
"undead": "animations",
"liki": "liki",
"kai": "liki",
"ana": "liki",
"gronk": "liki",
"teren": "teren",
"ground": "teren",
"grass": "teren",
"dirt": "teren",
"water": "teren",
"soil": "teren",
"okolje": "teren", # Merging as per previous logic
"tree": "teren",
"stone": "teren",
"rock": "teren",
}
def sanitize_filename(name):
# Replace non-alphanumeric (except ._-) with _
return re.sub(r'[^a-zA-Z0-9._-]', '_', name)
def get_unique_path(path):
if not os.path.exists(path):
return path
base, ext = os.path.splitext(path)
counter = 1
while os.path.exists(f"{base}_{counter}{ext}"):
counter += 1
return f"{base}_{counter}{ext}"
def restore_all():
print("🚀 STARTING MASS TRANSFER (ALL 15k+ IMAGES)...")
if not os.path.exists(DEST_ROOT):
os.makedirs(DEST_ROOT)
count = 0
for source_dir in SOURCE_DIRS:
if not os.path.exists(source_dir):
continue
print(f"📦 Scanning {source_dir}...")
for root, dirs, files in os.walk(source_dir):
# Block recursion into our destination
if os.path.abspath(root).startswith(os.path.abspath(DEST_ROOT)):
continue
dirs[:] = [d for d in dirs if d not in EXCLUDE_DIRS]
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
src_path = os.path.join(root, file)
# 1. Determine Category
fname_lower = file.lower()
path_lower = src_path.lower()
target_sub = "MASTER_REFS" # Default
for key, val in MAPPING.items():
if key in fname_lower or key in path_lower:
target_sub = val
break
# 2. Generate Unique Filename (Flattening path to avoid collisions but keep context)
# e.g. godot/zombiji/walk.png -> godot_zombiji_walk.png
# Strip Source Root from path to get relative structure
rel_path = os.path.relpath(root, source_dir)
if rel_path == ".":
rel_path = ""
# Construct prefix
prefix = f"{source_dir}_{rel_path}".replace(os.sep, "_")
prefix = sanitize_filename(prefix)
new_filename = f"{prefix}_{file}"
# Shorten if too long (filesystem limits)
if len(new_filename) > 200:
new_filename = new_filename[-200:]
dest_dir = os.path.join(DEST_ROOT, target_sub)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
dest_path = os.path.join(dest_dir, new_filename)
# Dedupe
dest_path = get_unique_path(dest_path)
try:
shutil.copy2(src_path, dest_path)
count += 1
if count % 1000 == 0:
print(f" Processed {count} images...")
except Exception as e:
print(f"❌ Error {file}: {e}")
print("\n" + "="*40)
print(f"✅ MASS TRANSFER COMPLETE.")
print(f"Total Images in assets/slike: {count}")
print("="*40)
# Final Stats
for sub in ["animations", "liki", "teren", "MASTER_REFS"]:
p = os.path.join(DEST_ROOT, sub)
if os.path.exists(p):
c = len([f for f in os.listdir(p) if f.lower().endswith(('.png', '.jpg'))])
print(f" - {sub}: {c}")
if __name__ == "__main__":
restore_all()