Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3. import os
  4. import subprocess
  5. from collections import OrderedDict
  6. from termcolor import cprint
  7. import sh
  8.  
  9. def printWarning(message):
  10.     cprint(" %s " %message, 'white', 'on_red', ['bold'])
  11.  
  12. def printSuccess(message):
  13.     cprint(" %s " %message, 'white', 'on_green')
  14.  
  15. def git(args):
  16.     args = ['git'] + args
  17.     git = subprocess.Popen(args, stdout = subprocess.PIPE)
  18.     details = git.stdout.read()
  19.     details = details.decode("utf-8").strip()
  20.     return details
  21.  
  22. def _git_config():
  23.     raw_config = git(['config', '-l', '-z'])
  24.     items = raw_config.split("\0")
  25.     items = filter(lambda i: len(i) > 0, items)
  26.     items = [item.partition("\n")[0:3:2] for item in items]
  27.     return OrderedDict(items)
  28.  
  29. GIT_CONFIG = _git_config()
  30.  
  31. def get_config(key, default=None):
  32.     return GIT_CONFIG.get(key, default)
  33.  
  34. REPO_URL = get_config('remote.origin.url')
  35.  
  36. if os.path.isfile('Jenkinsfile'):
  37.     with open('Jenkinsfile', 'r') as file:
  38.         filedata = file.read()
  39.  
  40.     isFound = filedata.find('git url:')
  41.     if isFound:
  42.         for line in filedata.split('\n'):
  43.             if "git url:" in line:
  44.                 currUrl = reduce(lambda a, kv: a.replace(*kv), (('git url:', ''), (' ', ''), ("'", "")), line.strip())
  45.                 if currUrl != REPO_URL:
  46.                     printSuccess('Updated JenkinsFile, please push again')
  47.                     filedata = filedata.replace('$url', "'%s'" % (REPO_URL))
  48.                     with open('Jenkinsfile', 'w') as file:
  49.                         file.write(filedata)
  50.                     git = sh.git.bake(_cwd=os.getcwd())
  51.                     git.add('Jenkinsfile')
  52.                     git.commit(m='Jenkinsfile adjustment')
  53.                     sys.exit(1)
  54.     else:
  55.         printWarning('Blad w pliku Jenkinsfile')
  56.         sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement