#!/usr/bin/env python3
# This file is placed in the Public Domain.


"cleanup subdirectories"


import os
import sys


SKIP = ["env", ".git"]


def loop(path, txt):
    old = os.getcwd()
    os.chdir(path)
    for fnn in os.listdir(path):
        if fnn in SKIP:
            continue
        old = os.getcwd()
        fpath = os.path.abspath(os.path.join(path, fnn))
        if os.path.isdir(fpath):
            loop(fpath, txt)
        if not os.path.isdir(fpath):
            continue
        os.chdir(fpath)
        popen(txt)
        os.chdir(old)
    os.chdir(old)


def popen(txt):
    for line in os.popen(txt).readlines():
        print(line.rstrip())
        sys.stdout.flush()


def main():
    popen("rm -fR DEAD* build dist MANIFEST *.egg-info *.whl")
    popen("rm -fR .pytest_cache .test __pycache__ .ruff_cache .mypy_cache")
    popen("rm -fR .*~ *~")
    popen("rm -fR store .venv uv.lock")
    popen("rm -fR env")
    loop(".", "rm -fR *~")
    loop(".", "rm -fR __pycache__")


if __name__ == "__main__":
    main()
