|
| 1 | +from os import PathLike, remove |
| 2 | +from os.path import exists, isdir |
| 3 | +from shutil import rmtree |
| 4 | + |
| 5 | +import click |
| 6 | + |
| 7 | + |
| 8 | +__version__ = "0.0.1-dev0" |
| 9 | + |
| 10 | + |
| 11 | +@click.command() |
| 12 | +@click.help_option("--help", "-h") |
| 13 | +@click.option("--verbose", "-v", is_flag=True, default=False) |
| 14 | +@click.option("--version", "-V", is_flag=True, default=False) |
| 15 | +@click.argument("PATH", nargs=-1, type=click.Path()) |
| 16 | +def main(path, verbose, version): |
| 17 | + """This utility is meant for use in makefiles as the clean target or similar usecases. |
| 18 | + It takes an arbitray number of PATH objects and removes them if they exists. |
| 19 | + Folders will be remove recursivly.""" |
| 20 | + if version: |
| 21 | + print(f"makeclean.py v{__version__}") |
| 22 | + return |
| 23 | + clean(path, verbose) |
| 24 | + |
| 25 | + |
| 26 | +def clean(path: list[str|PathLike], verbose=False): |
| 27 | + """Removes all given Path-Objects, meaning files are deleted and |
| 28 | + directorys are removed recurlivly. |
| 29 | +
|
| 30 | + Args: |
| 31 | + `path` (list[str|PathLike]): List of Paths to delete. |
| 32 | + `verbose` (bool, optional): If set to `True`, prints informations to the screen. |
| 33 | + """ |
| 34 | + for p in path: |
| 35 | + if not exists(p): |
| 36 | + if verbose: |
| 37 | + click.echo(f'{click.style("skip", fg="yellow")}: {p}') |
| 38 | + continue |
| 39 | + if isdir(p): |
| 40 | + if not p.endswith("/"): |
| 41 | + p += "/" |
| 42 | + if verbose: |
| 43 | + click.echo(f'{click.style("dir", fg="bright_cyan")}: {p}') |
| 44 | + rmtree(p) |
| 45 | + else: |
| 46 | + if verbose: |
| 47 | + click.echo(f'{click.style("file", fg="blue")}: {p}') |
| 48 | + remove(p) |
0 commit comments