-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
111 lines (99 loc) · 3.09 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
#!/usr/bin/env python2.7
"""
Script to install dotfiles
Copyright 2012 Kevin S Lin
Basic Usage:
./install.py
"""
import os
def _collect_files(suffix, ignore_dirs = []):
"""
Collect all files by searching recursively via current directory.
Will ignore directories specified by ignore_dirs
@params:
suffix - suffix to search for
ignore_dirs - list of directories to ignore
@return:
List of tuples (path, [files])
eg.
[(path, [ foo.symlink]), (...),...]
"""
out = []
walker = os.walk(".")
for path, directory, files in walker:
for inum, dname in enumerate(directory):
# filter out all unwanted directories
if (dname in ignore_dirs): del directory[inum]
# gets all files that match the suffix
match = filter(lambda x: x.endswith(suffix), files)
# filter out unwanted suffix
if (match):
out.append((path, match))
return out
def get_response(fname):
prompt = "%s exists. [s]kip, [S]kip All, [o]verwrite: " % fname
resp = ''
while (resp not in ['s', 'S', 'o']):
resp = raw_input(prompt)
return resp
def get_symlinks():
"""
Get all files ending in .symlink
@return:
List of tuples (path, [files])
eg.
[(path, [ foo.symlink]), (...),...]
"""
res = _collect_files("symlink")
return res
### Main
def install(args=None):
"""
Install dotfiles in computer
:params target:
:params skip_all:
"""
print (args)
print ("dest: %s" % args.target)
symlinks = get_symlinks()
for path, links in symlinks:
for link in links:
# Create link to new file
fname = "." + link.rsplit(".symlink")[0]
fpath_old = os.path.join(path, link)
fpath = os.path.join(args['target'], fname)
# handle file collisions
flag_create = True
resp = None
if (os.path.exists(fpath)):
# skip all on, then skip file
if (args['skip_all']):
flag_create = False
else:
resp = get_response(fname)
if (resp == 's'):
flag_create = False
elif (resp == 'S'):
args['skip_all'] = True
flag_create = False
elif (resp.lower() == 'o'):
print ("overwriting...")
os.remove(fpath)
else:
# code should not get here
raise("Something bad has happened")
# Create hard link to dot file
if (flag_create):
print("linking %s to %s" % (fname, fpath))
if not args['dry_run']: os.link(fpath_old, fpath)
else:
print("skipping %s" % fname)
print("done :)")
def main():
#TODO(HACK)
from collections import defaultdict
args = defaultdict(list)
args['target'] = os.path.expanduser("~")
install(args)
if __name__ == "__main__":
main()