Skip to content

Instantly share code, notes, and snippets.

@twobob
Created October 2, 2025 20:12
Show Gist options
  • Select an option

  • Save twobob/ad509162d15fef2a150dd24f0b0a9b3c to your computer and use it in GitHub Desktop.

Select an option

Save twobob/ad509162d15fef2a150dd24f0b0a9b3c to your computer and use it in GitHub Desktop.
Follows all symbolic links in a given source directory, copies the target files (blobs) to a new 'MODEL' directory, and names the copies after the original symlinks. So rebuild a gguf from a huggingface blob dir
import os
import shutil
from pathlib import Path
def follow_and_copy_symlinks(source_directory: str, model_directory_name: str = "MODEL"):
"""
Follows all symbolic links in a given source directory, copies the target
files (blobs) to a new 'MODEL' directory, and names the copies after
the original symlinks.
Args:
source_directory (str): The path to the directory containing the symlinks.
model_directory_name (str): The name of the new directory to store the
copied files. Defaults to "MODEL".
"""
# Use pathlib for clean path manipulation
source_path = Path(source_directory)
# The new 'MODEL' directory will be created in the parent of the source directory.
model_path = source_path.parent / model_directory_name
# Create the destination directory if it doesn't exist.
# The 'exist_ok=True' flag prevents an error if the directory is already present.
try:
os.makedirs(model_path, exist_ok=True)
print(f"Created or verified directory: '{model_path}'")
except OSError as e:
print(f"Error creating directory '{model_path}': {e}")
return
# Check if the source directory exists.
if not source_path.is_dir():
print(f"Error: Source directory '{source_directory}' does not exist.")
return
# Iterate over all entries in the source directory.
for entry in os.listdir(source_path):
entry_path = source_path / entry
# Check if the entry is a symbolic link.
if entry_path.is_symlink():
try:
# Use os.path.realpath() to resolve all symlinks and get the
# absolute path of the target file. This is the most robust way
# to handle both relative and absolute symlinks.
blob_path = os.path.realpath(entry_path)
# Construct the destination path using the symlink's original name.
destination_path = model_path / entry
# Check if the resolved blob file exists before attempting to copy.
if not os.path.exists(blob_path):
print(f"Warning: Target file for symlink '{entry}' not found at '{blob_path}'. Skipping.")
continue
# Copy the file. shutil.copy2 also copies metadata like modification times.
shutil.copy2(blob_path, destination_path)
print(f"Copied '{entry}' -> '{destination_path}'")
except OSError as e:
print(f"Error processing symlink '{entry}': {e}")
print("\nScript finished.")
if __name__ == "__main__":
# The user-specified directory path. pick yours
huggingface_directory = r"D:\.cache\huggingface\hub\models--WestZhang--VibeVoice-Large-pt\snapshots\c1ae159a62b0e60972b57b8dc585962b76b61079"
follow_and_copy_symlinks(huggingface_directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment