Skip to content

Instantly share code, notes, and snippets.

@Luzifer
Created June 4, 2011 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luzifer/1008413 to your computer and use it in GitHub Desktop.
Save Luzifer/1008413 to your computer and use it in GitHub Desktop.
This script cleans a directory containing debian packages up leaving only the three newest versions. Package filenames must follow "<package>_<version>-<build>.deb" format and package name must not contain underscores.
#!/usr/bin/env python
import sys, os
def extract_pkg(package_file):
result = {}
package_file = package_file.replace('.deb', '')
(result['package'], tmp) = package_file.split('_')
(tmp, result['build']) = tmp.split('-')
tmp = tmp.split('.')
result['version'] = 0.0
for i in range(0, len(tmp)):
result['version'] = result['version'] + float(tmp[i]) / 1000**i
return result
def main(argv=sys.argv):
packages = os.listdir(argv[1])
basenames = []
[ basenames.append(name.split('_')[0]) for name in packages if '.deb' in name and not basenames.count(name.split('_')[0]) ]
for package in basenames:
package_files = [ name for name in packages if package in name ]
package_files.sort(lambda x, y: cmp(extract_pkg(y)['version'], extract_pkg(x)['version']))
old_files = package_files[3:]
for filename in old_files:
os.unlink(os.path.realpath(os.path.join(argv[1], filename)))
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment