Files
sources/.tools/archive_folders.py
aka paul e2413ee76c sort
2026-03-09 16:26:43 +01:00

113 lines
3.0 KiB
Python

#!/usr/bin/env python3
import json
import os
import shutil
from pathlib import Path
from typing import Set
INDEX_FILE = "index.json"
ARCHIVE_FOLDER = "archive"
RESERVED_PATHS = {
'.git', '.gitignore', '.github', 'node_modules', '.vscode',
'archive', '.idea', '__pycache__', '.DS_Store', '.env', '.gitea'
}
def load_index() -> Set[str]:
try:
with open(INDEX_FILE, 'r', encoding='utf-8') as f:
data = json.load(f)
print(f"Loaded {INDEX_FILE}")
indexed_folders = set()
for key in data.keys():
folder = key.split('/')[0]
indexed_folders.add(folder)
print(f"Found {len(indexed_folders)} indexed folders")
return indexed_folders
except FileNotFoundError:
print(f"Error: {INDEX_FILE} not found")
raise
except json.JSONDecodeError as e:
print(f"Error parsing {INDEX_FILE}: {e}")
raise
def get_all_folders() -> Set[str]:
all_folders = set()
try:
for item in os.listdir('.'):
item_path = os.path.join('.', item)
if os.path.isdir(item_path) and item not in RESERVED_PATHS:
all_folders.add(item)
print(f"Found {len(all_folders)} total folders in repo")
return all_folders
except Exception as e:
print(f"Error scanning directories: {e}")
raise
def create_archive_folder() -> None:
if not os.path.exists(ARCHIVE_FOLDER):
os.makedirs(ARCHIVE_FOLDER)
print(f"Created '{ARCHIVE_FOLDER}' folder")
else:
print(f"'{ARCHIVE_FOLDER}' folder already exists")
def archive_folders(indexed_folders: Set[str], all_folders: Set[str]) -> None:
folders_to_archive = all_folders - indexed_folders
if not folders_to_archive:
print(f"\nAll folders are indexed, nothing to archive")
return
print(f"\nFound {len(folders_to_archive)} folders to archive:")
for folder in sorted(folders_to_archive):
source = os.path.join('.', folder)
destination = os.path.join(ARCHIVE_FOLDER, folder)
try:
if os.path.exists(destination):
print(f"Skipping '{folder}' (already exists in archive)")
continue
shutil.move(source, destination)
print(f"Archived: {folder}")
except Exception as e:
print(f"Error archiving '{folder}': {e}")
def main():
"""Main execution function."""
print("=" * 60)
print("FOLDER ARCHIVAL SCRIPT")
print("=" * 60)
print()
try:
indexed_folders = load_index()
all_folders = get_all_folders()
create_archive_folder()
archive_folders(indexed_folders, all_folders)
print("\n" + "=" * 60)
print("ARCHIVAL COMPLETE")
print("=" * 60)
except Exception as e:
print(f"\nScript failed: {e}")
return 1
return 0
if __name__ == "__main__":
exit(main())