From 01ba7f730b9af8a81f88a5b12fb8a7031e624829 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 19 Jan 2016 14:30:06 -0500 Subject: Fixing yaml validation in python. Inputs behave differently as does glob --- git/yaml_validation.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 git/yaml_validation.py (limited to 'git/yaml_validation.py') diff --git a/git/yaml_validation.py b/git/yaml_validation.py new file mode 100755 index 000000000..aa909b584 --- /dev/null +++ b/git/yaml_validation.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# +# python yaml validator for a git commit +# +''' +python yaml validator for a git commit +''' +import shutil +import sys +import os +import tempfile +import subprocess +import yaml + +def get_changes(oldrev, newrev, tempdir): + '''Get a list of git changes from oldrev to newrev''' + proc = subprocess.Popen(['/usr/bin/git', 'diff', '--name-only', oldrev, + newrev, '--diff-filter=ACM'], stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + files = stdout.split('\n') + + # No file changes + if not files: + return [] + + cmd = '/usr/bin/git archive %s %s | /bin/tar x -C %s' % (newrev, " ".join(files), tempdir) + proc = subprocess.Popen(cmd, shell=True) + _, _ = proc.communicate() + + rfiles = [] + for dirpath, _, fnames in os.walk(tempdir): + for fname in fnames: + rfiles.append(os.path.join(dirpath, fname)) + + return rfiles + +def main(): + ''' + Perform yaml validation + ''' + results = [] + try: + tmpdir = tempfile.mkdtemp(prefix='jenkins-git-') + old, new, _ = sys.argv[1:] + + for file_mod in get_changes(old, new, tmpdir): + + print "+++++++ Received: %s" % file_mod + + if not file_mod.endswith('.yml') or not file_mod.endswith('.yaml'): + continue + + try: + yaml.load(file_mod) + results.append(True) + + except yaml.scanner.ScannerError as yerr: + print yerr.message + results.append(False) + finally: + shutil.rmtree(tmpdir) + + if not all(results): + sys.exit(1) + +if __name__ == "__main__": + main() -- cgit v1.2.3 From 952858ca62acf92879662339353ab819f8e0e30d Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 19 Jan 2016 14:53:02 -0500 Subject: Fixed a logic bug and yaml load --- git/yaml_validation.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'git/yaml_validation.py') diff --git a/git/yaml_validation.py b/git/yaml_validation.py index aa909b584..94b8b0435 100755 --- a/git/yaml_validation.py +++ b/git/yaml_validation.py @@ -47,15 +47,15 @@ def main(): print "+++++++ Received: %s" % file_mod - if not file_mod.endswith('.yml') or not file_mod.endswith('.yaml'): + if not file_mod.endswith('.yml') and not file_mod.endswith('.yaml'): continue try: - yaml.load(file_mod) + yaml.load(open(file_mod)) results.append(True) except yaml.scanner.ScannerError as yerr: - print yerr.message + print yerr results.append(False) finally: shutil.rmtree(tmpdir) @@ -65,3 +65,4 @@ def main(): if __name__ == "__main__": main() + -- cgit v1.2.3