Skip to content

Instantly share code, notes, and snippets.

@qh-huang
Last active July 24, 2023 11:39
Show Gist options
  • Save qh-huang/f20298fbf1bdf97e4478748bec330bf3 to your computer and use it in GitHub Desktop.
Save qh-huang/f20298fbf1bdf97e4478748bec330bf3 to your computer and use it in GitHub Desktop.
Trim tools for large code base trim to avoid massive processing time for other software to process
#!/usr/bin/env python3
import os
import sys
from tqdm import tqdm # Import tqdm for the progress bar
def is_code_file(file_path):
code_file_names = {"Makefile", "configure", "Kconfig", ".gitignore", ".gitmodules", "CMakeFile"} # Add more conventional file names as needed
code_file_extensions = {".c", ".cc", ".cpp", ".h", ".hh", ".hpp", ".inc", ".java", ".dts", ".dtsi", ".log", ".conf",
".ini", ".xml", ".yaml", ".xls", ".csv", ".js", ".py", ".s", ".gitmodules", ".cmake", ".gn", ".md", ".m4", ".m", ".diff",
".mk", ".mak", ".sh", ".json", ".txt", ".po"} # Add more extensions as needed
if os.path.basename(file_path) in code_file_names:
return True
_, file_extension = os.path.splitext(file_path)
file_extension = file_extension.lower()
return file_extension in code_file_extensions
def delete_non_code_files(directory_path):
total_files = 0
for root, dirs, files in os.walk(directory_path):
total_files += len(files)
with tqdm(total=total_files, desc="Deleting Non-code Files", unit="file") as pbar:
for root, dirs, files in os.walk(directory_path):
for filename in files:
file_path = os.path.join(root, filename)
if not is_code_file(file_path):
try:
os.remove(file_path)
print(f"Deleted file: {file_path}")
except OSError as e:
print(f"Error deleting {file_path}: {e}")
pbar.update(1)
def display_usage():
print("Usage: python script.py <directory_path>")
print("This script walks through the specified directory and deletes non-code files.")
if __name__ == "__main__":
if len(sys.argv) != 2 or sys.argv[1] in ("-h", "--help"):
display_usage()
sys.exit(1)
directory_path = sys.argv[1]
delete_non_code_files(directory_path)
#!/usr/bin/env python3
import os
import sys
from tqdm import tqdm # Import tqdm for the progress bar
def delete_empty_directories(directory_path):
total_dirs = 0
for root, dirs, files in os.walk(directory_path, topdown=False):
total_dirs += len(dirs)
with tqdm(total=total_dirs, desc="Deleting Empty Directories", unit="dir") as pbar:
for root, dirs, files in os.walk(directory_path, topdown=False):
for dirname in dirs:
dir_path = os.path.join(root, dirname)
if os.path.islink(dir_path):
# If the directory is a symbolic link, just unlink it
os.unlink(dir_path)
print(f"Unlinked symbolic link: {dir_path}")
elif not os.listdir(dir_path): # Check if the directory is empty
try:
os.rmdir(dir_path)
print(f"Deleted empty directory: {dir_path}")
except OSError as e:
print(f"Error deleting {dir_path}: {e}")
pbar.update(1)
def display_usage():
print("Usage: python script.py <directory_path>")
print("This script walks through the specified directory and empty directories.")
if __name__ == "__main__":
if len(sys.argv) != 2 or sys.argv[1] in ("-h", "--help"):
display_usage()
sys.exit(1)
directory_path = sys.argv[1]
delete_empty_directories(directory_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment