Skip to content

Instantly share code, notes, and snippets.

@funkaoshi
Created February 13, 2013 01:37
Show Gist options
  • Save funkaoshi/4827204 to your computer and use it in GitHub Desktop.
Save funkaoshi/4827204 to your computer and use it in GitHub Desktop.
Merge versions in two puppet files.
import sys
import re
from pkg_resources import parse_version
updates = sys.stdin.read()
records = updates.split(';')
packages = {}
for record in records:
fields = [r.strip() for r in record.split('\n') if r.strip()]
if not fields:
continue
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])
else:
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