packages/cmd_clean(): make src/ and pkg/ selectable, add -n/--noop

This commit is contained in:
InsanePrawn 2022-02-17 02:34:56 +01:00
parent 2ece1f288c
commit 335cf2bf05

View file

@ -6,6 +6,8 @@ import shutil
import subprocess import subprocess
from copy import deepcopy from copy import deepcopy
from joblib import Parallel, delayed from joblib import Parallel, delayed
from glob import glob
from shutil import rmtree
from constants import REPOSITORIES, CROSSDIRECT_PKGS, QEMU_BINFMT_PKGS, GCC_HOSTSPECS, ARCHES, Arch, CHROOT_PATHS from constants import REPOSITORIES, CROSSDIRECT_PKGS, QEMU_BINFMT_PKGS, GCC_HOSTSPECS, ARCHES, Arch, CHROOT_PATHS
from config import config from config import config
@ -691,24 +693,50 @@ def cmd_sideload(paths: list[str]):
@cmd_packages.command(name='clean') @cmd_packages.command(name='clean')
@click.option('--force', is_flag=True, default=False, help="Don't prompt for confirmation") @click.option('-f', '--force', is_flag=True, default=False, help="Don't prompt for confirmation")
def cmd_clean(force: bool = False): @click.option('-n', '--noop', is_flag=True, default=False, help="Print what would be removed but dont execute")
@click.argument('what', type=click.Choice(['all', 'src', 'pkg']), nargs=-1)
def cmd_clean(what: list[str] = ['all'], force: bool = False, noop: bool = False):
"""Remove files and directories not tracked in PKGBUILDs.git""" """Remove files and directories not tracked in PKGBUILDs.git"""
enforce_wrap() enforce_wrap()
if noop:
logging.debug('Running in noop mode!')
if force:
logging.debug('Running in FORCE mode!')
pkgbuilds = config.get_path('pkgbuilds')
if 'all' in what:
warning = "Really reset PKGBUILDs to git state completely?\nThis will erase any untracked changes to your PKGBUILDs directory." warning = "Really reset PKGBUILDs to git state completely?\nThis will erase any untracked changes to your PKGBUILDs directory."
if not (force or click.confirm(warning)): if not (noop or force or click.confirm(warning)):
return return
result = git( result = git(
[ [
'clean', 'clean',
'-dffX', '-dffX' + ('n' if noop else ''),
] + REPOSITORIES, ] + REPOSITORIES,
dir=config.get_path('pkgbuilds'), dir=pkgbuilds,
) )
if result.returncode != 0: if result.returncode != 0:
logging.fatal('Failed to git clean') logging.fatal('Failed to git clean')
exit(1) exit(1)
else:
what = set(what)
dirs = []
for loc in ['pkg', 'src']:
if loc in what:
logging.info(f'gathering {loc} directories')
dirs += glob(os.path.join(pkgbuilds, '*', '*', loc))
dir_lines = '\n'.join(dirs)
verb = 'Would remove' if noop or force else 'Removing'
logging.info(verb + ' directories:\n' + dir_lines)
if not (noop or force):
if not click.confirm("Really remove all of these?", default=True):
return
for dir in dirs:
if not noop:
rmtree(dir)
@cmd_packages.command(name='check') @cmd_packages.command(name='check')