From d7fe57b9d0c888747395d2aeedb98ba2713895e5 Mon Sep 17 00:00:00 2001 From: Matt Troutman Date: Thu, 12 Sep 2024 01:05:03 -0500 Subject: [PATCH] Adding Cleanup script --- .fontfoldercleanup/__init__.py | 0 .fontfoldercleanup/cleanup.py | 150 +++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 .fontfoldercleanup/__init__.py create mode 100644 .fontfoldercleanup/cleanup.py diff --git a/.fontfoldercleanup/__init__.py b/.fontfoldercleanup/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/.fontfoldercleanup/cleanup.py b/.fontfoldercleanup/cleanup.py new file mode 100644 index 0000000..1f64dd0 --- /dev/null +++ b/.fontfoldercleanup/cleanup.py @@ -0,0 +1,150 @@ +import os +import time +from fileinput import filename + +bad_phrases = [' Free', + '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): + """ + 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): + """ + 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): + continue + if not os.listdir(dir_path): + os.rmdir(dir_path) + print(f"Removed empty folder: {dir_path}") + +# Usage +def remove_bad_phrases(path): + """ + 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): + """ + 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): + """ + Lowercases all the folders in the root path. + + Args: + root_path (str): The path to the directory to be checked and cleaned. + + Returns: + None + """ + for dir in os.listdir(root_path): + dir_path = os.path.join(root_path, dir) + if os.path.isdir(dir_path): + new_dir_path = os.path.join(root_path, dir.lower()) + os.rename(dir_path, new_dir_path) + print(f"Renamed directory: {dir_path} -> {new_dir_path}") + +if __name__ == '__main__': + + remove_ds_store_files('/Users/fishy/custom_fonts/') + remove_empty_folders('/Users/fishy/custom_fonts/') + remove_bad_phrases('/Users/fishy/custom_fonts/') + collect_orphaned_fonts('/Users/fishy/custom_fonts/') + lowercase_all_the_folders('/Users/fishy/custom_fonts/') \ No newline at end of file