Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- import os
- import subprocess
- from collections import OrderedDict
- from termcolor import cprint
- import sh
- def printWarning(message):
- cprint(" %s " %message, 'white', 'on_red', ['bold'])
- def printSuccess(message):
- cprint(" %s " %message, 'white', 'on_green')
- def git(args):
- args = ['git'] + args
- git = subprocess.Popen(args, stdout = subprocess.PIPE)
- details = git.stdout.read()
- details = details.decode("utf-8").strip()
- return details
- def _git_config():
- raw_config = git(['config', '-l', '-z'])
- items = raw_config.split("\0")
- items = filter(lambda i: len(i) > 0, items)
- items = [item.partition("\n")[0:3:2] for item in items]
- return OrderedDict(items)
- GIT_CONFIG = _git_config()
- def get_config(key, default=None):
- return GIT_CONFIG.get(key, default)
- REPO_URL = get_config('remote.origin.url')
- if os.path.isfile('Jenkinsfile'):
- with open('Jenkinsfile', 'r') as file:
- filedata = file.read()
- isFound = filedata.find('git url:')
- if isFound:
- for line in filedata.split('\n'):
- if "git url:" in line:
- currUrl = reduce(lambda a, kv: a.replace(*kv), (('git url:', ''), (' ', ''), ("'", "")), line.strip())
- if currUrl != REPO_URL:
- printSuccess('Updated JenkinsFile, please push again')
- filedata = filedata.replace('$url', "'%s'" % (REPO_URL))
- with open('Jenkinsfile', 'w') as file:
- file.write(filedata)
- git = sh.git.bake(_cwd=os.getcwd())
- git.add('Jenkinsfile')
- git.commit(m='Jenkinsfile adjustment')
- sys.exit(1)
- else:
- printWarning('Blad w pliku Jenkinsfile')
- sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement