Remove deprecated font folder cleanup scripts and add new FontFolderCleaner implementation for organizing font files.

This commit is contained in:
Matt Troutman 2025-05-19 11:57:10 -05:00
parent 8508981848
commit df75b6e955
No known key found for this signature in database
9 changed files with 100 additions and 571 deletions

View file

@ -0,0 +1,100 @@
#!/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':
file_path.unlink()
print(f"Removed .DS_Store file: {file_path}")
return
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()

View file

@ -1,311 +0,0 @@
import os
import time
from fileinput import filename
repo_location = '/Users/fishy/custom_fonts'
os.system(f"cd ")
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
while new_dir.endswith('-'):
new_dir = new_dir[:-1]
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)
extensions = ['.otf', '.ttf', '.ttc', '.pdf', '.woff', '.woff2']
if any(file_path.endswith(ext) for ext in extensions):
new_file_path = None
for x in ['.','_', ' ', '-', '.-','--']:
if file.startswith(x):
new_file_path = os.path.join(root, file.lstrip(x))
if not os.path.exists(new_file_path):
os.rename(file_path, new_file_path)
print(f"Renamed file: {file_path} -> {new_file_path}")
else:
print(f"Skipped renaming {file_path} as {new_file_path} already exists")
new_file_path = None
def remove_double_and_triple_dashes(root_path=font_location):
## remove double and triple dashes from file names
## should loop until no more double or triple dashes are found
for root, dirs, files in os.walk(root_path):
for file in files:
file_path = os.path.join(root, file)
#replace -- or --- with -
if '--' in file or '---' in file:
new_file = file.replace('--','-')
new_file = new_file.replace('---','-')
new_file_path = os.path.join(root, new_file)
os.rename(file_path, new_file_path)
print(f"Renamed file: {file_path} -> {new_file_path}")
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()
prepend_folder_name_to_font_dash()
dont_use_dots_for_font_files()
remove_double_and_triple_dashes()
remove_double_and_triple_dashes()
remove_ds_store_files()
remove_empty_folders()

View file

@ -1,51 +0,0 @@
import os
import subprocess
import sys
def main():
mydir = os.path.dirname(__file__)
# Define the absolute path to the prep script
prep_script = os.path.join(mydir, 'prep 1 font.sh')
# Activate python environment
env_activate = os.path.join(mydir, '.env/bin/activate')
subprocess.run(['source', env_activate], shell=True, check=True)
# Move orphans before we do anything else
orphans_script = os.path.join(mydir, 'orphans.py')
subprocess.run(['python3', orphans_script], check=True)
# Check if the prep script exists
if not os.path.isfile(prep_script):
print(f"Prep script not found: {prep_script}")
sys.exit(1)
# Ensure the prep script is executable
os.chmod(prep_script, 0o755)
# Check if the prep script is executable
if not os.access(prep_script, os.X_OK):
print(f"Prep script not executable: {prep_script}")
sys.exit(1)
# Define the absolute path to the font_files directory
font_files_dir = os.path.join(mydir, 'font_files')
# Check if the font_files directory exists
if not os.path.isdir(font_files_dir):
print(f"font_files directory not found: {font_files_dir}")
sys.exit(1)
# Iterate over each folder in font_files
for folder in os.listdir(font_files_dir):
folder_path = os.path.join(font_files_dir, folder)
if os.path.isdir(folder_path):
os.chdir(folder_path)
subprocess.run(['zsh', prep_script], check=True)
os.chdir('..')
else:
print(f"No such directory: {folder}")
if __name__ == '__main__':
main()

View file

@ -1,47 +0,0 @@
#!/bin/zsh
mydir=$(dirname "$0")
# Define the absolute path to the prep script
PREP_SCRIPT="$(cd "$(dirname "$0")" && pwd)/prep 1 font.sh"
#activate python environment
source "$mydir"/.env/bin/activate
#move orphans before we do anything else
python3 "$mydir"/orphans.py
# Check if the prep script exists
if [[ ! -f "$PREP_SCRIPT" ]]; then
echo "Prep script not found: $PREP_SCRIPT"
exit 1
fi
# Ensure the prep script is executable
chmod u+x "$PREP_SCRIPT"
# Check if the prep script is executable
if [[ ! -x "$PREP_SCRIPT" ]]; then
echo "Prep script not executable: $PREP_SCRIPT"
exit 1
fi
# Define the absolute path to the font_files directory
FONT_FILES_DIR="$(cd "$(dirname "$0")" && pwd)/font_files"
# Check if the font_files directory exists
if [[ ! -d "$FONT_FILES_DIR" ]]; then
echo "font_files directory not found: $FONT_FILES_DIR"
exit 1
fi
# Iterate over each folder in font_files
for folder in "$FONT_FILES_DIR"/*; do
if [[ -d "$folder" ]]; then
cd "$folder" || continue
zsh "$PREP_SCRIPT"
cd ..
else
echo "No such directory: $folder"
fi
done

View file

@ -1,21 +0,0 @@
import os
# Define the absolute path to the font_files directory
FONT_FILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'font_files')
# find all files that are otfs or ttfs at the root of the font_files directory
def handle_orphans(font_files_dir=FONT_FILES_DIR):
FONT_FILES = [file for file in os.listdir(FONT_FILES_DIR) if file.endswith('.otf') or file.endswith('.ttf')]
print(FONT_FILES)
for file in FONT_FILES:
#create a directory for the file
new_dir_path = os.path.join(FONT_FILES_DIR, file.split('.')[0])
if not os.path.exists(new_dir_path):
os.mkdir(new_dir_path)
print(f"Created directory: {new_dir_path}")
#move the file to the directory
new_file_path = os.path.join(new_dir_path, file)
os.rename(os.path.join(FONT_FILES_DIR, file), new_file_path)
print(f"Moved {file} to {new_file_path}")
if __name__ == '__main__':
handle_orphans()

View file

@ -1,48 +0,0 @@
#! /bin/zsh
#uncomment if you want to delete all .setup_complete files and process everything
#find . -name '*.setup_complete' -type f -delete
#if file name .setup_complete exists, exit
if [ -f .setup_complete ]; then
echo "This folder has already been prepped. Please delete the .setup_complete file if you would like to run this script again."
exit
fi
#if current working directory is named font_files or custom_fonts, exit
if [[ $PWD == *"font_files" ]] || [[ $PWD == *"custom_fonts" ]]; then
echo "You are in the wrong directory. Please move to the individual font folder."
exit
fi
#recursively find all .zip files and unzip them
find . -name '*.zip' -exec unzip {} \;
# Make otf and ttf folders if they don't exist
mkdir -p otf ttf "other files"
# Recurse through all folders and subfolders and move files that are not .ttf or .otf into the "other files" folder
find . -type f -exec mv {} "other files" \;
# Recurse through all folders and subfolders and delete all .DS_Store files
find . -name '.DS_Store' -type f -delete
# Recurse through all folders and subfolders and delete all .g2n files
find . -name '*.g2n' -type f -delete
find . -name '*.ofm' -type f -delete
find . -name '*.cfg' -type f -delete
find . -name '*.zip' -type f -delete
# Recurse through all folders and subfolders and move all .ttf files into the ttf folder
find . -name '*.ttf' -type f -exec mv {} ttf \;
# Recurse through all folders and subfolders and move all .ttc files into the ttf folder
find . -name '*.ttc' -type f -exec mv {} ttf \;
# Recurse through all folders and subfolders and move all .otf files into the otf folder
find . -name '*.otf' -type f -exec mv {} otf \;
# Recurse through all folders and subfolders and delete all empty folders
find . -type d -empty -delete
# create a .setup_complete file to indicate that the folder has been prepped
touch .setup_complete

View file

@ -1,17 +0,0 @@
import font_folder_cleanup
import os
def git_cleanup():
os.system("cd $(git rev-parse --show-toplevel) && git stash push -- .fontfoldercleanup/ && git reset --hard && git clean -fdx -e .env/ -e .idea/ -e .fontfoldercleanup/")
def apply_stash():
os.system("cd $(git rev-parse --show-toplevel) && git stash apply")
if __name__ == '__main__':
# Clean up git repository
git_cleanup() # Clean up git repository
font_folder_cleanup.remove_ds_store_files() # Remove .DS_Store files from the repository
font_folder_cleanup.remove_setup_complete_files() # Remove setup complete files from the repository
font_folder_cleanup.remove_empty_folders() # Remove empty folders from the repository
git_cleanup() # Clean up git repository again after removing files and folders
apply_stash() # Apply the stash to restore the files that were removed

View file

@ -1,76 +0,0 @@
import requests, os
GITEA_URL = "https://git.trtmn.io"
API_TOKEN = "315d5a6d692eeea061f714725f0d116b819ea62b"
organization = "fonts"
headers = {
"Authorization": f"token {API_TOKEN}",
"Content-Type": "application/json"
}
def get_folders_in_folder(path="./font_files"):
return [file for file in os.listdir(path) if os.path.isdir(os.path.join(path, file))]
folders = ["folder-1", "folder-2", "folder-3", "folder-4", "folder-5"]
# def make_local_repos(folder_list=folders):
# for folder in folder_list:
# # Create a local folder
# print(f"Creating folder {folder}")
# os.makedirs(folder, exist_ok=True)
# # Create a README.md file in the folder
# with open(f"{folder}/README.md", "w") as f:
# f.write(f"# {folder}\n\nThis is a repo for {folder}")
# #git init
# os.system(f"cd {folder} && git init")
# #git create branch "main" and switch to it
# os.system(f"cd {folder} && git checkout -b main")
# #git add README.md
# os.system(f"cd {folder} && git add README.md")
# #git commit -m "Initial commit"
# os.system(f"cd {folder} && git commit -m 'Initial commit'")
# #git remote add origin
# os.system(f"cd {folder} && git remote add origin {GITEA_URL}/{organization}/{folder}.git")
# #git push -u origin master
# os.system(f"cd {folder} && git push --force -u origin main")
# def clone_repos(folder_list=folders):
# for folder in folder_list:
# os.system(f"git clone {GITEA_URL}/{organization}/{folder}.git")
def add_as_submodule(folder_list=folders):
for folder in folder_list:
os.system(f"git submodule add {GITEA_URL}/{organization}/{folder}.git")
def create_repos(folder_list=folders):
for folder in folder_list:
data = {
"name": folder,
"description": f"Repo for {folder}",
"private": False,
"auto_init": True
}
response = requests.post(f"{GITEA_URL}/api/v1/orgs/{organization}/repos", json=data, headers=headers)
if response.status_code == 201:
print(f"Successfully created repo for {folder}")
else:
print(f"Failed to create repo for {folder}: {response.status_code} {response.text}")
def delete_repos(folder_list=folders):
for folder in folder_list:
response = requests.delete(f"{GITEA_URL}/api/v1/repos/{organization}/{folder}", headers=headers)
if response.status_code == 204:
print(f"Successfully deleted repo for {folder}")
else:
print(f"Failed to delete repo for {folder}: {response.status_code} {response.text}")
if __name__ == '__main__':
# create_repos()
# make_local_repos()
# add_as_submodule()
# delete_repos()
print(get_folders_in_folder())