Skip to content

Instantly share code, notes, and snippets.

@ArcQ
Created October 23, 2016 21:37
Show Gist options
  • Save ArcQ/db9c7952a4265975112357569ad0ed4a to your computer and use it in GitHub Desktop.
Save ArcQ/db9c7952a4265975112357569ad0ed4a to your computer and use it in GitHub Desktop.
WIP for auto updating vimrc for use between different machines
#Usage: python autoVimrc.py (pull || push)
import subprocess
import sys
import os.path
import getpass
def main():
funcStr = sys.argv[1]
globals()[funcStr]()
def pull():
if not checker.isVimrcExist():
return
#copy vimrc to current, override prompt to write over current
bash('yes | cp ~/.vimrc ./.vimrc')
git('pull origin')
def push():
#pull first to avoid conflicts
pull()
if not checker.isDiffOrigin():
return
git('add -A')
git('commit -m "auto-commit:update vimrc"')
remoteUrl = getFormattedOrigin()
git('push %s master' % (remoteUrl))
def bash(command):
return systemCmd.run(command,'bash')
def git(command):
return systemCmd.run(command,'git')
def getFormattedOrigin():
remoteUrl = bash('git config --get remote.origin.url')
print(remoteUrl)
remoteUrl = remoteUrl.decode('utf-8').replace('https://', '').replace('\n','')
gitUsr = input("Enter GIT Username: ")
gitPw = getpass.getpass("Enter GIT Password: ")
# git format: https://username:password@myrepository.biz/file.git
remoteUrl = "https://%s:%s@%s" % (gitUsr,gitPw,remoteUrl)
return remoteUrl
# runs a variety of commands in commandline
class systemCmd():
@staticmethod
def run(command,type):
runFunc = systemCmd.gitFunc if type == 'git' else systemCmd.bashFunc
if isinstance(command, list):
for commandStr in command:
output = runFunc(commandStr)
print(output)
else:
output = runFunc(command)
print(output)
return output
@staticmethod
def gitFunc(commandStr):
commandArr = commandStr.split(' ')
if(commandArr[0] == 'commit'):
commandArr = ['git','commit','-m','%s' % commandStr.split('"')[1]]
else:
commandArr = ['git'] + commandArr
return subprocess.check_output(commandArr)
@staticmethod
def bashFunc(commandStr):
commandArr = ['bash','-c',commandStr]
return subprocess.check_output(commandArr)
# checks a variety of condtions
class checker():
@staticmethod
def isVimrcExist():
errStr = "no vimrc file"
homePath = os.path.expanduser('~')
return checker.__checkAndLog(os.path.isfile('%s/.vimrc' % homePath),errStr)
@staticmethod
def isDiffOrigin():
errStr = 'no changes to vimrc'
checkOutput = True if (git('diff')) else False
return checker.__checkAndLog(checkOutput,errStr)
@staticmethod
def __checkAndLog(checkOutput,errStr):
if(not checkOutput):
print(errStr)
return checkOutput
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment