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