Created
March 14, 2025 10:00
-
-
Save kibotu/ebaa2bc988b53d9ab10a7a4a5134ec6e to your computer and use it in GitHub Desktop.
Reclaims build folder in a directory. Ignores git folder, too and adds a summary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
import argparse | |
def get_dir_size(dir_path): | |
"""Calculate the total size of a directory in bytes.""" | |
total_size = 0 | |
for dirpath, dirnames, filenames in os.walk(dir_path): | |
for filename in filenames: | |
file_path = os.path.join(dirpath, filename) | |
total_size += os.path.getsize(file_path) | |
return total_size | |
def delete_build_folders(root_dir): | |
total_space_reclaimed = 0 | |
for dirpath, dirnames, filenames in os.walk(root_dir): | |
# Skip if the current path contains '.git' | |
if '.git' in dirpath.split(os.sep): | |
continue | |
if 'build' in dirnames: | |
build_dir_path = os.path.join(dirpath, 'build') | |
# Calculate size before deletion | |
space_to_reclaim = get_dir_size(build_dir_path) | |
total_space_reclaimed += space_to_reclaim | |
print(f"Deleting: {build_dir_path} ({space_to_reclaim / 1024 / 1024:.2f} MB)") | |
shutil.rmtree(build_dir_path) | |
# Print summary | |
if total_space_reclaimed > 0: | |
print(f"\nSummary: Total space reclaimed: {total_space_reclaimed / 1024 / 1024:.2f} MB " | |
f"({total_space_reclaimed / 1024 / 1024 / 1024:.2f} GB)") | |
else: | |
print("\nSummary: No space reclaimed (no build folders deleted)") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Recursively delete all /build folders in a given directory, excluding those in .git folders.") | |
parser.add_argument("root_directory", type=str, help="The root directory to start searching from.") | |
args = parser.parse_args() | |
if os.path.isdir(args.root_directory): | |
delete_build_folders(args.root_directory) | |
else: | |
print("The provided path is not a valid directory.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output: