-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest.py
executable file
·314 lines (263 loc) · 10.4 KB
/
manifest.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
#!/usr/bin/env python3
# vi: sts=4 ts=4 sw=4 et:
from __future__ import print_function
import argparse
import logging
import os
import sys
from os import chdir, makedirs, remove, symlink
from os.path import (basename, dirname, exists, expanduser, isdir, islink,
join, lexists, normpath)
from pprint import pprint
from shutil import rmtree
is_windows = sys.platform in ('win32', 'cygwin',)
# is_linux = sys.platform.startswith('linux')
# is_macos = sys.platform == 'darwin'
PY3 = sys.version_info >= (3, 0, 0,)
if PY3:
def iteritems(obj): return obj.items()
else:
if is_windows:
raise EnvironmentError('python 3 required on windows')
# Due to missing py2 os.symlink: but could call using ctypes
def iteritems(obj): return obj.iteritems()
STARTDIR = os.getcwd()
log = logging.getLogger(__name__)
log.setLevel(logging.WARN)
log.addHandler(logging.StreamHandler())
class IllegalSyntax(Exception):
pass
def nop(f):
"""
syscall nop function used with `--dry-run`
"""
name = f.__name__
def _nop(*a, **k):
log.debug('nop: %s %s %s' % (name, a, k))
return _nop
class Manifest(dict):
DELETE_MACRO = '@delete'
INCLUDE_MACRO = '@include'
SRC_MACROS = {DELETE_MACRO}
DEST_MACROS = {INCLUDE_MACRO}
def __init__(self, path, force):
self.force = force
if path:
with open(path, 'r') as fp:
self._parse(fp)
@staticmethod
def _parse_line_comment(line):
return not line or len(line) and line[0] == "#"
@staticmethod
def _parse_line_section_declaration(line):
has_prefix = line and len(line) and line[0] == "$"
if not has_prefix:
return None
if line[-1] in '@*:':
raise IllegalSyntax(
"section name {:s} cannot end in {:s}".format(line[:-1], line[-1]))
return line.strip("$").strip()
@staticmethod
def _parse_part_macro(part):
return part and isinstance(part, str) and part.startswith("@")
@staticmethod
def _is_macro_or_parsed(rubberducky):
return rubberducky and (
not isinstance(
rubberducky,
str) or rubberducky.startswith('@'))
@staticmethod
def _parse_part_terminal_glob(part):
return part and isinstance(part, str) and part.endswith("*")
def _parse(self, fp):
section = None
for (i, line) in enumerate(fp, 1):
line = line.strip()
if self._parse_line_comment(line):
continue
new_section = self._parse_line_section_declaration(line)
if new_section:
if new_section in self:
raise IllegalSyntax(
"line {:d}: duplicate section declaration".format(i))
section = self[new_section] = dict()
continue
elif section is None:
raise IllegalSyntax("line {:d}: target definition before "
"section declaration".format(i))
paths = line.split(":", 2)
unparsed_separators = paths[-1].find(':') > -1
if unparsed_separators:
raise IllegalSyntax("line {:d}: multiple colons".format(i))
dest, src = paths = map(lambda p: p.strip(), paths)
dest_is_macro = self._parse_part_macro(dest)
src_is_macro = self._parse_part_macro(src)
if dest_is_macro:
if dest not in self.DEST_MACROS:
raise IllegalSyntax(
"line {:d}: invalid {:}".format(
i, dest))
if dest == self.INCLUDE_MACRO:
src = set(src.split(' '))
elif src_is_macro:
if src not in self.SRC_MACROS:
raise IllegalSyntax(
"line {:d}: invalid {:}".format(
i, src))
if not (dest_is_macro or src_is_macro):
if dest in section:
raise IllegalSyntax("line {:d}: target redefinition `{:}` "
"in same section".format(i, dest))
has_glob = self._parse_part_terminal_glob(src)
assert has_glob ^ (not dest.endswith('/')), \
'line {:d}: glob dest must be directory '\
'ending with `/`'.format(i)
section[dest] = src
def install_file(self, dest, src):
"""
:assumptions: manifest has already been parsed and validated.
"""
destdir = dirname(dest)
destname = basename(dest)
if lexists(dest) or exists(dest):
if not self.force:
log.info('skipped (exists): %s' % dest)
return
if isdir(dest) and not islink(dest):
rmtree(dest)
else:
remove(dest)
if not isdir(destdir):
makedirs(destdir, 0o755)
chdir(destdir)
if src in self.SRC_MACROS:
if src == self.DELETE_MACRO:
if lexists(dest):
remove(dest)
else:
assert False
return
assert exists(src), \
"Manifest src `{:}` does not exist on the filesystem".format(src)
try:
# XXX: Following line's kwarg is useless. [ypcrts // 20180120]
# symlink(src, destname, target_is_directory=isdir(src))
# XXX: No kwargs in py3. Should be unnecessary per docs:
# "If the target is present, the type of the symlink will be created to match."
# - https://docs.python.org/3.6/library/os.html
symlink(src, destname)
log.warning('linked %s' % dest)
except OSError as e:
log.error('failure - %s :: %s' % (e, dest))
def iter_section(self, section_name, included=None):
# recursive base case
if not included:
included = set()
included.add(section_name)
for (dest, src) in iteritems(self[section_name]):
if not self._is_macro_or_parsed(src):
src = normpath(join(STARTDIR, expanduser(src)))
if not self._is_macro_or_parsed(dest):
dest = normpath(expanduser(dest))
else:
if dest == self.INCLUDE_MACRO:
included.update(src)
for sn in src:
assert sn != section_name,\
"cannot include `{:}` inside itself"\
"!".format(section_name)
assert sn in self, \
"cannot include `{:}` "\
"dependency `{:}` does not exist".format(section_name, sn)
for s in self.iter_section(sn, included):
yield s
continue
else:
assert False
dest = normpath(dest)
has_glob = self._parse_part_terminal_glob(src)
if not has_glob:
yield dest, src
continue
src = src.strip('*')
names = os.listdir(src)
for name in names:
yield (join(dest, name), join(src, name),)
class Actions:
def __init__(self, manifest, args):
self.manifest = manifest
self.args = args
self._assert_symlink_works()
def install(self):
for section in self.args.section:
for (dest, src) in self.manifest.iter_section(section):
self.manifest.install_file(dest, src)
def purge(self):
for section in self.args.section:
for (dest, src) in self.manifest.iter_section(section):
if lexists(dest) or exists(dest):
log.warning('purged %s' % dest)
remove(dest)
def inspect(self):
print('inspecting...')
pprint(dict(
(key, self.manifest[key].get('@include', None),)
for key in self.manifest.keys()
))
def _assert_symlink_works(self):
if self.args.no_preflight:
return True
nonce = 'symlinknonce-lkjho8ho98hp923h4iouh90sa0d98u9d8j1p92d8'
target = nonce + '.target'
try:
symlink(nonce, target)
except OSError:
raise
finally:
if lexists(target):
remove(target)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='creates symlinks described by a manifest')
parser.add_argument('action',
choices=('install', 'purge', 'inspect'),
nargs='?', type=str, default='inspect')
parser.add_argument('-n', '--dry-run', action='store_true',
help='nop out all syscalls, verbose')
parser.add_argument('-m', '--manifest', type=str,
help='path to custom manifest file',
default='./MANIFEST')
parser.add_argument('-f', '--force', action='store_true',
help='allow clobbering files in target paths')
parser.add_argument('-v', '--verbose', default=0, action='count')
parser.add_argument('-d', '--dir', type=str, default=None,
help='override HOME and USERPROFILE (tilde expansion)')
parser.add_argument( '--no-preflight',
help='skip the preflight sanity checks',
action='store_true',
dest='no_preflight')
parser.add_argument('section',
help='manifest target', type=str, nargs='*')
args = parser.parse_args()
if args.dry_run:
args.verbose = 3
log.warn('seting up dry run')
rmtree = nop(rmtree)
remove = nop(remove)
makedirs = nop(makedirs)
symlink = nop(symlink)
chdir = nop(chdir)
if args.dir:
os.environ['HOME'] = os.environ['USERPROFILE'] = args.dir
if args.verbose >= 2:
log.setLevel(logging.DEBUG)
elif args.verbose >= 1:
log.setLevel(logging.INFO)
m = Manifest(force=args.force, path=args.manifest)
if not args.section:
args.section = ('default',)
else:
args.section = list(map(lambda sn: sn.rstrip('/'), args.section))
for sn in args.section:
assert sn in m, "section `{:s}` is not in the manifest".format(sn)
getattr(Actions(m, args), args.action)()