Skip to content

Instantly share code, notes, and snippets.

@blackandred
Last active December 30, 2016 17:32
Show Gist options
  • Save blackandred/719da8bf5bfeab253c35ce3f450473f1 to your computer and use it in GitHub Desktop.
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
#!/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