Skip to content

Instantly share code, notes, and snippets.

@nekoya
Created January 6, 2015 06:50
Show Gist options
  • Save nekoya/5737ae85dd20d6431cee to your computer and use it in GitHub Desktop.
Save nekoya/5737ae85dd20d6431cee to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import argparse
import re
import sys
from collections import namedtuple
import git
match_serial = re.compile(r'^\+\s+(\d{10})\s*; serial$').match
Zone = namedtuple('Zone', ['filename', 'serial'])
def get_diff(repo, commit):
return repo.commit('master').diff(commit, paths=['bind/etc/zone'], create_patch=True)
def find_updated_serial(diff):
res = [match_serial(d).group(1) for d in parse_contents(diff) if match_serial(d)]
serial = res[0] if len(res) == 1 else None
return Zone(diff.b_blob.name, serial)
def parse_contents(diff):
return diff.diff.splitlines()[2:]
def report(zone):
print '- %s : %s' % (zone.filename, zone.serial)
return zone.serial
if __name__ == '__main__':
try:
parser = argparse.ArgumentParser(description='Bind zone file serial checker')
parser.add_argument('repo', help='target repository')
parser.add_argument('commit', help='target commit')
args = parser.parse_args()
repo = git.Repo(args.repo)
res = [report(find_updated_serial(d)) for d in get_diff(repo, args.commit)]
if not all(res):
raise Exception('Error: serials are not updated correctly')
except Exception as e:
print str(e)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment