Skip to content

Instantly share code, notes, and snippets.

@lsjostro
Created June 27, 2014 12:53
Show Gist options
  • Save lsjostro/0e4ace7b64998c5abcf8 to your computer and use it in GitHub Desktop.
Save lsjostro/0e4ace7b64998c5abcf8 to your computer and use it in GitHub Desktop.
def main():
options = parse_options()
db = Connection('localhost', 27017).pulp_database
rpms = db.units_rpm
repo_content_units = db.repo_content_units.find({'repo_id': options.repo_id})
units = {}
for unit in repo_content_units:
data = rpms.find_one({"_id": unit['unit_id']})
if data:
units.setdefault(data['name'], [])
units[data['name']].append(data)
for pkg_name, versions in units.items():
print "Package %s (Versions: %d)" % (pkg_name, len(versions))
sorted_versions = sorted(versions, key=lambda k: LooseVersion('-'.join([k['version'],k['release']])))
while len(sorted_versions) > options.num_to_keep:
d = sorted_versions.pop(0)
print "-- Going to delete version: %s-%s" % (d['version'], d['release'])
if options.doit:
db.repo_content_units.remove({'unit_id': d['_id']})
print "deleteing...."
print "Packages to keep %s (%d)" % (pkg_name, len(sorted_versions))
for v in sorted_versions:
print "++ Keeping version: %s-%s" % (v['version'], v['release'])
print ""
if options.doit:
print "Please run: pulp-admin orphan remove --type rpm"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment