dotfiles/envsetup.py

172 lines
4.7 KiB
Python
Raw Normal View History

2012-01-05 17:39:46 +00:00
#!/usr/bin/env python
#Personal environment setup script
2012-01-05 20:02:05 +00:00
#Dependencies - Supported distros, sudo, python2
2012-01-05 17:39:46 +00:00
import os
from subprocess import call
import platform
import urllib2
import tarfile
2012-01-05 17:39:46 +00:00
#global vars
vimrcurl = "https://raw.github.com/staticsafe/dotfiles/master/.vimrc"
zshrcurl = "https://raw.github.com/staticsafe/dotfiles/master/.zshrc"
tmuxurl = "https://raw.github.com/staticsafe/dotfiles/master/.tmux.conf"
2012-01-08 20:43:24 +00:00
vimdirurl = "http://dl.dropbox.com/u/2888062/vimdir.tar.bz2"
2012-02-06 00:09:28 +00:00
conkyrc = "https://raw.github.com/staticsafe/dotfiles/master/.conkyrc"
2012-01-21 18:19:40 +00:00
envupdate = "https://raw.github.com/staticsafe/dotfiles/master/envupdate.sh"
2012-01-05 17:39:46 +00:00
2012-01-05 19:19:47 +00:00
def sudocheck():
#sudocheck
sudopath = "/usr/bin/sudo"
2012-01-08 20:03:52 +00:00
if os.path.isfile(sudopath) == False:
2012-01-05 19:19:47 +00:00
print 'This script needs sudo to run!'
2012-01-05 17:39:46 +00:00
raise SystemExit
2012-01-05 18:19:14 +00:00
else:
2012-01-05 19:19:47 +00:00
print 'Sudo check: PASSED!'
2012-01-05 18:19:14 +00:00
2012-02-13 19:07:34 +00:00
def macportscheck():
# This function checks if MacPorts exists on a OSX machine or not
portspath = "/opt/local/bin/port"
2012-02-23 16:19:41 +00:00
if os.path.isfile(portspath) == False:
2012-02-13 19:07:34 +00:00
print "We need MacPorts to run this script for you! Get it from here - http://www.macports.org/"
raise SystemExit
else:
print "MacPorts check: PASSED!"
2012-01-05 18:19:14 +00:00
def urldownload(confurl = ""):
2012-01-05 20:02:05 +00:00
#Thanks PabloG from StackOverflow for this little snippet - http://stackoverflow.com/a/22776
2012-01-05 18:19:14 +00:00
url = confurl
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
file_size_dl = 0
block_sz = 8192
2012-01-05 18:28:53 +00:00
2012-01-05 18:19:14 +00:00
while True:
2012-01-05 18:32:08 +00:00
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
2012-01-05 18:33:57 +00:00
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
2012-01-05 18:32:08 +00:00
status = status + chr(8)*(len(status)+1)
print status,
2012-01-05 18:19:14 +00:00
f.close()
2012-01-06 05:50:48 +00:00
def checksandactions():
#vars for various conf files and dirs
vimrcdir = os.path.join(os.environ['HOME'], ".vimrc")
zshrcdir = os.path.join(os.environ['HOME'], ".zshrc")
tmuxconfdir = os.path.join(os.environ['HOME'], ".tmux.conf")
vimdir = os.path.join(os.environ['HOME'], ".vim")
2012-02-06 00:09:28 +00:00
conkydir = os.path.join(os.environ['HOME'], ".conkyrc")
2012-01-06 05:50:48 +00:00
usershell = os.getenv('SHELL')
2012-01-05 20:02:05 +00:00
2012-01-06 05:50:48 +00:00
#checks to prevent clobbering
if os.path.isfile(vimrcdir) == True:
print ".vimrc already exists, skipping download!"
else:
urldownload(confurl = vimrcurl)
if os.path.isfile(zshrcdir) == True:
print ".zshrc already exists, skipping download!"
else:
urldownload(confurl = zshrcurl)
if os.path.isfile(tmuxconfdir) == True:
print ".tmux.conf already exists, skipping download!"
else:
urldownload(confurl = tmuxurl)
if os.path.isdir(vimdir) == True:
print ".vim dir already exists, skipping download!"
else:
urldownload(confurl = vimdirurl)
tar = tarfile.open("vimdir.tar.bz2")
tar.extractall()
tar.close()
2012-01-06 05:50:48 +00:00
2012-02-06 00:09:28 +00:00
if os.path.isfile(conkydir) == True:
print ".conkyrc already exists, skipping download!"
else:
2012-02-23 16:29:45 +00:00
urldownload(confurl = conkyrc)
2012-02-06 00:09:28 +00:00
2012-01-08 20:00:38 +00:00
if usershell == "/bin/zsh":
2012-01-06 05:50:48 +00:00
print "Your default shell is already zsh! Skipping."
else:
print "Setting default shell for this user to zsh! Log out and log back in to see changes."
setzsh = call("chsh -s $(which zsh)", shell = True)
zshhistory = call("touch ~/.zhistory", shell = True)
2012-02-06 00:09:28 +00:00
2012-01-05 18:19:14 +00:00
def envArch():
2012-01-05 20:02:05 +00:00
sudocheck()
2012-01-06 05:50:48 +00:00
#Install relevant packages
2012-01-05 18:19:14 +00:00
installpackages = call("sudo pacman --noconfirm -S vim zsh tmux git subversion", shell=True)
#Get relevant dotfiles
2012-01-06 05:50:48 +00:00
checksandactions()
2012-01-05 18:19:14 +00:00
def envFedora():
2012-01-05 20:02:05 +00:00
sudocheck()
2012-01-05 18:19:14 +00:00
#Install relevant packages
installpackages = call ("sudo yum install -y vim zsh tmux git subversion", shell=True)
#Get relevant dotfiles
2012-01-06 05:50:48 +00:00
checksandactions()
2012-01-05 18:19:14 +00:00
def envDebian():
2012-01-05 20:02:05 +00:00
sudocheck()
2012-01-05 18:19:14 +00:00
#Install relevant packages
installpackages = call ("sudo apt-get install --assume-yes vim zsh tmux git subversion", shell=True)
#Get relevant dotfiles
2012-01-06 05:50:48 +00:00
checksandactions()
2012-01-05 17:39:46 +00:00
2012-02-13 19:07:34 +00:00
def envOSX():
sudocheck()
macportscheck()
#Install relevant packages
installpackages = call ("sudo /opt/local/bin/port install vim zsh tmux git subversion", shell=True)
#Get relevant dotfiles
checksandactions()
2012-01-05 17:39:46 +00:00
def main():
#homedircheck
homedir = os.environ['HOME']
currentdir = os.getcwd()
if currentdir != homedir:
2012-01-05 20:02:05 +00:00
print "Changing cwd to homedir!"
2012-01-05 17:39:46 +00:00
os.chdir(homedir)
else:
print "Home directory check : PASSED!"
2012-01-05 18:19:14 +00:00
2012-01-05 17:39:46 +00:00
#distrocheck
userdistro = platform.linux_distribution()
2012-02-13 19:07:34 +00:00
osxcheck = platform.uname() # linux_distribution is useless on OSX so we use this
2012-01-05 17:39:46 +00:00
if userdistro[0] == "Fedora":
2012-01-05 18:35:12 +00:00
envFedora()
2012-01-05 17:39:46 +00:00
elif userdistro[0] == "debian":
2012-01-05 18:19:14 +00:00
envDebian()
2012-01-05 17:39:46 +00:00
elif userdistro[0] == "Arch" or os.path.isfile("/etc/arch-release") == True:
2012-01-05 18:19:14 +00:00
envArch()
2012-01-05 17:39:46 +00:00
elif userdistro[0] == "Ubuntu":
2012-01-05 18:19:14 +00:00
envDebian()
2012-02-13 19:07:34 +00:00
elif osxcheck[0] == "Darwin":
envOSX()
2012-01-05 17:39:46 +00:00
else:
print "This script is not supported for your distro, exiting."
raise SystemExit
if __name__ == "__main__":
2012-01-21 18:19:40 +00:00
main()