mirror of
https://github.com/lilydjwg/archrepo2.git
synced 2025-03-10 12:02:43 +00:00
add scripts/remove_old_packages
This commit is contained in:
parent
88c540263d
commit
ac5a7cd1cd
1 changed files with 57 additions and 0 deletions
57
scripts/remove_old_packages
Executable file
57
scripts/remove_old_packages
Executable file
|
@ -0,0 +1,57 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
import archpkg
|
||||||
|
from archpkg import parse_version
|
||||||
|
|
||||||
|
class PkgNameInfo(archpkg.PkgNameInfo):
|
||||||
|
def __lt__(self, other):
|
||||||
|
# ignore arch mismatch
|
||||||
|
if self.name != other.name:
|
||||||
|
return NotImplemented
|
||||||
|
if self.version != other.version:
|
||||||
|
return parse_version(self.version) < parse_version(other.version)
|
||||||
|
return int(self.release) < int(other.release)
|
||||||
|
|
||||||
|
def remove_pkg(path):
|
||||||
|
try:
|
||||||
|
os.unlink(path)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
sig = path + '.sig'
|
||||||
|
try:
|
||||||
|
os.unlink(sig)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def main(args):
|
||||||
|
pkgs = defaultdict(list)
|
||||||
|
for f in args.packages:
|
||||||
|
pkg = PkgNameInfo.parseFilename(f)
|
||||||
|
pkgs[pkg.name].append((pkg, f))
|
||||||
|
for _, v in pkgs.items():
|
||||||
|
try:
|
||||||
|
v.sort()
|
||||||
|
except TypeError:
|
||||||
|
print('Bad things happen: %s' % v)
|
||||||
|
raise
|
||||||
|
for pkg, f in v[:-args.keep]:
|
||||||
|
if args.dry_run:
|
||||||
|
print('would remove %s.' % f)
|
||||||
|
else:
|
||||||
|
print('removing %s.' % f)
|
||||||
|
remove_pkg(f)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
parser = argparse.ArgumentParser(description='remove old Arch Linux packages')
|
||||||
|
parser.add_argument('-n', '--dry-run', action='store_true',
|
||||||
|
help='dry run')
|
||||||
|
parser.add_argument('-k', '--keep', metavar='N', type=int, default=2,
|
||||||
|
help='how many versions to keep. Default is 2')
|
||||||
|
parser.add_argument('packages', metavar='PACKAGE', nargs='+',
|
||||||
|
help='package files to check')
|
||||||
|
args = parser.parse_args()
|
||||||
|
main(args)
|
Loading…
Add table
Reference in a new issue