summaryrefslogtreecommitdiffstats
path: root/git/yaml_validation.py
diff options
context:
space:
mode:
authorMatt Woodson <mwoodson@gmail.com>2016-01-20 15:01:38 -0500
committerMatt Woodson <mwoodson@gmail.com>2016-01-20 15:01:38 -0500
commit9410cdff9342fae80a3149c530b819e473996cce (patch)
treecf26be9b05f3b97dbdafb7bae109b0707da0a0d4 /git/yaml_validation.py
parenta2b745039bce597d419b03fdce39b4c6c69139f6 (diff)
parentbdedb63403ea582c4aaa5f56caed302f51744de2 (diff)
downloadopenshift-9410cdff9342fae80a3149c530b819e473996cce.tar.gz
openshift-9410cdff9342fae80a3149c530b819e473996cce.tar.bz2
openshift-9410cdff9342fae80a3149c530b819e473996cce.tar.xz
openshift-9410cdff9342fae80a3149c530b819e473996cce.zip
Merge pull request #1247 from openshift/master
Merge master into prod
Diffstat (limited to 'git/yaml_validation.py')
-rwxr-xr-xgit/yaml_validation.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/git/yaml_validation.py b/git/yaml_validation.py
new file mode 100755
index 000000000..94b8b0435
--- /dev/null
+++ b/git/yaml_validation.py
@@ -0,0 +1,68 @@
+#!/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') and not file_mod.endswith('.yaml'):
+ continue
+
+ try:
+ yaml.load(open(file_mod))
+ results.append(True)
+
+ except yaml.scanner.ScannerError as yerr:
+ print yerr
+ results.append(False)
+ finally:
+ shutil.rmtree(tmpdir)
+
+ if not all(results):
+ sys.exit(1)
+
+if __name__ == "__main__":
+ main()
+