Skip to content

Instantly share code, notes, and snippets.

@5ec1cff
Created June 12, 2025 06:36
Show Gist options
  • Save 5ec1cff/0c1f84c443769ce5c945cb46f78474c4 to your computer and use it in GitHub Desktop.
Save 5ec1cff/0c1f84c443769ce5c945cb46f78474c4 to your computer and use it in GitHub Desktop.
Build Cleaner
import os
import shutil
from pathlib import Path
from traceback import print_exc
def rmall(p: Path):
print('removing', p)
try:
if os.path.isdir(p):
shutil.rmtree(p)
else:
os.remove(p)
except KeyboardInterrupt:
raise KeyboardInterrupt()
except:
print_exc()
print('remove', p, 'failed')
def rmoutputs(p: Path):
for f in os.listdir(p):
np = p / f
fl = f.lower()
if fl in ['apk', 'mapping']:
continue
rmall(np)
def rmbuild(p: Path):
for f in os.listdir(p):
np = p / f
fl = f.lower()
if fl == 'symbols':
continue
if fl == 'outputs':
rmoutputs(np)
rmall(np)
def traversal(p: Path):
ls = os.listdir(p)
is_gradle = ('build.gradle' in ls) or ('build.gradle.kts' in ls)
is_rust = 'Cargo.toml' in ls
for f in ls:
np = p / f
if is_gradle:
if f == '.cxx':
rmall(np)
continue
elif f == 'build' and os.path.isdir(np):
rmbuild(np)
continue
if is_rust:
if f == 'target':
rmall(np)
if f in ['.pnpm', 'node_modules']:
continue
if os.path.isdir(np):
traversal(np)
if __name__ == '__main__':
import sys
traversal(Path(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment