Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Merge versions in two puppet files.
import sys
import re
from pkg_resources import parse_version
updates =
records = updates.split(';')
packages = {}
for record in records:
fields = [r.strip() for r in record.split('\n') if r.strip()]
if not fields:
p = re.match(r"'(.*)':", fields[0]).group(1) # package
e = re.match(r"ensure\s+=>\s+'(.*)'", fields[1]).group(1) # ensure
v = parse_version(e)
if packages.has_key(p):
# replace the stored version number if it's smaller, otherwise
# no change.
if parse_version(packages[p]) < v:
print '%s: %s -> %s' % (p, packages[p], e)
packages[p] = e
elif parse_version(packages[p]) > v:
print '%s: %s -> %s' % (p, e, packages[p])
packages[p] = e
for package, ensure in sorted(packages.items()):
print "'%s':" % package
print " ensure => '%s'," % ensure
print " provider => 'aptitude';"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.