Skip to content

Instantly share code, notes, and snippets.

@jayzeng
Created May 25, 2013 03:14
Show Gist options
  • Save jayzeng/5647763 to your computer and use it in GitHub Desktop.
Save jayzeng/5647763 to your computer and use it in GitHub Desktop.
syntax check for modified puppet template and manifests
#!/usr/bin/python
import os.path
import subprocess
import sys
def get_modified_files():
"""
return a list of modified files tracked by git
"""
git_status = subprocess.Popen(['git', 'status', '-s'], stdout=subprocess.PIPE)
files, err = git_status.communicate()
return [f.strip('? MA') for f in files.split('\n')]
def syntax_check(files):
"""
Perform syntax check towards given files
arguments:
files -- a list of modified files
"""
err_msg = ''
for file in files:
# manifest files
if os.path.splitext(file)[1] == '.pp':
check_manifest = subprocess.Popen(['puppet','parser','validate',file])
check_manifest.communicate()
if check_manifest.returncode != 0:
err_msg += str(check_manifest.stderr)
# template files
if os.path.splitext(file)[1] == '.erb':
check_template_cmd = "erb -P -x -T '-' %s | ruby -c" % file
check_template = subprocess.Popen(check_template_cmd, shell=True, stdout=subprocess.PIPE)
check_template.communicate()
if check_template.returncode != 0:
err_msg += str(check_template.stderr)
return err_msg
if __name__ == '__main__':
modified_files = get_modified_files()
output = syntax_check(modified_files)
if output:
sys.exit('modified files have syntax error')
else:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment