#!/usr/bin/env python3 import os import shutil import argparse from pathlib import Path class FontFolderCleaner: def __init__(self, font_location): self.font_location = Path(font_location) self.required_dirs = ['ttf', 'otf', 'web', 'other_files'] self.web_extensions = {'.woff', '.woff2', '.eot', '.svg'} self.font_extensions = {'.ttf', '.otf'} def create_required_directories(self, folder_path): """Create required subdirectories if they don't exist.""" for dir_name in self.required_dirs: dir_path = folder_path / dir_name if not dir_path.exists(): dir_path.mkdir(parents=True) print(f"Created directory: {dir_path}") def get_target_directory(self, file_path): """Determine the target directory based on file extension.""" ext = file_path.suffix.lower() if ext == '.ttf': return 'ttf' elif ext == '.otf': return 'otf' elif ext in self.web_extensions: return 'web' else: return 'other_files' def move_file_to_correct_directory(self, file_path, folder_path): """Move file to the appropriate subdirectory.""" if file_path.name == '.DS_Store' or file_path.name.startswith('._'): file_path.unlink() print(f"Removed macOS metadata file: {file_path}") return # Remove AppleDouble resource fork files (start with _ and aren't real fonts) if file_path.name.startswith('_') and file_path.suffix.lower() in ( '.ttf', '.otf', '.ttc', '.woff', '.woff2', '.eot', '.svg', ): # Check if it's an AppleDouble file by reading magic bytes try: with open(file_path, 'rb') as f: magic = f.read(4) if magic == b'\x00\x05\x16\x07': # AppleDouble magic number file_path.unlink() print(f"Removed AppleDouble resource fork: {file_path}") return except OSError: pass target_dir = self.get_target_directory(file_path) target_path = folder_path / target_dir / file_path.name if file_path != target_path: shutil.move(str(file_path), str(target_path)) print(f"Moved {file_path.name} to {target_dir}/") def cleanup_folder(self, folder_path): """Clean up a single font folder.""" folder_path = Path(folder_path) # Skip if not a font folder if not folder_path.name.startswith('font-'): print(f"Skipping non-font folder: {folder_path}") return print(f"\nProcessing folder: {folder_path}") # Create required directories self.create_required_directories(folder_path) # Move files to appropriate directories for item in folder_path.iterdir(): if item.is_file(): self.move_file_to_correct_directory(item, folder_path) elif item.is_dir() and item.name not in self.required_dirs: # Handle nested directories for file in item.rglob('*'): if file.is_file(): self.move_file_to_correct_directory(file, folder_path) # Remove empty directories if not any(item.iterdir()): item.rmdir() print(f"Removed empty directory: {item}") def cleanup_all_folders(self): """Clean up all font folders in the font location.""" for item in self.font_location.iterdir(): if item.is_dir() and not item.name.startswith('.'): self.cleanup_folder(item) def main(): parser = argparse.ArgumentParser(description='Clean up font folders and organize files.') parser.add_argument('--path', type=str, default='../font_files', help='Path to the font files directory (default: ../font_files)') args = parser.parse_args() # Convert relative path to absolute path font_location = Path(__file__).parent / args.path font_location = font_location.resolve() if not font_location.exists(): print(f"Error: Font location does not exist: {font_location}") return cleaner = FontFolderCleaner(font_location) cleaner.cleanup_all_folders() if __name__ == "__main__": main()