forked from svetlyak40wt/dotfiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
530 lines (415 loc) · 18.7 KB
/
core.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# coding: utf-8
from __future__ import absolute_import
import os
import re
import subprocess
from itertools import groupby
from .real_filesystem import RealFS
from .virtual_fs import VirtualFS
from .logging import (log_mkdir, log_link, log_verbose,
log_error, log_rm)
class File(object):
def __init__(self, name, envs):
self.name = name
self.envs = envs
def __repr__(self):
return 'File: ({0})/{1}'.format(
'|'.join(self.envs),
self.name)
def __eq__(self, right):
return (isinstance(right, File) and
self.name == right.name and
self.envs == right.envs)
class Dir(object):
def __init__(self, name, envs, children=None):
self.name = name
self.envs = list(set(envs))
self.children = children or []
def __repr__(self):
return 'Dir: ({0})/{1}/[{2}]'.format(
'|'.join(self.envs),
self.name,
', '.join(map(repr, self.children)))
def __eq__(self, right):
return (isinstance(right, Dir) and
self.name == right.name and
self.envs == right.envs and
self.children == right.children)
def processor_real(actions, created_links, fs):
new_created_links = created_links.copy()
def mkdir(dir):
fs.mkdir(dir)
log_mkdir('Directory {0} was created.'.format(dir))
def rm(dir):
fs.rm(dir)
new_created_links.pop(dir, None)
log_rm('Symlink {0} was removed.'.format(dir))
def link(source, target):
fs.symlink(source, target)
new_created_links[target] = source
log_link('Symlink from {0} to {1} was created'.format(
target, source))
def already_linked(source, target):
log_verbose('Symlink from {0} to {1} already exists'.format(
target, source))
def error(message):
log_error(message)
mapping = locals()
for action in actions:
mapping[action[0].replace('-', '_')](*action[1:])
return new_created_links
def processor_dry(actions, created_links, fs):
mapping = {'mkdir': (log_mkdir, 'Directory {0} will be created'),
'link': (log_link, 'Symlink from {1} to {0} will be created'),
'already-linked': (
log_verbose, 'Symlink from {1} to {0} already exists'),
'error': (log_error, '{0}'),
'rm': (log_rm, 'Symlink {0} will be removed.')}
for action in actions:
func, fmt = mapping[action[0]]
func(fmt.format(*action[1:]))
return created_links
def create_tree_from_text(text):
head = lambda item: item[0]
tail = lambda item: item[1:]
# parse text
lines = (line.strip() for line in text.split('\n'))
lines = [line.split('/') for line in lines if line]
lines.sort(key=lambda x: x[1:])
# extract environments, they are first level directories
envs = map(head, lines)
lines = map(tail, lines)
# now, each item in lines will be tuple, where second item it's env
lines = zip(lines, envs)
extract_envs = lambda lines: [line[1] for line in lines]
def process(*lines):
if not filter(lambda x: x[0][0], lines):
return ()
else:
grouped = groupby(lines, key=lambda line: line[0][0]) # group by first path item
# make it lists, not iterators
grouped = [(key, list(items))
for key, items in grouped]
# here is we are doing woodoo magick with items in pipeline
grouped = [(key,
filter(lambda x: x[0], [(tail(item[0]), item[1]) for item in items]),
extract_envs(items))
for key, items in grouped]
grouped = [Dir(key, envs, children=process(*reminder)) if reminder else File(key, envs)
for key, reminder, envs in grouped]
grouped = filter(None, grouped)
return grouped
return process(*lines)
def create_tree_from_filesystem(base_dir, envs):
ignored_dirs = {'.git'}
ignored_files_re = re.compile(ur'(readme.*|news|changelog.*|.gitignore|tags)',
re.I)
base_dir_len = len(base_dir)
text = u''
for env in envs:
env_path = os.path.join(base_dir, env)
for root, dirs, files in os.walk(env_path):
dirs[:] = [d for d in dirs if d not in ignored_dirs]
files[:] = [f for f in files if ignored_files_re.match(f) is None]
for filename in files:
full_path = os.path.join(root, filename)
text += full_path[base_dir_len + 1:]
text += u'\n'
return create_tree_from_text(text)
def create_install_actions(base_dir, home_dir, tree, filesystem):
actions = []
vfs = VirtualFS(filesystem)
def push_action(action, *args):
action = (action,) + args
# each destructive action is applied to the
# virtual filesystem to get actual view
if action[0] in ('rm', 'mkdir', 'link'):
getattr(vfs, action[0])(*action[1:])
if len(filter(lambda a: a == action, actions)) == 0:
actions.append(action)
def push_actions(actions):
for action in actions:
push_action(*action)
def walk(items, prefix=tuple()):
"""Returns tuples (path, env, alternatives) to where path is a
path to a leaf item of the file tree or a
directory with files from one env only.
Alternatives, is the structure and could be tried in case
when initial path already exists."""
for item in items:
new_prefix = prefix + (item.name,)
children = getattr(item, 'children', [])
if len(item.envs) > 1:
if children:
for result in walk(children, prefix=new_prefix):
yield result
else:
yield (prefix + (item.name,),
item.envs,
[])
else:
yield (new_prefix,
item.envs,
walk(children, prefix=new_prefix))
def process(path, envs, alternatives):
if len(envs) > 1:
push_action('error', 'File {0} exists in more then one environments: {1}'.format(
os.path.join(*path), ', '.join(envs)))
else:
source = os.path.join(base_dir, envs[0], *path)
target = os.path.join(home_dir, *path)
if True:
exists = vfs.exists(target)
is_symlink = exists and vfs.is_symlink(target)
target_dir = os.path.dirname(target)
in_symlinked_directory = vfs.realpath(target_dir) != target_dir
realpath = vfs.realpath(target)
already_linked = exists and realpath == source
symlink_target = vfs.get_symlink_target(target) if is_symlink else None
symlink_outside_base_dir = is_symlink and not symlink_target.startswith(base_dir)
symlink_to_some_other_dotfile = (is_symlink
and symlink_target.startswith(base_dir)
and symlink_target != source)
if already_linked and not in_symlinked_directory:
push_action('already-linked', source, target)
elif symlink_outside_base_dir:
push_action('error', 'File {0} is a symlink to {1}, please, remove it manually if you really want to replace it.'.format(
target, symlink_target))
elif not exists or symlink_to_some_other_dotfile or in_symlinked_directory:
# now, add actions to create all intermediate directories
# but only if there isn't such actions already
mkdirs = []
for i in range(1, len(path)):
dirname = os.path.join(home_dir, *path[:-i])
if vfs.exists(dirname):
if vfs.is_symlink(dirname):
symlink_target = vfs.get_symlink_target(dirname)
if symlink_target.startswith(base_dir):
push_action('rm', dirname)
push_action('mkdir', dirname)
else:
push_action('error', 'Intermediate directory {0} is a symlink to {1}, please remove it manually.'.format(
dirname, symlink_target))
break
else:
action = ('mkdir', dirname)
if action not in actions:
mkdirs.insert(0, action)
if not actions or actions[-1][0] != 'error':
push_actions(mkdirs)
if symlink_to_some_other_dotfile:
push_action('rm', target)
push_action('link', source, target)
else:
################## Other actions
already_exists_but_not_symlink = exists and not is_symlink
if symlink_to_some_other_dotfile:
# TODO REMOVE
push_action('rm', target)
push_action('link', source, target)
if already_exists_but_not_symlink:
alternatives = list(alternatives)
if alternatives:
process(*alternatives[0])
else:
push_action('error', 'File {0} already exists, can\'t make symlink instead of it.'.format(target))
if False:
# для файлов, которые не внутри засимлинканой директории
if vfs.exists(target) and vfs.realpath(target) == target:
# если сам файл не является симлинком, то это ошибка
if not vfs.is_symlink(target):
# TODO: REMOVE
push_action('error', 'File {0} already exists, can\'t make symlink instead of it.'.format(target))
else:
symlink_target = vfs.get_symlink_target(target)
if symlink_target.startswith(base_dir):
if symlink_target == source:
# TODO: REMOVE
push_action('already-linked', source, target)
else:
# TODO: REMOVE
push_action('rm', target)
push_action('link', source, target)
else:
# TODO: REMOVE
push_action('error', 'File {0} is a symlink to {1}, please, remove it manually if you really want to replace it.'.format(
target, symlink_target))
else:
# TODO: REMOVE
# now, add actions to create all intermediate directories
# but only if there isn't such actions already
mkdirs = []
for i in range(1, len(path)):
dirname = os.path.join(home_dir, *path[:-i])
if vfs.exists(dirname):
if vfs.is_symlink(dirname):
symlink_target = vfs.get_symlink_target(dirname)
if symlink_target.startswith(base_dir):
push_action('rm', dirname)
push_action('mkdir', dirname)
else:
push_action('error', 'Intermediate directory {0} is a symlink to {1}, please remove it manually.'.format(
dirname, symlink_target))
break
else:
action = ('mkdir', dirname)
if action not in actions:
mkdirs.insert(0, action)
if not actions or actions[-1][0] != 'error':
push_actions(mkdirs)
push_action('link', source, target)
for item in walk(tree):
process(*item)
return actions
def create_actions_to_remove_broken_symlinks(created_links, fs):
"""Removes dangling symlinks, created during previous 'dot update' calls.
This could happen when you remove or rename some file in the environment.
"""
results = []
for source, target in created_links.items():
if fs.exists(source) \
and fs.is_symlink(source) \
and fs.realpath(source) == target \
and not fs.exists(target):
results.append(('rm', source))
return results
def _get_envs(base_dir):
"""Searches installed environments in the base_dir.
"""
ignored_dirs = ['.git', 'bin']
envs = os.listdir(base_dir)
envs = [env
for env in envs
if os.path.isdir(os.path.join(base_dir, env)) and env not in ignored_dirs]
return envs
def _current_env_has_remote_upstream():
"""Returns True, if repository at CWD has at least one
remote upstream."""
if os.path.exists('.git'):
process = subprocess.Popen(['git', 'remote'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = process.stdout.read()
return bool(stdout)
return False
def make_pull(base_dir, env):
pwd = os.getcwd()
try:
os.chdir(os.path.join(base_dir, env))
if _current_env_has_remote_upstream():
log_verbose('Making pull in "{0}":'.format(env))
process = subprocess.Popen(['git', 'pull'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in process.stdout:
log_verbose(' ' * 4 + line.strip())
finally:
os.chdir(pwd)
def update(base_dir, home_dir, args,
processor=None,
tree_builder=None):
dry_run = args['--dry']
envs = _get_envs(base_dir)
if not args['--skip-pull']:
for env in envs:
make_pull(base_dir, env)
# create a files tree
if tree_builder is None:
tree_builder = create_tree_from_filesystem
tree = tree_builder(base_dir, envs)
fs = RealFS()
# now, generate 'rm' actions for broken symlinks, among created
# during previous 'dot update' invocation
created_links_filename = os.path.join(base_dir, '.created-links')
if os.path.exists(created_links_filename):
with open(created_links_filename) as f:
created_links = dict((line.strip().split(' -> '))
for line in f.readlines())
remove_actions = create_actions_to_remove_broken_symlinks(created_links, fs)
else:
created_links = {}
remove_actions = []
# next, generate actions to create necessary symlinks
actions = create_install_actions(base_dir, home_dir, tree, fs)
if processor is None:
processor = processor_dry if dry_run else processor_real
created_links = processor(remove_actions + actions, created_links, fs)
if not dry_run:
with open(created_links_filename, 'w') as f:
f.writelines('{0} -> {1}\n'.format(*item)
for item in sorted(created_links.items()))
def status(base_dir, home_dir, args):
envs = _get_envs(base_dir)
cwd = os.getcwd()
try:
for env in envs:
full_path = os.path.join(base_dir, env)
os.chdir(full_path)
if os.path.exists('.git'):
lines = []
# check if it has remotes first, because if dont, than it is bad!
if not _current_env_has_remote_upstream():
lines.append('This repository has no remote upstream.')
# next check repository's status
process = subprocess.Popen(['git', 'status', '--porcelain', '--branch'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = process.stdout.read()
if stdout:
def replace_ahead(line):
if line.startswith('##'):
match = re.match('^##.*\[ahead (.*?)].*$', line)
if match:
return 'Has {0} not pushed change(s).'.format(match.group(1))
else:
return line
new_lines = [line for line in stdout.split('\n')]
new_lines = map(replace_ahead, new_lines)
new_lines = filter(None, new_lines)
lines.extend(new_lines)
# and finally, print all findings
if lines:
print env
print '\n'.join(' ' + line for line in lines)
else:
print env
print ' Is not version controlled.'
finally:
os.chdir(cwd)
def _normalize_url(url):
"""Returns tuple (real_url, env_name), using
following rules:
- if url has scheme, its returned as is.
- if url is in the form username/repo, then
we consider they are username/repo at the github
and return full https url.
- env_name is a last part of the path with removed
'.git' suffix and 'dot[^-]*-' prefix.
"""
# extract name
name = url.rsplit('/', 1)[-1]
name = re.sub(r'^dot[^-]*-', '', name)
name = re.sub(r'\.git$', '', name)
# check if this is a github shortcut
match = re.match('^([^/:]+)/([^/]+)$', url)
if match is not None:
url = 'https://github.com/' + url
return (url, name)
def _add_url(url):
"""Installs repo from given url at current dir.
"""
url, env = _normalize_url(url)
if os.path.exists(env):
log_error('Environment "{0}" already exists.'.format(env))
else:
log_verbose('Cloning repository "{0} to "{1}" dir.'.format(url, env))
process = subprocess.check_call(['git', 'clone', url, env])
def add(base_dir, home_dir, args):
urls = args['<url>']
original_cwd = os.getcwd()
os.chdir(base_dir)
try:
for url in urls:
_add_url(url)
finally:
os.chdir(original_cwd)
COMMANDS = dict(update=update,
status=status,
add=add)