Last active
December 30, 2016 17:32
-
-
Save blackandred/719da8bf5bfeab253c35ce3f450473f1 to your computer and use it in GitHub Desktop.
Arch Linux: Clean up pacman cache, leave only 2 versions of every package, the rest of olders remove
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/python | |
import os | |
import collections | |
def getPackageName(fileName): | |
exp = fileName.split('-') | |
found = False | |
name = "" | |
sep = "" | |
for chunk in exp: | |
try: | |
float(chunk.replace('.', '')) | |
break | |
except Exception: | |
if not name: | |
sep = "" | |
else: | |
sep = "-" | |
name = name + sep + chunk | |
return name | |
packagesCount = {} | |
maxVersions = 2 | |
for file in os.listdir('/var/cache/pacman/pkg'): | |
if not getPackageName(file) in packagesCount: | |
packagesCount[getPackageName(file)] = {} | |
packagesCount[getPackageName(file)][os.path.getmtime('/var/cache/pacman/pkg/'+file)] = '/var/cache/pacman/pkg/'+file | |
for package in packagesCount: | |
sortedDict = reversed(sorted(packagesCount[package].items())) | |
i = 0 | |
for item in sortedDict: | |
i = i + 1 | |
if i > maxVersions: | |
print("rm "+item[1]) | |
os.system("rm "+item[1]) | |
#print(packagesCount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment