From db356978d437ed70f7d4f8023d79c43e70c2e0bf Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Mon, 18 Jan 2016 15:20:23 -0500 Subject: Removing removing scripts and moving to python. --- git/parent.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++ git/yaml_validate.py | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100755 git/parent.py create mode 100755 git/yaml_validate.py diff --git a/git/parent.py b/git/parent.py new file mode 100755 index 000000000..154a02350 --- /dev/null +++ b/git/parent.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python +''' + Script to determine if this commit has also + been merged through the stage branch +''' +# +# Usage: +# parent_check.py +# +# +import sys +import subprocess + +def run_cli_cmd(cmd, in_stdout=None, in_stderr=None): + '''Run a command and return its output''' + if not in_stderr: + proc = subprocess.Popen(cmd, bufsize=-1, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=False) + else: + proc = subprocess.check_output(cmd, bufsize=-1, stdout=in_stdout, stderr=in_stderr, shell=False) + stdout, stderr = proc.communicate() + if proc.returncode != 0: + return {"rc": proc.returncode, "error": stderr} + else: + return {"rc": proc.returncode, "result": stdout} + +def main(): + '''Check to ensure that the commit that is currently + being submitted is also in the stage branch. + + if it is, succeed + else, fail + ''' + branch = 'prod' + + if sys.argv[1] != branch: + sys.exit(0) + + # git co stg + results = run_cli_cmd(['/usr/bin/git', 'checkout', 'stg']) + + # git pull latest + results = run_cli_cmd(['/usr/bin/git', 'pull']) + + # setup on the branch in git + results = run_cli_cmd(['/usr/bin/git', 'checkout', 'prod']) + + results = run_cli_cmd(['/usr/bin/git', 'pull']) + # merge the passed in commit into my current + + commit_id = sys.argv[2] + results = run_cli_cmd(['/usr/bin/git', 'merge', commit_id]) + + # get the differences from stg and + results = run_cli_cmd(['/usr/bin/git', 'rev-list', '--left-right', 'stg...prod']) + + # exit here with error code if the result coming back is an error + if results['rc'] != 0: + print results['error'] + sys.exit(results['rc']) + + count = 0 + # Each 'result' is a commit + # Walk through each commit and see if it is in stg + for commit in results['result'].split('\n'): + + # continue if it is already in stg + if not commit or commit.startswith('<'): + continue + + # remove the first char '>' + commit = commit[1:] + + # check if any remote branches contain $commit + results = run_cli_cmd(['/usr/bin/git', 'branch', '-q', '-r', '--contains', commit], in_stderr=None) + + # if this comes back empty, nothing contains it, we can skip it as + # we have probably created the merge commit here locally + if results['rc'] == 0 and len(results['result']) == 0: + continue + + # The results generally contain origin/pr/246/merge and origin/pr/246/head + # this is the pull request which would contain the commit in question. + # + # If the results do not contain origin/stg then stage does not contain + # the commit in question. Therefore we need to alert! + if 'origin/stg' not in results['result']: + print "\nFAILED: (These commits are not in stage.)\n" + print "\t%s" % commit + count += 1 + + # Exit with count of commits in #{branch} but not stg + sys.exit(count) + +if __name__ == '__main__': + main() + 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() -- cgit v1.2.3