2021-08-04 18:36:37 +02:00
|
|
|
import shutil
|
|
|
|
import click
|
|
|
|
import os
|
2021-09-29 23:18:12 +02:00
|
|
|
from config import config
|
|
|
|
from wrapper import enforce_wrap
|
|
|
|
import logging
|
2021-08-04 18:36:37 +02:00
|
|
|
|
2021-10-25 17:42:31 +02:00
|
|
|
PATHS = ['chroots', 'pacman', 'jumpdrive', 'packages', 'images']
|
2021-09-30 03:49:20 +02:00
|
|
|
|
2021-08-04 18:36:37 +02:00
|
|
|
|
|
|
|
@click.group(name='cache')
|
|
|
|
def cmd_cache():
|
2022-02-13 19:57:04 +01:00
|
|
|
"""Clean caches and chroots"""
|
2021-08-04 18:36:37 +02:00
|
|
|
|
|
|
|
|
2021-09-29 23:18:12 +02:00
|
|
|
@cmd_cache.command(name='clean')
|
2021-09-30 03:49:20 +02:00
|
|
|
@click.option('--force', default=False)
|
|
|
|
@click.argument('paths', nargs=-1, required=False)
|
|
|
|
def cmd_clean(paths: list[str], force=False):
|
|
|
|
if unknown_paths := (set(paths) - set(PATHS + ['all'])):
|
|
|
|
raise Exception(f"Unknown paths: {' ,'.join(unknown_paths)}")
|
|
|
|
if 'all' in paths or (not paths and force):
|
|
|
|
paths = PATHS.copy()
|
|
|
|
|
2021-09-29 23:18:12 +02:00
|
|
|
enforce_wrap()
|
2021-09-30 03:49:20 +02:00
|
|
|
|
|
|
|
clear = {path: (path in paths) for path in PATHS}
|
|
|
|
query = not paths
|
|
|
|
if not query or force:
|
|
|
|
click.confirm(f'Really clear {", ".join(paths)}?', abort=True)
|
|
|
|
for path_name in PATHS:
|
|
|
|
if query:
|
|
|
|
clear[path_name] = click.confirm(f'Clear {path_name}?')
|
|
|
|
if clear[path_name]:
|
|
|
|
logging.info(f'Clearing {path_name}')
|
|
|
|
dir = config.get_path(path_name)
|
|
|
|
for file in os.listdir(dir):
|
|
|
|
path = os.path.join(dir, file)
|
|
|
|
logging.debug(f'Removing "{path_name}/{file}"')
|
|
|
|
if os.path.isdir(path):
|
|
|
|
shutil.rmtree(path)
|
|
|
|
else:
|
|
|
|
os.unlink(path)
|