Created
May 6, 2013 18:45
-
-
Save amyreese/5527172 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
from os import path | |
import os, sys, shutil | |
settings = { | |
"source": "~/profile", | |
"target": "~", | |
} | |
def item( name, link=None ): | |
"""Symlink a profile item found in 'source' with the given 'name'. | |
By default, 'source' is the current directory, and 'target' is | |
'name' prepended with a period.""" | |
if None == name: | |
print "Null name!" | |
sys.exit(2) | |
if None == link: | |
link = "." + name | |
source = path.expanduser(settings["source"]) | |
target = path.expanduser(settings["target"]) | |
sourceItem = path.join( source, name ) | |
targetItem = path.join( target, link ) | |
print "Processing '" + sourceItem + "' -> '" + targetItem + "' ..." | |
if not path.lexists( sourceItem ): | |
print "Source '" + sourceItem + "' not found." | |
return | |
targetDir = path.dirname( targetItem ) | |
if not path.lexists( targetDir ): | |
print "Target parent '" + targetDir + "' does not exist; creating..." | |
os.makedirs( targetDir ) | |
if path.lexists( targetItem ): | |
print "Target '" + targetItem + "' already exists; deleting..." | |
if path.islink( targetItem ) or path.isfile( targetItem ): | |
os.unlink( targetItem ) | |
elif path.isdir( targetItem ): | |
try: | |
os.rmdir( targetItem ) | |
except OSError: | |
shutil.rmtree( targetItem ) | |
os.symlink( sourceItem, targetItem ) | |
#end | |
def item_tuple( tuple ): | |
length = len( tuple ) | |
if length == 2: | |
item( tuple[0], tuple[1] ) | |
elif length == 1: | |
item( tuple[0] ) | |
else: | |
print "Bad tuple: " + tuple | |
#end | |
def profile_items( source="~/profile" ): | |
settings["source"] = source | |
items = [ | |
# Regular dotfiles | |
( "alias", ), | |
( "bashrc", ), | |
( "gitconfig", ), | |
( "gitignore", ), | |
( "profile", ), | |
( "screenrc", ), | |
( "screenrc.irssi", ), | |
( "sieverc", ), | |
( "synergy.conf", ), | |
( "tmux.conf", ), | |
( "vimrc", ), | |
( "xmodmaprc", ), | |
( "zshrc", ), | |
# Regular dotdirs | |
( "fonts", ), | |
( "gnupg", ), | |
( "irssi", ), | |
( "oh-my-zsh", ), | |
( "purple", ), | |
( "vim", ), | |
# Other | |
( "bin", "bin" ), | |
( "xchat", ".xchat2" ), | |
( "ssh/authorized_keys", ".ssh/authorized_keys" ), | |
( "ssh/config", ".ssh/config" ), | |
( "htoprc", ".config/htop/htoprc" ), | |
( "sublime", ".config/sublime-text-2" ), | |
] | |
map( item_tuple, items ); | |
#end | |
if __name__ == "__main__": | |
profilepath = path.abspath( path.dirname( path.dirname( sys.argv[0] ) ) ) | |
if profilepath != "": | |
profile_items( source=profilepath ) | |
else: | |
profile_items() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment