Created
April 13, 2017 00:29
-
-
Save ZimbiX/93db5c075cf3c1374322389ccfe73384 to your computer and use it in GitHub Desktop.
Fix Pacman "error: duplicated database entry '<package>'" for Arch Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os,re, datetime | |
d = '/var/lib/pacman/local' | |
packages = os.listdir(d) | |
packages.sort() | |
packages.remove('ALPM_DB_VERSION') | |
pkgname_search = re.compile('^(.*?)-[0-9]') | |
old_packages = [] | |
for pkg1 in packages: | |
if pkg1 in old_packages: | |
continue | |
#get package name | |
matches = pkgname_search.findall(pkg1) | |
print(pkg1) | |
if matches == []: | |
continue | |
pkgname = matches[0] | |
#look for other items with the same package name | |
for pkg2 in packages: | |
if pkg2 == pkg1: | |
continue | |
if pkg2 in old_packages: | |
continue | |
if pkgname == pkgname_search.findall(pkg2)[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
Original script from @jeffmikels: https://bbs.archlinux.org/viewtopic.php?pid=1034553#p1034553.
I added line 8 to account for an extra file in that directory on my system,
ALPM_DB_VERSION
.