Skip to content

Instantly share code, notes, and snippets.

@neurobin
Created August 20, 2017 15:03
Show Gist options
  • Save neurobin/1ebf702c6b9dfe0082226f4b0bdd22c4 to your computer and use it in GitHub Desktop.
Save neurobin/1ebf702c6b9dfe0082226f4b0bdd22c4 to your computer and use it in GitHub Desktop.
Remove duplicates from /var/lib/pacman/local to resolve errors such as "error: duplicated database entry 'man-pages'"
#!/usr/bin/python
import os,re, datetime
d = '/var/lib/pacman/local'
packages = os.listdir(d)
packages.sort()
pkgname_search = re.compile('^(.*?)-[0-9]')
old_packages = []
for pkg1 in packages:
if pkg1 in old_packages:
continue
#get package name
res = pkgname_search.findall(pkg1)
print(res)
if not res: continue
pkgname = res[0]
#look for other items with the same package name
for pkg2 in packages:
if pkg2 == pkg1:
continue
if pkg2 in old_packages:
continue
res = pkgname_search.findall(pkg2)
if res and pkgname == res[0]:
# We now have two duplicate packages, we want to delete the old one
old_package = pkg1
path1 = os.path.join(d,pkg1)
path2 = os.path.join(d,pkg2)
if os.stat(path1).st_mtime > os.stat(path2).st_mtime:
old_package = pkg2
old_packages.append(old_package)
#print ('duplicate found:\t')
#print (pkg1)
#print (pkg2)
#print ('old:', old_package)
oldpath = os.path.join(d,old_package)
target = os.path.join('/var/lib/pacman/OLD',old_package)
cmd = 'mv "%s" "%s"' % (oldpath, target)
#double-check that the oldpath still exists (it may have been removed in a previous pass)
if os.path.exists(oldpath):
print(cmd)
os.system(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment