import os import time from fileinput import filename repo_location = '/Users/fishy/custom_fonts' font_location = f'{repo_location}/font_files' bad_phrases = [' Free', '-medium', '-Medium', '_3', ' by jeremy vessey', ' teenage foundry', ' font', 'TBJ ', ' - Zarma Type', ' Only', ' Personal use', ' PERSONAL USE', ' SVG', ' Free Non-Commercial Use', ' Free for Personal Use', ' Free for personal use', ' Personal Use', ' Personl Use Only', ' Personl Use', ' Free To Try Personal Use Only', ' - Personal Use Only', ' - DCU', ' - Demo', ' - Free for Personal Use', ' - Free for personal use', ' - Free for personal use only', ' Demo', ' DCU', ' To Try', ' - Free Personal Use Only', ' - Free Personal Use', ' - Personal use Only', ' -', "'", 'Non-Commercial Use', ] def remove_ds_store_files(path=font_location): """ Recursively removes .DS_Store files within the given path. Args: path (str): The path to the directory to be checked and cleaned. Returns: None """ for root, dirs, files in os.walk(path): for file in files: if file == '.DS_Store': file_path = os.path.join(root, file) os.remove(file_path) print(f"Removed .DS_Store file: {file_path}") def remove_empty_folders(path=font_location): """ Recursively removes empty directories within the given path, except for .git and .env directories and their subdirectories. Args: path (str): The path to the directory to be checked and cleaned. Returns: None """ for root, dirs, files in os.walk(path, topdown=False): for dir in dirs: dir_path = os.path.join(root, dir) if '.git' in dir_path.split(os.sep) or '.env' in dir_path.split(os.sep) or '.fontfoldercleanup' in dir_path.split(os.sep): continue if not os.listdir(dir_path): os.rmdir(dir_path) print(f"Removed empty folder: {dir_path}") # Usage def remove_bad_phrases(path=font_location): """ Recursively removes bad phrases from the names of directories at the root of the given path. Args: path (str): The path to the directory to be checked and cleaned. Returns: None """ while True: removed_any = False for root, dirs, files in os.walk(path): for dir in dirs: dir_path = os.path.join(root, dir) for phrase in bad_phrases: if phrase in dir: new_dir = dir.replace(phrase, '') new_dir_path = os.path.join(root, new_dir) os.rename(dir_path, new_dir_path) print(f"Renamed directory: {dir_path} -> {new_dir_path}") removed_any = True break # Exit the loop after renaming to avoid modifying the same directory multiple times if not removed_any: break # time.sleep(1) # Delay to allow the file system to settle def collect_orphaned_fonts(path=font_location): """ Collects orphaned font files at the root of the given path. Args: path (str): The path to the directory to be checked for orphaned font files. Returns: list: A list of orphaned font files. """ orphaned_fonts = [] for file in os.listdir(path): file_path = os.path.join(path, file) if os.path.isfile(file_path) and (file.endswith('.otf') or file.endswith('.ttf')): # Create a directory for the orphaned font orphan_folder = os.path.join(path, os.path.splitext(file)[0]) if not os.path.exists(orphan_folder): os.mkdir(orphan_folder) # Move the font file into the new directory new_path = os.path.join(orphan_folder, file) os.rename(file_path, new_path) orphaned_fonts.append(new_path) print(f"Moved {file_path} to {new_path}") return orphaned_fonts def lowercase_all_the_folders(root_path=font_location): """ Lowercases all the folders recursively in the root_path. Args: root_path (str): The path to the directory to be checked and cleaned. Returns: None """ for root, dirs, files in os.walk(root_path): for dir in dirs: dir_path = os.path.join(root, dir) new_dir_path = os.path.join(root, dir.lower()) os.rename(dir_path, new_dir_path) print(f"Renamed directory: {dir_path} -> {new_dir_path}") def add_font_folders_to_each(root_path=font_location): for dir in os.listdir(root_path): dir_path = os.path.join(root_path, dir) if os.path.isdir(dir_path): if dir.startswith('.'): continue #get recursive list of files in the directory files = os.listdir(dir_path) # print(files) for file in files: # if file extension is otf, create a folder named oft if it doesn't exist, and move the file there if file.endswith('.otf'): new_dir_path = os.path.join(dir_path, 'otf') if not os.path.exists(new_dir_path): os.mkdir(new_dir_path) new_file_path = os.path.join(new_dir_path, file) os.rename(os.path.join(dir_path, file), new_file_path) print(f"Moved {file} to {new_file_path}") # if file extension is ttf, create a folder named oft if it doesn't exist, and move the file there elif file.endswith('.ttf'): new_dir_path = os.path.join(dir_path, 'ttf') if not os.path.exists(new_dir_path): os.mkdir(new_dir_path) new_file_path = os.path.join(new_dir_path, file) os.rename(os.path.join(dir_path, file), new_file_path) print(f"Moved {file} to {new_file_path}") else: # if file is not a font file,move it to a folder called "other" new_dir_path = os.path.join(dir_path, 'other') if not os.path.exists(new_dir_path): os.mkdir(new_dir_path) new_file_path = os.path.join(new_dir_path, file) os.rename(os.path.join(dir_path, file), new_file_path) def unzip_if_no_fonts(root_path=font_location): #if there are no font files, find the zip file and unzip it for dir in os.listdir(root_path): dir_path = os.path.join(root_path, dir) if os.path.isdir(dir_path): if dir.startswith('.'): continue #get recursive list of files in the directory files = os.listdir(dir_path) # print(files) font_files = [] for file in files: if file.endswith('.otf') or file.endswith('.ttf'): font_files.append(file) if len(font_files) == 0: zip_files = [] for file in files: if file.endswith('.zip'): zip_files.append(file) if len(zip_files) == 1: zip_file = zip_files[0] zip_file_path = os.path.join(dir_path, zip_file) os.system(f'unzip {zip_file_path} -d {dir_path}') print(f"Unzipped {zip_file_path}") def remove_setup_complete_files(root_path=font_location): # walk through the root path directory and remove all files named ".setup_complete" for root, dirs, files in os.walk(root_path): for file in files: if file == '.setup_complete': file_path = os.path.join(root, file) os.remove(file_path) print(f"Removed .setup_complete file: {file_path}") def remove_spaces_in_dir_names(root_path=font_location): for root, dirs, files in os.walk(root_path, topdown=False): for dir in dirs: dir_path = os.path.join(root, dir) if ' ' in dir: new_dir = dir.replace(' ', '-') #lowercase the new directory name new_dir = new_dir.lower() new_dir_path = os.path.join(root, new_dir) os.rename(dir_path, new_dir_path) print(f"Renamed directory: {dir_path} -> {new_dir_path}") def prepend_folder_name_to_font_dash(root_path=font_location): for dir in os.listdir(root_path): dir_path = os.path.join(root_path, dir) if os.path.isdir(dir_path): #if the directory doesn't start with 'font-', already, prepend it if not dir.startswith('font-'): new_dir = f'font-{dir}' new_dir_path = os.path.join(root_path, new_dir) os.rename(dir_path, new_dir_path) print(f"Renamed directory: {dir_path} -> {new_dir_path}") def lowercase_and_dash_file_names(root_path=font_location): for root, dirs, files in os.walk(root_path): for file in files: file_path = os.path.join(root, file) new_file_path = os.path.join(root, file.lower()) # Remove spaces in the file names new_file_path = new_file_path.replace(' ', '-') # Check if the file exists before renaming if os.path.exists(file_path): os.rename(file_path, new_file_path) print(f"Renamed file: {file_path} -> {new_file_path}") else: print(f"File not found: {file_path}") def dont_use_dots_for_font_files(root_path=font_location): for root, dirs, files in os.walk(root_path): for file in files: file_path = os.path.join(root, file) if file_path.endswith('.otf') or file_path.endswith('.ttf') or file_path.endswith('.ttc') or file_path.endswith('.pdf'): new_file_path = None for x in ['.', ' ', '-', '.-','--']: if file_path.startswith(x): new_file_path = file_path.lstrip(x) os.rename(file_path, new_file_path) print(f"Renamed file: {file_path} -> {new_file_path}") new_file_path = None if __name__ == '__main__': remove_ds_store_files() remove_empty_folders() collect_orphaned_fonts() remove_bad_phrases() lowercase_all_the_folders() remove_spaces_in_dir_names() lowercase_and_dash_file_names() dont_use_dots_for_font_files() prepend_folder_name_to_font_dash() remove_ds_store_files() remove_empty_folders()