forked from majnemer/davesdots
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinstall.py
executable file
·219 lines (173 loc) · 5.54 KB
/
install.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#! /usr/bin/env python
import os
import os.path
import optparse
import shutil
links = {
'screenrc': '.screenrc',
'ackrc': '.ackrc',
'toprc': '.toprc',
'dir_colors': '.dir_colors',
'lessfilter': '.lessfilter',
'vim': '.vim',
'vimrc': '.vimrc',
'_vimrc': '_vimrc',
'gvimrc': '.gvimrc',
'emacsrc': '.emacs',
'emacs': '.emacsdir',
'commonsh': '.commonsh',
'inputrc': '.inputrc',
'bash': '.bash',
'bashrc': '.bashrc',
'bash_profile': '.bash_profile',
'zsh': '.zsh',
'zshrc': '.zshrc',
'ksh': '.ksh',
'kshrc': '.kshrc',
'mkshrc': '.mkshrc',
'shinit': '.shinit',
'Xdefaults': '.Xdefaults',
'Xresources': '.Xresources',
'uncrustify.cfg': '.uncrustify.cfg',
'indent.pro': '.indent.pro',
'xmobarrc': '.xmobarrc',
'xmonad.hs': '.xmonad/xmonad.hs',
'Wombat.xccolortheme': 'Library/Application Support/Xcode/Color Themes/Wombat.xccolortheme',
# 'Wombat.dvtcolortheme': 'Library/Developer/Xcode/UserData/FontAndColorThemes/Wombat.dvtcolortheme',
'gitconfig': '.gitconfig',
'gitignore': '.gitignore',
'tigrc': '.tigrc',
'caffeinate': 'bin/caffeinate',
'lock': 'bin/lock',
'git-info': 'bin/git-info',
'git-untrack-ignored': 'bin/git-untracked-ignored',
'gdbinit': '.gdbinit',
}
scriptdir = os.path.dirname(os.path.realpath(__file__))
home = os.path.expanduser('~')
contained = os.path.commonprefix([scriptdir, home]) == home
def read_link_abs(path):
"""Read the absolute path to which the symbolic link points.
Args:
path - path to the link
Returns:
the target absolute path or the empty string if path isn't a link
"""
if not os.path.islink(path):
return ''
target = os.path.join(os.path.dirname(path), os.readlink(path))
return os.path.abspath(target)
def uninstall(file):
"""Uninstall a file.
Args:
file - the file to uninstall
Returns:
True if the file was uninstalled, False otherwise
"""
src = os.path.join(scriptdir, file)
dest = os.path.join(home, links[file])
# Only remove if it's a link to a dotfile in the repo
if read_link_abs(dest) != src:
return False
os.remove(dest)
# Remove empty directories that contained the link
dir = os.path.dirname(dest)
while dir and not os.listdir(dir):
os.rmdir(dir)
dir = os.path.dirname(dir)
return True
def uninstall_all():
uname = os.uname()[0]
links['answerback.' + uname] = 'bin/answerback.' + uname
links['gitprivate'] = '.gitprivate'
i = 0; # Keep track of how many links we remove
for file in links:
if uninstall(file):
i += 1
print '{0:d} link{1} removed'.format(i, 's' if i != 1 else '')
def install(file, force=False):
"""Install a file.
Args:
file - the file to install
force - whether to overwrite existing files
Returns:
True if the file was installed, False otherwise
"""
src = os.path.join(scriptdir, file)
dest = os.path.join(home, links[file])
path = os.path.dirname(dest)
# If needed, create the directory this file resides in
if not os.path.exists(path):
os.makedirs(path)
# unlike exists, lexists will return true for broken symlinks
if os.path.lexists(dest):
if force:
# Remove the destination if it exists
if os.path.isdir(dest) and not os.path.islink(dest):
shutil.rmtree(dest)
else:
os.remove(dest)
else:
# If a link already exists, see if it points to this file
# to prevent extra warnings caused by previous runs
if read_link_abs(dest) != src:
print 'Could not link "{0}" to "{1}": File exists'.format(src, dest)
return False
# Use relative links if the dotfiles are contained in home
if contained:
os.chdir(home)
src = os.path.relpath(src, path)
dest = links[file]
os.symlink(src, dest)
return True
def install_all(force=False):
# Compile answerback and add it to links
uname = os.uname()[0]
answerback_src = os.path.join(scriptdir, 'answerback.c')
answerback_bin = os.path.join(scriptdir, 'answerback.' + uname)
if os.system('cc {0} -o {1}'.format(answerback_src, answerback_bin)) == 0:
links['answerback.' + uname] = 'bin/answerback.' + uname
else:
print 'Could not compile answerback.'
# Create private git config and add it to links
if not os.path.exists(os.path.join(scriptdir, 'gitprivate')):
name = raw_input('Full name for Git config (enter to skip): ')
email = raw_input('Email address for Git config (enter to skip): ')
if name or email:
with open(os.path.join(scriptdir, 'gitprivate'), 'w') as config:
config.write('[user]\n')
if name:
config.write('\tname = ' + name + '\n')
if email:
config.write('\temail = ' + email + '\n')
links['gitprivate'] = '.gitprivate'
else:
links['gitprivate'] = '.gitprivate'
i = 0; # Keep track of how many links we added
for file in links:
if install(file, force):
i += 1
print '{0:d} link{1} created'.format(i, 's' if i != 1 else '')
def main():
class RawHelpFormatter(optparse.IndentedHelpFormatter):
def format_description(self, description):
return description + '\n'
# Parse arguments
parser = optparse.OptionParser(
usage='usage: %prog [-h] [-u] [-f]',
formatter=RawHelpFormatter(),
description= \
"""Installs symbolic links from dotfile repo into your home directory
Destination directory is "{0}".
Source files are in "{1}".""".format(home, scriptdir))
parser.add_option('-u', '--uninstall', action='store_true',
help='unlink dotfiles from your home directory')
parser.add_option('-f', '--force', action='store_true',
help='force to overwrite existing files')
args, _ = parser.parse_args()
if args.uninstall:
uninstall_all()
else:
install_all(args.force)
if __name__ == '__main__':
main()