-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·102 lines (86 loc) · 2.85 KB
/
setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
import sys
import re
import os
import platform
import shutil
import datetime
files = [ ".gitconfig", ".vim", ".vimrc", ".myconfigs", ".hgrc" ]
bash_files = [ ".inputrc", ".zshrc", ".tmux.conf", ".linux", ".bash_profile", \
".myconfigs", ".bashrc" ]
powershell_files = [ "Microsoft.PowerShell_profile.ps1" ];
def print_usage():
global dir_delim
print "Usage:"
print "help\t\tPrint usage"
print "install\t\tInstall dotfiles to ~/.<file>"
print "uninstall\tUninstall dotfiles from ~/.<file>"
def get_platform_files():
global files, bash_files, powershell_files
if re.search(r"(linux|darwin)", sys.platform):
print "Running on bash."
files.extend(bash_files)
return files
if re.search(r"win", sys.platform):
print "Running on powershell"
raise Exception("Not implemented for windows")
return files
def install():
files_to_install = get_platform_files()
home_dir = os.path.expanduser("~")
backup_dir = os.path.join(home_dir, "dotfiles_backup_" + datetime.datetime.now().isoformat())
dotfiles_dir = os.path.realpath(os.path.dirname(__file__))
#Create backup dir
os.mkdir(backup_dir)
for file in files_to_install:
fullpath = os.path.join(home_dir, file)
if os.path.exists(fullpath) or os.path.islink(fullpath):
print "Backing up %s... " % fullpath
if os.path.isdir(fullpath):
shutil.copytree(fullpath, os.path.join(backup_dir, file))
shutil.rmtree(fullpath)
else:
shutil.copy2(fullpath, os.path.join(backup_dir, file))
os.remove(fullpath)
source = os.path.join(dotfiles_dir, file)
dest = os.path.join(home_dir, file)
print "Symlinking " + source + " to " + dest
os.symlink(source, dest)
def uninstall():
files_to_uninstall = get_platform_files()
homedir = os.path.expanduser("~") + dir_delim
for file in files_to_uninstall:
fullpath = os.path.expanduser("~") + dir_delim + "." + file
if os.path.exists(fullpath) or os.path.islink(fullpath):
print "Removing " + fullpath + "..."
try:
os.unlink(fullpath)
except:
shutil.rmtree(fullpath)
else:
print "File %s not found..." % fullpath
for afile in os.listdir(homedir):
if afile.startswith("."):
ans = raw_input("Would you like to remove this dot file: " + afile + " (Y/q)? ")
if ans == "Y":
print "Removing", afile
if os.path.isdir(homedir + afile):
shutil.rmtree(homedir + afile)
else:
os.unlink(homedir + afile)
elif ans == "q":
return
if __name__ == "__main__":
if len(sys.argv) != 2:
print "setup takes one argument"
print_usage()
sys.exit()
if sys.argv[1] == "install":
install()
elif sys.argv[1] == "uninstall":
uninstall()
elif sys.argv[1] == "help":
print_usage()
else:
print "Command not valid"
print_usage()