summaryrefslogtreecommitdiffstats
path: root/git/yaml_validate.py
diff options
context:
space:
mode:
authorKenny Woodson <kwoodson@redhat.com>2016-01-19 12:20:06 -0500
committerKenny Woodson <kwoodson@redhat.com>2016-01-19 12:20:06 -0500
commitc10c59d4919d6a61df96cdcaf3b407623125a46d (patch)
tree3252cb9a12fdd502745c43a03bf088217963d7de /git/yaml_validate.py
parent4a3d48da4ed98995f02a99ea59d38d5e84049b62 (diff)
parentdb356978d437ed70f7d4f8023d79c43e70c2e0bf (diff)
downloadopenshift-c10c59d4919d6a61df96cdcaf3b407623125a46d.tar.gz
openshift-c10c59d4919d6a61df96cdcaf3b407623125a46d.tar.bz2
openshift-c10c59d4919d6a61df96cdcaf3b407623125a46d.tar.xz
openshift-c10c59d4919d6a61df96cdcaf3b407623125a46d.zip
Merge pull request #1217 from kwoodson/remove_ruby
Removing ruby scripts and adding python ones.
Diffstat (limited to 'git/yaml_validate.py')
-rwxr-xr-xgit/yaml_validate.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/git/yaml_validate.py b/git/yaml_validate.py
new file mode 100755
index 000000000..7e0a08a4b
--- /dev/null
+++ b/git/yaml_validate.py
@@ -0,0 +1,63 @@
+#!/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 glob
+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)
+ proc.wait()
+ files = proc.stdout.read().strip().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.wait()
+
+ return [fmod for fmod in glob.glob('%s/**/*' % tempdir) if not os.path.isdir(fmod)]
+
+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()