-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotfiles.py
executable file
·170 lines (125 loc) · 4.8 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
#!/usr/bin/env python3
import os
import yaml
from pathlib import Path
HOME = os.path.expanduser("~")
def build_constructor_map(cls):
def constructor(loader, node) :
fields = loader.construct_mapping(node)
return cls(**fields)
return constructor
def build_constructor_seq(cls):
def constructor(loader, node) :
fields = loader.construct_sequence(node)
return cls(*fields)
return constructor
# Maintain abspath, because os.listdir doesn't
def listdir(path):
return [os.path.join(path, p) for p in os.listdir(path)]
class PromptPath(Path):
# This is fugly but it works™
def __new__(self, query, default=None):
return Path(input(f"Specify {query} " +
(f"({default})" if default else "") +
": ") or default)
yaml.add_constructor('!PromptPath', build_constructor_map(PromptPath))
yaml.add_constructor('!Path', build_constructor_seq(Path))
class Dotfile:
VERBOSE = False
def __init__(self, source, target, dotify=False, create_parent=False):
self.source = source
target_dir = os.path.dirname(target)
target_file = os.path.basename(target)
if dotify:
target_file = ".{}".format(target_file)
self.target = os.path.expanduser(os.path.join(target_dir, target_file))
self.create_parent = create_parent
def install(self):
if os.path.exists(self.target):
raise FileExistsError("Destination file {} already exists".format(self.target))
d = os.path.dirname(self.target)
if not os.path.exists(d):
if self.create_parent:
if self.VERBOSE:
print(f"Creating directory {d}")
else:
os.makedirs(d, exist_ok=True)
else:
raise FileNotFoundError("Parent directory doesn't exist")
if self.VERBOSE:
print(f"Symlinking {self.source} to {self.target}")
else:
os.symlink(self.source, self.target)
class DotfileDirectory:
# TODO(?): make this nestable
def __init__(self, directory):
self.directory = directory
layout_file = os.path.join(directory, "layout.yaml")
self.layout = dict()
self.dir_settings = {}
self.defaults = {}
if os.path.exists(layout_file):
with open(layout_file, "r") as fh:
self.layout = yaml.load(fh)
self.dir_settings = self.layout.pop("__directory__", {})
self.defaults = self.dir_settings.get("defaults", {})
def install(self):
handled = set([os.path.join(self.directory, p)
for p in self.dir_settings.get("ignore", []) +
# XXX: make this tweakable via a root layout.yaml
["layout.yaml"]]
)
handled |= self.install_specials(handled)
handled |= self.install_rest(handled)
def install_rest(self, exclude):
"""
Install files in directory
"""
handled = set()
for source in listdir(self.directory):
if source in exclude:
continue
handled.add(source)
file_args = self.defaults.copy()
link_name = os.path.basename(source)
target = os.path.join(file_args.pop("target_dir", HOME), link_name)
dotfile = Dotfile(source, target, **file_args)
try:
dotfile.install()
except (FileExistsError, FileNotFoundError) as exc:
print(exc)
return handled
def install_specials(self, exclude):
"""
Install special files called out in the directory's layout.yaml
"""
handled = set()
if not self.layout:
return handled
for source_name, args in self.layout.items():
source = os.path.join(self.directory, source_name)
if source in exclude:
continue
handled.add(source)
# Use __directory__ defaults
file_args = self.defaults.copy()
# Update with file-specific attributes
file_args.update(args)
target_dir = file_args.pop("target_dir", HOME)
target = os.path.join(target_dir, file_args.pop("link_name", source_name))
dotfile = Dotfile(source, target, **file_args)
try:
dotfile.install()
except (FileExistsError, FileNotFoundError) as exc:
print(exc)
return handled
def main():
here = os.path.dirname(os.path.realpath(__file__))
for d in listdir(here):
if not os.path.isdir(d):
continue
if os.path.basename(d).startswith('.'):
continue
DotfileDirectory(d).install()
if __name__ == "__main__":
main()