Skip to content

Instantly share code, notes, and snippets.

@adamrpah
Created August 14, 2014 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamrpah/d8c6af9260bf8a96ce35 to your computer and use it in GitHub Desktop.
Save adamrpah/d8c6af9260bf8a96ce35 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import json
import sys
def check_bashrc():
'''
Checks the bashrc for the appropriate pyenv lines
'''
bashrc_path = os.path.join(os.environ["HOME"], '.bashrc')
#Pyenv codelines
pyenv_root = 'export PYENV_ROOT="$HOME/.pyenv"'
pyenv_path = 'export PATH="$PYENV_ROOT/bin:$PATH"'
pyenv_eval = 'eval "$(pyenv init -)"'
#Do the check
modify = False
bashrc = [l.strip() for l in open(bashrc_path).readlines()]
if pyenv_root not in bashrc:
bashrc.append(pyenv_root)
modify = True
if pyenv_path not in bashrc:
bashrc.append(pyenv_path)
modify = True
if pyenv_eval not in bashrc:
bashrc.append(pyenv_eval)
modify=True
#Rewrite if we failed teh checks
if modify:
with open(bashrc_path, 'w') as wfile:
for bline in bashrc:
print >> wfile, bline
wfile.close()
return modify
def generate_repo_checkout_line(repo):
'''
Generates a repository checkout line
'''
cmdLine = ''
#Start it up with the command
if repo['type']=='hg' or repo['type']=='git':
cmdLine += repo['type'] + ' clone '
else:
print >> sys.stderr, "I do not currently support repositories of type %s" % repo['type']
#Now add in the checkout type
if repo['properties'] =='private':
cmdLine += 'ssh://'
else:
cmdLine += 'https://'
cmdLine += repo['url'] + repo['repo'] + ' ' + repo['dest']
return cmdLine
#Check out the private repositories
#Try with ssh first, then switch to https
repositories = [
{
'type': 'git',
'properties': 'public',
'url': 'github.com/',
'repo': 'yyuu/pyenv.git',
'dest': os.path.join(os.environ['HOME'], '.pyenv')
},
{
'type': 'git',
'properties': 'public',
'url': 'github.com/',
'repo': 'yyuu/pyenv-virtualenv.git',
'dest': os.path.join(os.environ['HOME'], '.pyenv/plugins/pyenv-virtualenv')
},
{
'type': 'git',
'properties': 'public',
'url': 'github.com/',
'repo': 'yyuu/pyenv-doctor.git',
'dest': os.path.join(os.environ['HOME'], '.pyenv/plugins/pyenv-doctor')
}
]
#Do the checkouts
for repo in repositories:
if not os.path.exists(repo['dest']):
cmdLine = generate_repo_checkout_line(repo)
print cmdLine
os.system(cmdLine)
else:
os.system('cd %s && git pull' % repo['dest'])
#Check the bashrc
bashrc_status = check_bashrc()
if bashrc_status:
print "The bashrc file was not configured for pyenv prior to this script installation"
#Execute for pyenv
os.system('exec "$SHELL"')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment