format with gray formatter

This commit is contained in:
askiiart 2024-11-11 10:25:09 -06:00
parent 0fd73bfeed
commit 37f1833ffb
Signed by untrusted user who does not match committer: askiiart
GPG key ID: EA85979611654C30
2 changed files with 132 additions and 74 deletions

View file

@ -10,16 +10,31 @@ def cli():
@cli.command('run') @cli.command('run')
@click.option('--dry-run', is_flag=True, default=False, help="Do a dry run") @click.option('--dry-run', is_flag=True, default=False, help='Do a dry run')
@click.option('--only-delete', is_flag=True, default=False, help="Only delete files, don't run repo-remove and repo-add on their old and new versions (default: false)") @click.option(
@click.option('--delete-debug', is_flag=True, default=False, help="Delete debug symbol packages (default: false)") '--only-delete',
is_flag=True,
default=False,
help="Only delete files, don't run repo-remove and repo-add on their old and new versions (default: false)"
)
@click.option(
'--delete-debug',
is_flag=True,
default=False,
help='Delete debug symbol packages (default: false)'
)
@click.argument('repo_path') @click.argument('repo_path')
def run(repo_path, dry_run=False, only_delete=False, delete_debug=False): def run(repo_path, dry_run=False, only_delete=False, delete_debug=False):
get_old_packages_runner(repo_path) get_old_packages_runner(repo_path)
@cli.command('list-old-packages') @cli.command('list-old-packages')
@click.option('--list-debug', required=False, default=True, help="List debug symbol packages (default: true)") @click.option(
'--list-debug',
required=False,
default=True,
help='List debug symbol packages (default: true)'
)
@click.argument('repo_path') @click.argument('repo_path')
def get_old_packages_runner(repo_path, list_debug=True): def get_old_packages_runner(repo_path, list_debug=True):
''' '''
@ -36,8 +51,7 @@ def get_old_packages(repo_path, list_debug=True):
no_debug = [] no_debug = []
for item in pkg_map[pkg_name]: for item in pkg_map[pkg_name]:
# does nothing, but something needs to go here otherwise the linter complains about syntax; it needs the else for some reason # does nothing, but something needs to go here otherwise the linter complains about syntax; it needs the else for some reason
no_debug.append(item) if not item.startswith( no_debug.append(item) if not item.startswith(f'{pkg_name}-debug') else False
f'{pkg_name}-debug') else False
# skip in case there isn't an old version # skip in case there isn't an old version
if len(no_debug) == 1: if len(no_debug) == 1:
@ -46,15 +60,22 @@ def get_old_packages(repo_path, list_debug=True):
old_no_debug = natsorted(no_debug)[:1] old_no_debug = natsorted(no_debug)[:1]
old_packages.extend(old_no_debug) old_packages.extend(old_no_debug)
for item in old_no_debug: for item in old_no_debug:
if list_debug and item.replace(pkg_name, f'{pkg_name}-debug') in pkg_map[pkg_name]: if (
old_packages.append(item.replace( list_debug
pkg_name, f'{pkg_name}-debug')) and item.replace(pkg_name, f'{pkg_name}-debug') in pkg_map[pkg_name]
):
old_packages.append(item.replace(pkg_name, f'{pkg_name}-debug'))
return natsorted(old_packages) return natsorted(old_packages)
@cli.command('list-new-packages') @cli.command('list-new-packages')
@click.option('--list-debug', required=False, default=True, help="List debug symbol packages (default: true)") @click.option(
'--list-debug',
required=False,
default=True,
help='List debug symbol packages (default: true)'
)
@click.argument('repo_path') @click.argument('repo_path')
def get_new_packages_runner(repo_path, list_debug=True): def get_new_packages_runner(repo_path, list_debug=True):
for item in get_new_packages(repo_path, list_debug): for item in get_new_packages(repo_path, list_debug):
@ -73,23 +94,27 @@ def get_new_packages(repo_path, list_debug=True):
no_debug = [] no_debug = []
for item in pkg_map[pkg_name]: for item in pkg_map[pkg_name]:
# does nothing, but something needs to go here otherwise the linter complains about syntax; it needs the else for some reason # does nothing, but something needs to go here otherwise the linter complains about syntax; it needs the else for some reason
no_debug.append(item) if not item.startswith( no_debug.append(item) if not item.startswith(f'{pkg_name}-debug') else False
f'{pkg_name}-debug') else False
# add all in case there isn't an old version # add all in case there isn't an old version
if len(no_debug) == 1: if len(no_debug) == 1:
new_packages.append(no_debug[0]) new_packages.append(no_debug[0])
if list_debug and no_debug[0].replace(pkg_name, f'{pkg_name}-debug') in pkg_map[pkg_name]: if (
new_packages.append(no_debug[0].replace( list_debug
pkg_name, f'{pkg_name}-debug')) and no_debug[0].replace(pkg_name, f'{pkg_name}-debug')
in pkg_map[pkg_name]
):
new_packages.append(no_debug[0].replace(pkg_name, f'{pkg_name}-debug'))
continue continue
new_no_debug = natsorted(no_debug)[1:] new_no_debug = natsorted(no_debug)[1:]
new_packages.extend(new_no_debug) new_packages.extend(new_no_debug)
for item in new_no_debug: for item in new_no_debug:
if list_debug and item.replace(pkg_name, f'{pkg_name}-debug') in pkg_map[pkg_name]: if (
new_packages.append(item.replace( list_debug
pkg_name, f'{pkg_name}-debug')) and item.replace(pkg_name, f'{pkg_name}-debug') in pkg_map[pkg_name]
):
new_packages.append(item.replace(pkg_name, f'{pkg_name}-debug'))
return natsorted(new_packages) return natsorted(new_packages)
@ -115,15 +140,15 @@ class HelperFunctions:
package_names = [] package_names = []
for item in files: for item in files:
package_name = item[:item.rfind('-')] package_name = item[: item.rfind('-')]
package_name = package_name[:package_name.rfind('-')] package_name = package_name[: package_name.rfind('-')]
package_name = package_name[:package_name.rfind('-')] package_name = package_name[: package_name.rfind('-')]
package_names.append(package_name) package_names.append(package_name)
for item in files: for item in files:
package_name = item[:item.rfind('-')] package_name = item[: item.rfind('-')]
package_name = package_name[:package_name.rfind('-')] package_name = package_name[: package_name.rfind('-')]
package_name = package_name[:package_name.rfind('-')] package_name = package_name[: package_name.rfind('-')]
# can't just do endswith(), since normal packages can also end in `-debug`, like `dosbox-debug` in the AUR # can't just do endswith(), since normal packages can also end in `-debug`, like `dosbox-debug` in the AUR
# and if it still fails, that's a conflicting package failure, not an issue with the program # and if it still fails, that's a conflicting package failure, not an issue with the program
@ -143,22 +168,20 @@ class HelperFunctions:
def get_repo_name(repo_path): def get_repo_name(repo_path):
repo_name = glob.glob(f'{repo_path}/*.db')[0] repo_name = glob.glob(f'{repo_path}/*.db')[0]
repo_name = repo_name[repo_name.rfind('/')+1:] repo_name = repo_name[repo_name.rfind('/') + 1 :]
repo_name = repo_name[:repo_name.find('.')] repo_name = repo_name[: repo_name.find('.')]
return (repo_name) return repo_name
def get_package_list(repo_path): def get_package_list(repo_path):
''' '''
Lists all packages in repo_path, excluding the repo files (.db, .files) Lists all packages in repo_path, excluding the repo files (.db, .files)
''' '''
files = glob.glob(f'{repo_path}/*.tar.zst') files = glob.glob(f'{repo_path}/*.tar.zst')
files = [f[f.rfind('/')+1:] for f in files] files = [f[f.rfind('/') + 1 :] for f in files]
repo_name = HelperFunctions.get_repo_name(repo_path) repo_name = HelperFunctions.get_repo_name(repo_path)
files.remove( files.remove(f'{repo_name}.db.tar.zst')
f'{repo_name}.db.tar.zst') files.remove(f'{repo_name}.files.tar.zst')
files.remove(
f'{repo_name}.files.tar.zst')
return natsorted(files) return natsorted(files)
@ -168,43 +191,42 @@ if __name__ == '__main__':
class colors: class colors:
reset = '\033[0m' reset = '\x1b[0m'
bold = '\033[01m' bold = '\x1b[01m'
disable = '\033[02m' disable = '\x1b[02m'
underline = '\033[04m' underline = '\x1b[04m'
reverse = '\033[07m' reverse = '\x1b[07m'
strikethrough = '\033[09m' strikethrough = '\x1b[09m'
invisible = '\033[08m' invisible = '\x1b[08m'
class fg: class fg:
black = '\033[30m' black = '\x1b[30m'
red = '\033[31m' red = '\x1b[31m'
green = '\033[32m' green = '\x1b[32m'
orange = '\033[33m' orange = '\x1b[33m'
blue = '\033[34m' blue = '\x1b[34m'
purple = '\033[35m' purple = '\x1b[35m'
cyan = '\033[36m' cyan = '\x1b[36m'
lightgrey = '\033[37m' lightgrey = '\x1b[37m'
darkgrey = '\033[90m' darkgrey = '\x1b[90m'
lightred = '\033[91m' lightred = '\x1b[91m'
lightgreen = '\033[92m' lightgreen = '\x1b[92m'
yellow = '\033[93m' yellow = '\x1b[93m'
lightblue = '\033[94m' lightblue = '\x1b[94m'
pink = '\033[95m' pink = '\x1b[95m'
lightcyan = '\033[96m' lightcyan = '\x1b[96m'
rainbow = [lightred, orange, yellow, rainbow = [lightred, orange, yellow, lightgreen, lightcyan, blue, purple]
lightgreen, lightcyan, blue, purple]
class bg: class bg:
black = '\033[40m' black = '\x1b[40m'
red = '\033[41m' red = '\x1b[41m'
green = '\033[42m' green = '\x1b[42m'
orange = '\033[43m' orange = '\x1b[43m'
blue = '\033[44m' blue = '\x1b[44m'
purple = '\033[45m' purple = '\x1b[45m'
cyan = '\033[46m' cyan = '\x1b[46m'
lightgrey = '\033[47m' lightgrey = '\x1b[47m'
fg = colors.fg() fg = colors.fg()

56
test.py
View file

@ -16,8 +16,19 @@ def create_test_data():
except FileExistsError: except FileExistsError:
pass pass
files = ['test-r87.e176baf-1-x86_64.pkg.tar.zst', 'test-debug-r87.e176baf-1-x86_64.pkg.tar.zst', 'test-debug-r100.ab937ef-1-x86_64.pkg.tar.zst', 'test-r100.ab937ef-1-x86_64.pkg.tar.zst', 'repo.db', files = [
'repo.db.tar.zst', 'repo.files', 'repo.files.tar.zst', 'example-program-10.2.1-2-x86_64.pkg.tar.zst', 'example-program-8.4.3-5-x86_64.pkg.tar.zst', 'example-program-debug-10.2.1-2-x86_64.pkg.tar.zst'] 'test-r87.e176baf-1-x86_64.pkg.tar.zst',
'test-debug-r87.e176baf-1-x86_64.pkg.tar.zst',
'test-debug-r100.ab937ef-1-x86_64.pkg.tar.zst',
'test-r100.ab937ef-1-x86_64.pkg.tar.zst',
'repo.db',
'repo.db.tar.zst',
'repo.files',
'repo.files.tar.zst',
'example-program-10.2.1-2-x86_64.pkg.tar.zst',
'example-program-8.4.3-5-x86_64.pkg.tar.zst',
'example-program-debug-10.2.1-2-x86_64.pkg.tar.zst'
]
for f in files: for f in files:
touch(f'{repo_path}/{f}') touch(f'{repo_path}/{f}')
@ -26,22 +37,40 @@ class Tests(unittest.TestCase):
def test_list_old(self): def test_list_old(self):
create_test_data() create_test_data()
result = repo_cleanup.get_old_packages(repo_path) result = repo_cleanup.get_old_packages(repo_path)
expected = ['example-program-8.4.3-5-x86_64.pkg.tar.zst', expected = [
'test-debug-r87.e176baf-1-x86_64.pkg.tar.zst', 'test-r87.e176baf-1-x86_64.pkg.tar.zst'] 'example-program-8.4.3-5-x86_64.pkg.tar.zst',
'test-debug-r87.e176baf-1-x86_64.pkg.tar.zst',
'test-r87.e176baf-1-x86_64.pkg.tar.zst'
]
self.assertEqual(result, expected) self.assertEqual(result, expected)
def test_list_new(self): def test_list_new(self):
create_test_data() create_test_data()
result = repo_cleanup.get_new_packages(repo_path) result = repo_cleanup.get_new_packages(repo_path)
expected = ['example-program-10.2.1-2-x86_64.pkg.tar.zst', 'example-program-debug-10.2.1-2-x86_64.pkg.tar.zst', expected = [
'test-debug-r100.ab937ef-1-x86_64.pkg.tar.zst', 'test-r100.ab937ef-1-x86_64.pkg.tar.zst'] 'example-program-10.2.1-2-x86_64.pkg.tar.zst',
'example-program-debug-10.2.1-2-x86_64.pkg.tar.zst',
'test-debug-r100.ab937ef-1-x86_64.pkg.tar.zst',
'test-r100.ab937ef-1-x86_64.pkg.tar.zst'
]
self.assertEqual(result, expected) self.assertEqual(result, expected)
def test_package_map(self): def test_package_map(self):
create_test_data() create_test_data()
result = repo_cleanup.HelperFunctions.package_map(repo_path) result = repo_cleanup.HelperFunctions.package_map(repo_path)
expected = {'example-program': ['example-program-8.4.3-5-x86_64.pkg.tar.zst', 'example-program-10.2.1-2-x86_64.pkg.tar.zst', 'example-program-debug-10.2.1-2-x86_64.pkg.tar.zst'], 'test': [ expected = {
'test-debug-r87.e176baf-1-x86_64.pkg.tar.zst', 'test-debug-r100.ab937ef-1-x86_64.pkg.tar.zst', 'test-r87.e176baf-1-x86_64.pkg.tar.zst', 'test-r100.ab937ef-1-x86_64.pkg.tar.zst']} 'example-program': [
'example-program-8.4.3-5-x86_64.pkg.tar.zst',
'example-program-10.2.1-2-x86_64.pkg.tar.zst',
'example-program-debug-10.2.1-2-x86_64.pkg.tar.zst'
],
'test': [
'test-debug-r87.e176baf-1-x86_64.pkg.tar.zst',
'test-debug-r100.ab937ef-1-x86_64.pkg.tar.zst',
'test-r87.e176baf-1-x86_64.pkg.tar.zst',
'test-r100.ab937ef-1-x86_64.pkg.tar.zst'
]
}
self.assertEqual(result, expected) self.assertEqual(result, expected)
def test_repo_name(self): def test_repo_name(self):
@ -53,8 +82,15 @@ class Tests(unittest.TestCase):
def test_package_list(self): def test_package_list(self):
create_test_data() create_test_data()
result = repo_cleanup.HelperFunctions.get_package_list(repo_path) result = repo_cleanup.HelperFunctions.get_package_list(repo_path)
expected = ['test-r87.e176baf-1-x86_64.pkg.tar.zst', 'test-debug-r100.ab937ef-1-x86_64.pkg.tar.zst', 'test-r100.ab937ef-1-x86_64.pkg.tar.zst', 'example-program-10.2.1-2-x86_64.pkg.tar.zst', expected = [
'example-program-8.4.3-5-x86_64.pkg.tar.zst', 'test-debug-r87.e176baf-1-x86_64.pkg.tar.zst', 'example-program-debug-10.2.1-2-x86_64.pkg.tar.zst'] 'test-r87.e176baf-1-x86_64.pkg.tar.zst',
'test-debug-r100.ab937ef-1-x86_64.pkg.tar.zst',
'test-r100.ab937ef-1-x86_64.pkg.tar.zst',
'example-program-10.2.1-2-x86_64.pkg.tar.zst',
'example-program-8.4.3-5-x86_64.pkg.tar.zst',
'test-debug-r87.e176baf-1-x86_64.pkg.tar.zst',
'example-program-debug-10.2.1-2-x86_64.pkg.tar.zst'
]
create_test_data() create_test_data()