#!/usr/bin/python import tempfile from subprocess import Popen, PIPE problems = {} def get_filelist(): """Get the list of files which are to be committed""" cmd = ['git', 'diff-index', '--cached', '--name-only', 'HEAD'] p = Popen(cmd, stdout=PIPE) return p.communicate()[0].strip().split('\n') def get_contents(path): """Get the contents of a file in the index""" cmd = ['git', 'show', ':%s' % path] p = Popen(cmd, stdout=PIPE) return p.communicate()[0] def puppet_parse(path): """Check whether a file parses successfully""" cmd = ['puppet', '--color=false', '--parseonly', path] p = Popen(cmd, stdout=PIPE) return p.communicate()[0] for f in get_filelist(): if not f.endswith('.pp'): continue contents = get_contents(f) # Write contents to a temporary file # FIXME Patch puppet to parse via stdin tmp = tempfile.NamedTemporaryFile(dir='.') tmp.write(contents) tmp.flush() output = puppet_parse(tmp.name) if output: output = output.replace('%s:' % tmp.name, 'line ') problems[f] = output.rstrip() if problems: print '*' print '* You have problems with the following puppet file(s):' print '*' for f in sorted(problems): error = problems[f] print '* %s' % f print '* %s' % error print '*' raise SystemExit(1)