-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotfiles.py
executable file
·273 lines (202 loc) · 6.33 KB
/
dotfiles.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/python
import os
import shutil
import sys
import logging
from functools import wraps
from collections import namedtuple
from datetime import datetime
DT_FMT = "%Y-%m-%d_%H-%M-%S"
_id = lambda x: x
_path = os.path.join
_exists = os.path.exists
Env = namedtuple(
'Env', ['home', 'root', 'cfg_path', 'backup_dir', 'files_dir'])
Dotfile = namedtuple('Dotfile', ['name', 'src', 'dst'])
LOG = logging.getLogger()
LOG.addHandler(logging.StreamHandler())
LOG.setLevel(logging.DEBUG)
DEFAULT_CFG = "\n".join((
'.bashrc',
'.conkyrc',
'.emacs',
'.ghci',
'.hgrc',
'.irbrc',
'.jshintrc',
'.profile',
'.pylintrc',
'.screenrc',
'.tmux.conf',
'.vimperatorrc',
'.vimrc',
'.zshrc',
))
def command_registry(cls):
registry = dict((
(name, method.__doc__) for name, method in cls.__dict__.iteritems()
if hasattr(method, 'registry')
))
def dispatch(app):
args = sys.argv[1:]
cmd, params = args[0], args[1:] if args else 'help', []
getattr(app, cmd if cmd in app.registry.keys() else 'help')(*params)
def _help(app, *args):
print("Available commands:\n")
print("\t" + "\n\t".join(
["\t".join((k, v or "")) for k, v in app.registry.iteritems()]
))
cls.registry = registry
cls.dispatch = dispatch
cls.help = _help
return cls
def command(fn):
@wraps(fn)
def wrapper(self, *args, **kwargs):
fn(self, *args, **kwargs)
wrapper.registry = True
return wrapper
@command_registry
class App(object):
def __init__(self):
home = os.environ.get('HOME')
root = os.path.dirname(os.path.abspath(__file__))
self.env = Env(
home,
root,
_path(root, '.dotconfig'),
_path(root, 'backups'),
_path(root, 'files'),
)
self.cfg = self.read_cfg()
def read_cfg(self):
try:
with open(self.env.cfg_path, 'r') as f:
return f.read()
except IOError:
return ""
def write_cfg(self, cfg):
mkdirs(self.env.cfg_path)
with open(self.env.cfg_path, 'w') as f:
f.write(cfg)
return cfg
def mk_dotfile(self, path):
src = _path(self.env.home, normalize(path))
dst = src.replace(self.env.home, self.env.files_dir)
return Dotfile(path, src, dst)
def dotfiles(self, fltr):
if not getattr(self, "_all_dotfiles", None):
self._all_dotfiles = map(
self.mk_dotfile,
set(p for p in self.cfg.splitlines() if p)
)
return filter(fltr, self._all_dotfiles)
@command
def install(self):
self.cfg = self.cfg or self.write_cfg(DEFAULT_CFG)
self.backup()
map(link, self.dotfiles(pending))
@command
def uninstall(self):
map(unlink, self.dotfiles(tracked))
@command
def add(self, *names):
existing_paths = [d.src for d in self.dotfiles(_id)]
names_to_add = set(
n for n in names
if normalize(n) not in existing_paths and _exists(
normalize(n))
)
if names_to_add:
self.write_cfg("\n".join(
names_to_add.union((d.name for d in self.dotfiles(_id)))
))
[link(self.mk_dotfile(n)) for n in names_to_add]
else:
print("No valid files found: {}".format(" ".join(names)))
@command
def status(self):
print("Tracked:\n\t" + "\n\t".join(
(d.name for d in self.dotfiles(tracked))))
print("Pending:\n\t" + "\n\t".join(
(d.name for d in self.dotfiles(pending))))
print("Invalid:\n\t" + "\n\t".join(
(d.name for d in self.dotfiles(lambda x: not valid(x)))
))
@command
def forget(self, *names):
_names = [normalize(n) for n in names]
try:
[unlink(self.mk_dotfile(x)) for x in _names]
except OSError:
pass
self.write_cfg(
"\n".join(
[n for n in self.cfg.splitlines() if n not in _names]
))
@command
def gen_cfg(self):
def _walk(d, _, files):
return "\n".join([
_path(d, f).replace(self.env.files_dir, "~") for f in files])
self.write_cfg("\n".join(
[_walk(*p) for p in os.walk(self.env.files_dir) if p]
))
def backup(self):
dotfiles = self.dotfiles(valid)
if dotfiles:
dirname = _path(
self.env.backup_dir, datetime.strftime(datetime.now(), DT_FMT))
os.makedirs(dirname)
[cp(d.src, _path(dirname, os.path.basename(d.src)))
for d in dotfiles]
# filters -------------------------------------------------------
def valid(dotfile):
return _exists(dotfile.src) or _exists(dotfile.dst)
def pending(dotfile):
return _check_lazy(dotfile, all, (
lambda x: not tracked(x),
valid,
))
def tracked(dotfile):
return _check_lazy(dotfile, all, (
valid,
linked,
))
def linked(dotfile):
try:
return os.readlink(dotfile.src) == dotfile.dst and _exists(dotfile.dst)
except OSError:
return False
# utils -------------------------------------------------------
def _check_lazy(dotfile, method, checks):
return method((check(dotfile) for check in checks))
def link(dotfile):
if not _exists(dotfile.dst):
mkdirs(dotfile.dst)
mv(dotfile.src, dotfile.dst)
os.symlink(dotfile.dst, dotfile.src)
LOG.info("%s linked to %s", dotfile.src, dotfile.dst)
def unlink(dotfile):
mv(dotfile.dst, dotfile.src)
LOG.info("%s unlinked", dotfile.src)
def mkdirs(p):
dirname = os.path.dirname(p)
if not _exists(dirname):
os.makedirs(dirname)
LOG.info("%s created", p)
def cp(src, dst):
(shutil.copytree if os.path.isdir(src) else shutil.copy)(src, dst)
def rm(p):
(os.remove if os.path.islink(p) or os.path.isfile(p) else shutil.rmtree)(p)
def mv(src, dst):
if os.path.islink(src):
_src = os.readlink(src)
rm(src)
cp(_src, src)
shutil.move(src, dst)
LOG.info("%s moved to %s", src, dst)
def normalize(path):
return os.path.expanduser(path.strip("*").rstrip("/"))
if __name__ == "__main__":
App().dispatch()