Files
novafarma/scripts/final_clean_and_reset.py
2026-01-25 12:20:50 +01:00

96 lines
3.6 KiB
Python

import os
import shutil
import datetime
REPO_DIR = "/Users/davidkotnik/repos/novafarma"
DESKTOP_SOURCE = "/Users/davidkotnik/Desktop/referencne slike 2"
TIMESTAMP = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
BACKUP_DIR = os.path.join(REPO_DIR, f"_BACKUP_OLD_IMAGES_{TIMESTAMP}")
# Extensions to move to backup (Images)
IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp', '.tiff', '.svg'}
# Helper files to move (cleanup clutter)
CLUTTER_EXTENSIONS = {'.log'}
CLUTTER_FILES = {'PREIMENOVANJE_SLIK_POROCILO.md', 'VISUAL_CLEANUP_REPORT.md', 'UNNAMED_IMAGES_REPORT.md', 'TENT_FOUND_REPORT.md', 'TRANSPARENCY_FIX_REPORT.md'}
def is_safe_dir(dir_name):
# Directories to strictly IGNORE (do not touch files inside)
return dir_name not in ['.git', 'node_modules', '.agent', '_BACKUP_OLD_IMAGES_' + TIMESTAMP]
def clean_and_backup():
if not os.path.exists(BACKUP_DIR):
os.makedirs(BACKUP_DIR)
print(f"Created backup directory: {BACKUP_DIR}")
moved_count = 0
# 1. CLEANUP PHASE
for root, dirs, files in os.walk(REPO_DIR):
# Filter out unsafe directories from traversal
dirs[:] = [d for d in dirs if is_safe_dir(d) and not d.startswith('_BACKUP')]
for file in files:
file_path = os.path.join(root, file)
_, ext = os.path.splitext(file)
should_move = False
if ext.lower() in IMAGE_EXTENSIONS:
should_move = True
elif file in CLUTTER_FILES:
should_move = True
if should_move:
# Calculate relative path to maintain structure in backup?
# Or just flat dump to avoid empty folder issues?
# User said "vse slike stran", "clean map". Flat dump or simple structure is fine.
# Let's preserve structure in backup just in case.
rel_path = os.path.relpath(root, REPO_DIR)
dest_dir = os.path.join(BACKUP_DIR, rel_path)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
shutil.move(file_path, os.path.join(dest_dir, file))
moved_count += 1
print(f"Moved to backup: {file}")
# 2. DELETE EMPTY FOLDERS
removed_dirs = 0
for root, dirs, files in os.walk(REPO_DIR, topdown=False):
if root == REPO_DIR:
continue
# Don't delete .git or criticals
if '.git' in root or 'node_modules' in root or '_BACKUP' in root:
continue
try:
# If directory is empty
if not os.listdir(root):
os.rmdir(root)
removed_dirs += 1
print(f"Removed empty dir: {root}")
except OSError:
pass
print(f"\nCleanup Complete. Moved {moved_count} files to backup. Removed {removed_dirs} empty directories.")
# 3. FRESH START POPULATION
# Ensure assets dir exists
ASSETS_DIR = os.path.join(REPO_DIR, "assets")
if not os.path.exists(ASSETS_DIR):
os.makedirs(ASSETS_DIR)
print("\nPopulating assets from Desktop source...")
copied_count = 0
for file in os.listdir(DESKTOP_SOURCE):
if file.lower().endswith(('.jpg', '.png', '.jpeg')):
src = os.path.join(DESKTOP_SOURCE, file)
dst = os.path.join(ASSETS_DIR, file)
shutil.copy2(src, dst)
copied_count += 1
print(f"Copied {copied_count} fresh images to {ASSETS_DIR}")
if __name__ == "__main__":
clean_and_backup()