-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathdotdrop.py
More file actions
988 lines (854 loc) · 31.4 KB
/
dotdrop.py
File metadata and controls
988 lines (854 loc) · 31.4 KB
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
"""
author: deadc0de6 (https://github.com/deadc0de6)
Copyright (c) 2017, deadc0de6
entry point
"""
import os
import sys
import time
import fnmatch
from concurrent import futures
# local imports
from dotdrop.options import Options
from dotdrop.logger import Logger
from dotdrop.templategen import Templategen
from dotdrop.installer import Installer
from dotdrop.uninstaller import Uninstaller
from dotdrop.updater import Updater
from dotdrop.comparator import Comparator
from dotdrop.importer import Importer
from dotdrop.utils import get_tmpdir, removepath, \
uniq_list, ignores_to_absolute, dependencies_met, \
adapt_workers, check_version, pivot_path, dir_empty
from dotdrop.linktypes import LinkTypes
from dotdrop.exceptions import YamlException, \
UndefinedException, UnmetDependency, \
ConfigException, OptionsException
LOG = Logger()
TRANS_SUFFIX = 'trans'
###########################################################
# entry point
###########################################################
def action_executor(opts, actions, defactions, templater, post=False):
"""closure for action execution"""
def execute():
"""
execute actions and return
True, None if ok
False, errstring if issue
"""
actiontype = 'pre' if not post else 'post'
# execute default actions
for action in defactions:
if opts.dry:
LOG.dry(f'would execute def-{actiontype}-action: {action}')
continue
LOG.dbg(f'executing def-{actiontype}-action: {action}')
ret = action.execute(templater=templater, debug=opts.debug)
if not ret:
err = f'def-{actiontype}-action \"{action.key}\" failed'
LOG.err(err)
return False, err
# execute actions
for action in actions:
if opts.dry:
err = f'would execute {actiontype}-action: {action}'
LOG.dry(err)
continue
LOG.dbg(f'executing {actiontype}-action: {action}')
ret = action.execute(templater=templater, debug=opts.debug)
if not ret:
err = f'{actiontype}-action \"{action.key}\" failed'
LOG.err(err)
return False, err
return True, None
return execute
def _dotfile_update(opts, path, key=False):
"""
update a dotfile pointed by path
if key is false or by key (in path)
"""
updater = Updater(opts.dotpath, opts.variables, opts.conf, opts.profile,
dry=opts.dry, safe=opts.safe, debug=opts.debug,
ignore=opts.update_ignore,
showpatch=opts.update_showpatch,
ignore_missing_in_dotdrop=opts.ignore_missing_in_dotdrop)
if key:
return updater.update_key(path)
return updater.update_path(path)
def _dotfile_compare(opts, dotfile, tmp):
"""
compare a dotfile
returns True if same
"""
templ = _get_templater(opts)
ignore_missing_in_dotdrop = opts.ignore_missing_in_dotdrop or \
dotfile.ignore_missing_in_dotdrop
inst = Installer(create=opts.create, backup=opts.backup,
dry=opts.dry, base=opts.dotpath,
workdir=opts.workdir, debug=opts.debug,
backup_suffix=opts.install_backup_suffix,
diff_cmd=opts.diff_command,
force_chmod=True)
comp = Comparator(diff_cmd=opts.diff_command, debug=opts.debug,
ignore_missing_in_dotdrop=ignore_missing_in_dotdrop)
# add dotfile variables
newvars = dotfile.get_dotfile_variables()
templ.add_tmp_vars(newvars=newvars)
# dotfiles does not exist / not installed
LOG.dbg(f'comparing {dotfile}')
src = dotfile.src
if not os.path.lexists(os.path.expanduser(dotfile.dst)):
line = f'=> compare {dotfile.key}: \"{dotfile.dst}\" '
line += 'does not exist on destination'
LOG.log(line)
return False
# apply transformation
tmpsrc = None
if dotfile.trans_install:
LOG.dbg('applying transformation before comparing')
tmpsrc = apply_install_trans(opts.dotpath, dotfile,
templ, debug=opts.debug)
if not tmpsrc:
# could not apply trans
return False
src = tmpsrc
# is a symlink pointing to itself
asrc = os.path.join(opts.dotpath, os.path.expanduser(src))
adst = os.path.expanduser(dotfile.dst)
if os.path.samefile(asrc, adst):
line = f'=> compare {dotfile.key}: diffing with \"{dotfile.dst}\"'
LOG.dbg(line)
LOG.dbg('points to itself')
return True
ignores = list(set(opts.compare_ignore + dotfile.cmpignore))
ignores = ignores_to_absolute(ignores, [dotfile.dst, dotfile.src],
debug=opts.debug)
insttmp = None
if dotfile.template and \
Templategen.path_is_template(src,
debug=opts.debug):
# install dotfile to temporary dir for compare
ret, err, insttmp = inst.install_to_temp(templ, tmp, src, dotfile.dst,
is_template=True,
chmod=dotfile.chmod,
set_create=True)
if not ret:
# failed to install to tmp
line = f'=> compare {dotfile.key} error: {err}'
LOG.log(line)
LOG.err(err)
return False
src = insttmp
# compare
# need to be executed before cleaning
diff = comp.compare(src, dotfile.dst, ignore=ignores, mode=dotfile.chmod)
# clean tmp transformed dotfile if any
if tmpsrc:
tmpsrc = os.path.join(opts.dotpath, tmpsrc)
if os.path.exists(tmpsrc):
# ignore error
removepath(tmpsrc, logger=LOG)
# clean tmp template dotfile if any
if insttmp and os.path.exists(insttmp):
# ignore error
removepath(insttmp, logger=LOG)
if diff != '':
# print diff results
if opts.compare_fileonly:
line = f'=> differ: \"{dotfile.key}\" \"{dotfile.dst}\"'
LOG.log(line)
else:
line = f'=> compare {dotfile.key}: diffing with \"{dotfile.dst}\"'
LOG.log(line)
LOG.emph(diff)
return False
# no difference
line = f'=> compare {dotfile.key}: diffing with \"{dotfile.dst}\"'
LOG.dbg(line)
LOG.dbg('same file')
return True
def _dotfile_install(opts, dotfile, tmpdir=None):
"""
install a dotfile
returns <success, dotfile key, err>
"""
# installer
inst = _get_install_installer(opts, tmpdir=tmpdir)
# templater
templ = _get_templater(opts)
# add dotfile variables
newvars = dotfile.get_dotfile_variables()
templ.add_tmp_vars(newvars=newvars)
preactions = []
if not opts.install_temporary:
preactions.extend(dotfile.get_pre_actions())
defactions = opts.install_default_actions_pre
pre_actions_exec = action_executor(opts, preactions, defactions,
templ, post=False)
LOG.dbg(f'installing dotfile: \"{dotfile.key}\"')
LOG.dbg(dotfile.prt())
ignores = list(set(opts.install_ignore + dotfile.instignore))
ignores = ignores_to_absolute(ignores, [dotfile.dst, dotfile.src],
debug=opts.debug)
is_template = dotfile.template and Templategen.path_is_template(
dotfile.src,
)
if hasattr(dotfile, 'link') and dotfile.link in (
LinkTypes.LINK, LinkTypes.LINK_CHILDREN,
LinkTypes.RELATIVE, LinkTypes.ABSOLUTE
):
# nolink|relative|absolute|link_children
ret, err = inst.install(
templ,
dotfile.src,
dotfile.dst,
dotfile.link,
actionexec=pre_actions_exec,
is_template=is_template,
ignore=ignores,
chmod=dotfile.chmod,
dir_as_block=dotfile.dir_as_block,
)
else:
# nolink
src = dotfile.src
tmp = None
if dotfile.trans_install:
tmp = apply_install_trans(opts.dotpath, dotfile,
templ, debug=opts.debug)
if not tmp:
return False, dotfile.key, None
src = tmp
# make sure to re-evaluate if is template
is_template = dotfile.template and Templategen.path_is_template(src)
ret, err = inst.install(
templ,
src,
dotfile.dst,
LinkTypes.NOLINK,
actionexec=pre_actions_exec,
noempty=dotfile.noempty,
ignore=ignores,
is_template=is_template,
chmod=dotfile.chmod,
dir_as_block=dotfile.dir_as_block,
)
if tmp:
tmp = os.path.join(opts.dotpath, tmp)
if os.path.exists(tmp):
# ignore error
removepath(tmp, logger=LOG)
# check result of installation
if ret:
# dotfile was installed
if not opts.install_temporary:
defactions = opts.install_default_actions_post
postactions = dotfile.get_post_actions()
post_actions_exec = action_executor(opts, postactions, defactions,
templ, post=True)
post_actions_exec()
else:
# dotfile was NOT installed
if opts.install_force_action:
# pre-actions
LOG.dbg('force pre action execution ...')
pre_actions_exec()
# post-actions
LOG.dbg('force post action execution ...')
defactions = opts.install_default_actions_post
postactions = dotfile.get_post_actions()
post_actions_exec = action_executor(opts, postactions, defactions,
templ, post=True)
post_actions_exec()
return ret, dotfile.key, err
def cmd_install(opts):
"""install dotfiles for this profile"""
dotfiles = opts.dotfiles
prof = opts.conf.get_profile()
adapt_workers(opts, LOG)
pro_pre_actions = prof.get_pre_actions() if prof else []
pro_post_actions = prof.get_post_actions() if prof else []
if opts.install_keys:
# filtered dotfiles to install
uniq = uniq_list(opts.install_keys)
dotfiles = [d for d in dotfiles if d.key in uniq]
if not dotfiles:
msg = f'no dotfile to install for this profile (\"{opts.profile}\")'
LOG.warn(msg)
return False
lfs = [k.key for k in dotfiles]
LOG.dbg(f'dotfiles registered for install: {lfs}')
# the installer
tmpdir = None
if opts.install_temporary:
tmpdir = get_tmpdir()
installed = []
# clear the workdir
if opts.install_clear_workdir and not opts.dry:
LOG.dbg(f'clearing the workdir under {opts.workdir}')
for root, _, files in os.walk(opts.workdir):
for file in files:
fpath = os.path.join(root, file)
# ignore error
removepath(fpath, logger=LOG)
# execute profile pre-action
LOG.dbg(f'run {len(pro_pre_actions)} profile pre actions')
templ = _get_templater(opts)
ret, _ = action_executor(opts, pro_pre_actions, [], templ, post=False)()
if not ret:
return False
# install each dotfile
if opts.workers > 1:
# in parallel
LOG.dbg(f'run with {opts.workers} workers')
ex = futures.ThreadPoolExecutor(max_workers=opts.workers)
wait_for = []
for dotfile in dotfiles:
j = ex.submit(_dotfile_install, opts, dotfile, tmpdir=tmpdir)
wait_for.append(j)
# check result
for fut in futures.as_completed(wait_for):
tmpret, key, err = fut.result()
# check result
if tmpret:
installed.append(key)
elif err:
LOG.err(f'installing \"{key}\" failed: {err}')
else:
# sequentially
for dotfile in dotfiles:
tmpret, key, err = _dotfile_install(opts, dotfile, tmpdir=tmpdir)
# check result
if tmpret:
installed.append(key)
elif err:
LOG.err(f'installing \"{key}\" failed: {err}')
# execute profile post-action
if len(installed) > 0 or opts.install_force_action:
msg = f'run {len(pro_post_actions)} profile post actions'
LOG.dbg(msg)
ret, _ = action_executor(opts, pro_post_actions,
[], templ, post=False)()
if not ret:
return False
insts = ','.join(installed)
LOG.dbg(f'install done: installed \"{insts}\"')
if opts.install_temporary:
LOG.log(f'\ninstalled to tmp \"{tmpdir}\".')
LOG.log(f'\n{len(installed)} dotfile(s) installed.')
return True
def _workdir_enum(opts):
workdir_files = []
for root, _, files in os.walk(opts.workdir):
for file in files:
fpath = os.path.join(root, file)
workdir_files.append(fpath)
for dotfile in opts.dotfiles:
src = os.path.join(opts.dotpath, dotfile.src)
if dotfile.link == LinkTypes.NOLINK:
# ignore not link files
continue
if not Templategen.path_is_template(src):
# ignore not template
continue
newpath = pivot_path(dotfile.dst, opts.workdir,
striphome=True, logger=None)
if os.path.isdir(newpath):
# recursive
pattern = f'{newpath}/*'
files = workdir_files.copy()
for file in files:
if fnmatch.fnmatch(file, pattern):
workdir_files.remove(file)
# only checks children
children = [f.path for f in os.scandir(newpath)]
for child in children:
if child in workdir_files:
workdir_files.remove(child)
else:
if newpath in workdir_files:
workdir_files.remove(newpath)
for wfile in workdir_files:
line = f'=> \"{wfile}\" does not exist in dotdrop'
LOG.log(line)
return len(workdir_files)
def cmd_compare(opts, tmp):
"""compare dotfiles and return True if all identical"""
dotfiles = opts.dotfiles
if not dotfiles:
msg = f'no dotfile defined for this profile (\"{opts.profile}\")'
LOG.warn(msg)
return True
# compare only specific files
selected = dotfiles
if opts.compare_focus:
selected = _select(opts.compare_focus, dotfiles)
if len(selected) < 1:
LOG.log('\nno dotfile to compare')
return False
same = True
cnt = 0
if opts.workers > 1:
# in parallel
LOG.dbg(f'run with {opts.workers} workers')
ex = futures.ThreadPoolExecutor(max_workers=opts.workers)
wait_for = []
for dotfile in selected:
if not dotfile.src and not dotfile.dst:
# ignore fake dotfile
continue
j = ex.submit(_dotfile_compare, opts, dotfile, tmp)
wait_for.append(j)
# check result
for fut in futures.as_completed(wait_for):
if not fut.result():
same = False
cnt += 1
else:
# sequentially
for dotfile in selected:
if not dotfile.src and not dotfile.dst:
# ignore fake dotfile
continue
if not _dotfile_compare(opts, dotfile, tmp):
same = False
cnt += 1
if opts.compare_workdir and _workdir_enum(opts) > 0:
same = False
LOG.log(f'\n{cnt} dotfile(s) compared.')
return same
def cmd_update(opts):
"""update the dotfile(s) from path(s) or key(s)"""
cnt = 0
paths = opts.update_path
iskey = opts.update_iskey
if opts.profile not in [p.key for p in opts.profiles]:
LOG.err(f'no such profile \"{opts.profile}\"')
return False
adapt_workers(opts, LOG)
if not paths:
# update the entire profile
if iskey:
LOG.dbg(f'update by keys: {paths}')
paths = [d.key for d in opts.dotfiles]
else:
LOG.dbg(f'update by paths: {paths}')
paths = [d.dst for d in opts.dotfiles]
msg = f'Update all dotfiles for profile \"{opts.profile}\"'
if opts.safe and not LOG.ask(msg):
LOG.log(f'\n{cnt} file(s) updated.')
return False
# check there's something to do
if not paths:
LOG.log('\nno dotfile to update')
return True
LOG.dbg(f'dotfile to update: {paths}')
# update each dotfile
if opts.workers > 1:
# in parallel
LOG.dbg(f'run with {opts.workers} workers')
ex = futures.ThreadPoolExecutor(max_workers=opts.workers)
wait_for = []
for path in paths:
j = ex.submit(_dotfile_update, opts, path, key=iskey)
wait_for.append(j)
# check result
for fut in futures.as_completed(wait_for):
if fut.result():
cnt += 1
else:
# sequentially
for path in paths:
if _dotfile_update(opts, path, key=iskey):
cnt += 1
LOG.log(f'\n{cnt} file(s) updated.')
return cnt == len(paths)
def cmd_importer(opts):
"""import dotfile(s) from paths"""
ret = True
cnt = 0
paths = opts.import_path
importer = Importer(opts.profile, opts.conf,
opts.dotpath, opts.diff_command,
opts.variables,
dry=opts.dry, safe=opts.safe,
debug=opts.debug,
keepdot=opts.keepdot,
ignore=opts.import_ignore,
forcekey=opts.import_force_key)
for path in paths:
tmpret = importer.import_path(path,
import_as=opts.import_as,
import_link=opts.import_link,
import_mode=opts.import_mode,
trans_install=opts.import_trans_install,
trans_update=opts.import_trans_update)
if tmpret < 0:
ret = False
elif tmpret > 0:
cnt += 1
if opts.dry:
LOG.dry('new config file would be:')
LOG.raw(opts.conf.dump())
else:
opts.conf.save()
LOG.log(f'\n{cnt} file(s) imported.')
return ret
def cmd_list_profiles(opts):
"""list all profiles"""
LOG.emph('Available profile(s):\n')
for profile in opts.profiles:
if opts.profiles_grepable:
fmt = f'{profile.key}'
LOG.raw(fmt)
else:
LOG.sub(profile.key, end='')
LOG.log(f' ({len(profile.dotfiles)} dotfiles)')
LOG.log('')
def cmd_files(opts):
"""list all dotfiles for a specific profile"""
if opts.profile not in [p.key for p in opts.profiles]:
LOG.warn(f'unknown profile \"{opts.profile}\"')
return
what = 'Dotfile(s)'
if opts.files_templateonly:
what = 'Template(s)'
LOG.emph(f'{what} for profile \"{opts.profile}\":\n')
for dotfile in opts.dotfiles:
if opts.files_templateonly:
src = os.path.join(opts.dotpath, dotfile.src)
if not Templategen.path_is_template(src):
continue
if opts.files_grepable:
fmt = f'{dotfile.key},'
fmt += f'dst:{dotfile.dst},'
fmt += f'src:{dotfile.src},'
fmt += f'link:{dotfile.link.name.lower()}'
if dotfile.chmod:
fmt += f',chmod:{dotfile.chmod:o}'
else:
fmt += ',chmod:None'
LOG.raw(fmt)
else:
LOG.log(f'{dotfile.key}', bold=True)
LOG.sub(f'dst: {dotfile.dst}')
LOG.sub(f'src: {dotfile.src}')
LOG.sub(f'link: {dotfile.link.name.lower()}')
if dotfile.chmod:
LOG.sub(f'chmod: {dotfile.chmod:o}')
LOG.log('')
def cmd_detail(opts):
"""list details on all files for all dotfile entries"""
if opts.profile not in [p.key for p in opts.profiles]:
LOG.warn(f'unknown profile \"{opts.profile}\"')
return
dotfiles = opts.dotfiles
if opts.detail_keys:
# filtered dotfiles to install
uniq = uniq_list(opts.detail_keys)
dotfiles = [d for d in dotfiles if d.key in uniq]
LOG.emph(f'dotfiles details for profile \"{opts.profile}\":\n')
for dotfile in dotfiles:
_detail(opts.dotpath, dotfile)
LOG.log('')
def cmd_uninstall(opts):
"""uninstall"""
dotfiles = opts.dotfiles
keys = opts.uninstall_key
if keys:
# uninstall only specific keys for this profile
dotfiles = []
for key in uniq_list(keys):
dotfile = opts.conf.get_dotfile(key)
if dotfile:
dotfiles.append(dotfile)
if not dotfiles:
msg = f'no dotfile to uninstall for this profile (\"{opts.profile}\")'
LOG.warn(msg)
return False
if opts.debug:
lfs = [k.key for k in dotfiles]
LOG.dbg(f'dotfiles registered for uninstall: {lfs}')
uninst = Uninstaller(base=opts.dotpath,
workdir=opts.workdir,
dry=opts.dry,
safe=opts.safe,
debug=opts.debug,
backup_suffix=opts.install_backup_suffix)
uninstalled = 0
for dotf in dotfiles:
res, msg = uninst.uninstall(dotf.src,
dotf.dst,
dotf.link)
if not res:
LOG.err(msg)
continue
uninstalled += 1
LOG.log(f'\n{uninstalled} dotfile(s) uninstalled.')
return True
def cmd_remove(opts):
"""remove dotfile from dotpath and from config"""
paths = opts.remove_path
iskey = opts.remove_iskey
if not paths:
LOG.log('no dotfile to remove')
return False
pathss = ','.join(paths)
LOG.dbg(f'dotfile(s) to remove: {pathss}')
removed = []
for key in paths:
if not iskey:
# by path
dotfiles = opts.conf.get_dotfile_by_dst(key)
if not dotfiles:
LOG.warn(f'{key} ignored, does not exist')
continue
else:
# by key
dotfile = opts.conf.get_dotfile(key)
if not dotfile:
LOG.warn(f'{key} ignored, does not exist')
continue
dotfiles = [dotfile]
for dotfile in dotfiles:
k = dotfile.key
# ignore if uses any type of link
if dotfile.link != LinkTypes.NOLINK:
msg = f'{k} uses symlink, remove manually'
LOG.warn(msg)
continue
LOG.dbg(f'removing {key}')
# make sure is part of the profile
if dotfile.key not in [d.key for d in opts.dotfiles]:
msg = f'{key} ignored, not associated to this profile'
LOG.warn(msg)
continue
profiles = opts.conf.get_profiles_by_dotfile_key(k)
pkeys = ','.join([p.key for p in profiles])
if opts.dry:
LOG.dry(f'would remove {dotfile} from {pkeys}')
continue
msg = f'Remove \"{k}\" from all these profiles: {pkeys}'
if opts.safe and not LOG.ask(msg):
return False
LOG.dbg(f'remove dotfile: {dotfile}')
for profile in profiles:
if not opts.conf.del_dotfile_from_profile(dotfile, profile):
return False
if not opts.conf.del_dotfile(dotfile):
return False
# remove dotfile from dotpath
dtpath = os.path.join(opts.dotpath, dotfile.src)
removepath(dtpath, logger=LOG)
# remove empty directory
parent = os.path.dirname(dtpath)
# remove any empty parent up to dotpath
while parent != opts.dotpath:
if os.path.isdir(parent) and dir_empty(parent):
msg = f'Remove empty dir \"{parent}\"'
if opts.safe and not LOG.ask(msg):
break
if not removepath(parent, logger=LOG):
LOG.warn(f'unable to remove {parent}')
parent = os.path.dirname(parent)
removed.append(dotfile)
if opts.dry:
LOG.dry('new config file would be:')
LOG.raw(opts.conf.dump())
else:
opts.conf.save()
if removed:
LOG.log('\nFollowing dotfile(s) are not tracked anymore:')
entries = [f'- \"{r.dst}\" (was tracked as \"{r.key}\")'
for r in removed]
LOG.log('\n'.join(entries))
else:
LOG.log('\nno dotfile removed')
return True
###########################################################
# helpers
###########################################################
def _get_install_installer(opts, tmpdir=None):
"""get an installer instance for cmd_install"""
inst = Installer(create=opts.create, backup=opts.backup,
dry=opts.dry, safe=opts.safe,
base=opts.dotpath, workdir=opts.workdir,
diff=opts.install_diff, debug=opts.debug,
totemp=tmpdir,
showdiff=opts.install_showdiff,
backup_suffix=opts.install_backup_suffix,
diff_cmd=opts.diff_command,
remove_existing_in_dir=opts.install_remove_existing,
force_chmod=opts.install_force_chmod)
return inst
def _get_templater(opts):
"""get an templater instance"""
templ = Templategen(base=opts.dotpath, variables=opts.variables,
func_file=opts.func_file, filter_file=opts.filter_file,
debug=opts.debug)
return templ
def _detail(dotpath, dotfile):
"""display details on all files under a dotfile entry"""
entry = f'{dotfile.key}'
attribs = []
attribs.append(f'dst: \"{dotfile.dst}\"')
attribs.append(f'link: \"{dotfile.link.name.lower()}\"')
attribs.append(f'chmod: \"{dotfile.chmod}\"')
attrs = ', '.join(attribs)
LOG.log(f'{entry} ({attrs})')
path = os.path.join(dotpath, os.path.expanduser(dotfile.src))
if not os.path.isdir(path):
template = 'no'
if dotfile.template and Templategen.path_is_template(path):
template = 'yes'
LOG.sub(f'{path} (template:{template})')
else:
for root, _, files in os.walk(path):
for file in files:
fpath = os.path.join(root, file)
template = 'no'
if dotfile.template and Templategen.path_is_template(fpath):
template = 'yes'
LOG.sub(f'{fpath} (template:{template})')
def _select(selections, dotfiles):
selected = []
for selection in selections:
dotfile = next(
(x for x in dotfiles
if os.path.expanduser(x.dst) == os.path.expanduser(selection)),
None
)
if dotfile:
selected.append(dotfile)
else:
LOG.err(f'no dotfile matches \"{selection}\"')
return selected
def apply_install_trans(dotpath, dotfile, templater, debug=False):
"""
apply the install transformation to the dotfile
return None if fails and new source if succeed
"""
src = dotfile.src
new_src = f'{src}.{TRANS_SUFFIX}'
trans = dotfile.trans_install
LOG.dbg(f'executing install transformation: {trans}')
srcpath = os.path.join(dotpath, src)
temp = os.path.join(dotpath, new_src)
if not trans.transform(srcpath, temp, templater=templater, debug=debug):
msg = f'install transformation \"{trans.key}\"'
msg += f'failed for {dotfile.key}'
LOG.err(msg)
if new_src and os.path.exists(new_src):
# ignore error
removepath(new_src, logger=LOG)
return None
return new_src
###########################################################
# main
###########################################################
def _exec_command(opts):
"""execute command"""
ret = True
command = ''
try:
if opts.cmd_profiles:
# list existing profiles
command = 'profiles'
LOG.dbg(f'running cmd: {command}')
cmd_list_profiles(opts)
elif opts.cmd_files:
# list files for selected profile
command = 'files'
LOG.dbg(f'running cmd: {command}')
cmd_files(opts)
elif opts.cmd_install:
# install the dotfiles stored in dotdrop
command = 'install'
LOG.dbg(f'running cmd: {command}')
ret = cmd_install(opts)
elif opts.cmd_compare:
# compare local dotfiles with dotfiles stored in dotdrop
command = 'compare'
LOG.dbg(f'running cmd: {command}')
tmp = get_tmpdir()
ret = cmd_compare(opts, tmp)
# clean tmp directory
# ignore any error
removepath(tmp, logger=LOG)
elif opts.cmd_import:
# import dotfile(s)
command = 'import'
LOG.dbg(f'running cmd: {command}')
ret = cmd_importer(opts)
elif opts.cmd_update:
# update a dotfile
command = 'update'
LOG.dbg(f'running cmd: {command}')
ret = cmd_update(opts)
elif opts.cmd_detail:
# detail files
command = 'detail'
LOG.dbg(f'running cmd: {command}')
cmd_detail(opts)
elif opts.cmd_remove:
# remove dotfile
command = 'remove'
LOG.dbg(f'running cmd: {command}')
cmd_remove(opts)
elif opts.cmd_uninstall:
# uninstall dotfile
command = 'uninstall'
LOG.dbg(f'running cmd: {command}')
cmd_uninstall(opts)
except UndefinedException as exc:
LOG.err(exc)
ret = False
except KeyboardInterrupt:
LOG.err('interrupted')
ret = False
return ret, command
def main():
"""entry point"""
# check dependencies are met
try:
dependencies_met()
except UnmetDependency as exc:
LOG.err(exc)
return False
time0 = time.time()
try:
opts = Options()
except YamlException as exc:
LOG.err(f'yaml error: {exc}')
return False
except ConfigException as exc:
LOG.err(f'config error: {exc}')
return False
except UndefinedException as exc:
LOG.err(f'dependencies error: {exc}')
return False
except OptionsException as exc:
LOG.err(f'options error: {exc}')
return False
if opts.debug:
LOG.debug = opts.debug
LOG.dbg('\n\n')
options_time = time.time() - time0
if opts.check_version:
check_version()
time0 = time.time()
ret, command = _exec_command(opts)
cmd_time = time.time() - time0
opts.debug_command()
LOG.dbg(f'done executing command \"{command}\"')
LOG.dbg(f'options loaded in {options_time}')
LOG.dbg(f'command executed in {cmd_time}')
if ret and opts.conf.save():
LOG.log('config file updated')
LOG.dbg(f'return {ret}')
return ret
if __name__ == '__main__':
if main():
sys.exit(0)
sys.exit(1)