Improve cache clean
This commit is contained in:
parent
9b83bb16cd
commit
faa855eda9
1 changed files with 28 additions and 10 deletions
26
cache.py
26
cache.py
|
@ -5,6 +5,8 @@ from config import config
|
||||||
from wrapper import enforce_wrap
|
from wrapper import enforce_wrap
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
PATHS = ['chroots', 'pacman', 'jumpdrive']
|
||||||
|
|
||||||
|
|
||||||
@click.group(name='cache')
|
@click.group(name='cache')
|
||||||
def cmd_cache():
|
def cmd_cache():
|
||||||
|
@ -12,13 +14,29 @@ def cmd_cache():
|
||||||
|
|
||||||
|
|
||||||
@cmd_cache.command(name='clean')
|
@cmd_cache.command(name='clean')
|
||||||
def cmd_clean():
|
@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()
|
||||||
|
|
||||||
enforce_wrap()
|
enforce_wrap()
|
||||||
for path_name in ['chroots', 'pacman', 'jumpdrive']:
|
|
||||||
dir = config.file['paths'][path_name]
|
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):
|
for file in os.listdir(dir):
|
||||||
path = os.path.join(dir, file)
|
path = os.path.join(dir, file)
|
||||||
logging.debug('Removing "{path}"')
|
logging.debug(f'Removing "{path_name}/{file}"')
|
||||||
if os.path.isdir(path):
|
if os.path.isdir(path):
|
||||||
shutil.rmtree(path)
|
shutil.rmtree(path)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue