aboutsummaryrefslogtreecommitdiffstats
path: root/confman.py
blob: 017a70c423cb7e846e81bb3842cd3441b9374d4b (plain)
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
from __future__ import with_statement

import os
import os.path as osp
import re
from string import Template


class ConfmanException(Exception):
    pass


class ActionException(ConfmanException):
    def __init__(self, action, value, resolve=None):
        self.action = action
        self.resolve = resolve
        ConfmanException.__init__(self, value)

    def __str__(self):
        s = "%s (%s)" % (ConfmanException.__str__(self), repr(self.action))
        if self.resolve:
            s += "\nResolve the issue with:\n%s" % self.resolve
        return s


class Action(object):
    @classmethod
    def matches(cls, filename):
        """
        Tells if the file should be associated with this action.
        Returns False if not; confman will try with the next class.
        Returns None if the file should be ignored.
        Returns the destination filename (str) if it matches.
        """
        raise NotImplementedError()

    def __init__(self, config, relpath, source, dest):
        self.config = config
        self.relpath = relpath
        self.source = source
        self.dest = dest

    def __repr__(self):
        return "%s: %s => %s" % (self.__class__.__name__,
                                 osp.join(self.relpath, self.source),
                                 self.dest)

    def check(self):
        raise NotImplementedError()

    def sync(self):
        raise NotImplementedError()

    def dest_path(self):
        return osp.normpath(
            osp.join(self.config.dest, self.relpath, self.dest))

    def source_path(self):
        return osp.normpath(
            osp.join(self.config.source, self.relpath, self.source))

    def _makedirs(self):
        dir = osp.dirname(self.dest_path())
        if not osp.isdir(dir):
            os.makedirs(dir)


class SymlinkAction(Action):
    FORCE_SAME = True
    "Force replace destination file by symlink if source has same contents"

    @classmethod
    def matches(cls, filename):
        return filename

    def same_contents(self):
        source = self.source_path()
        dest = self.dest_path()
        with open(source) as s, open(dest) as d:
            return s.read() == d.read()

    def check(self):
        source = self.source_path()
        if not osp.exists(source):
            raise ActionException(self, "Source does not exists")

        dest = self.dest_path()
        if osp.lexists(dest):
            if not osp.islink(dest) and not (self.FORCE_SAME and self.same_contents()):
                resolve = "diff %s %s\nrm -vi %s" % (osp.abspath(source), osp.abspath(dest), osp.abspath(dest))
                raise ActionException(self,
                                      "Destination exists and is not a link",
                                      resolve=resolve)

    def sync(self):
        source = self.source_path()
        dest = self.dest_path()
        if osp.lexists(dest):
            # if the link already exists
            if osp.islink(dest):
                # if the old link is broken or incorrect
                if not osp.exists(dest) or not osp.samefile(dest, source):
                    os.unlink(dest)
                    print "Link target altered"
                else:
                    return
            # if the destination is not a link, but has same contents as source
            elif self.FORCE_SAME and self.same_contents():
                os.unlink(dest)
                print "Link target was a file with same contents"
        else:
            self._makedirs()

        relsource = osp.normpath(osp.relpath(source,
                                             osp.join(self.config.dest, self.relpath)))
        os.symlink(relsource, dest)
        print "Created new link: %s => %s" % (dest, source)


class TextAction(Action):
    ONCE = False

    def check(self):
        """
        This action can't be invoked by a file;
        the source parameter is used to provide the text.
        It is used by a ProgrammableAction.
        """
        if self.source is not None:
            self.text = self.source
            self.source = None

    def sync(self):
        """
        Write the file only if necessary
        """
        dest = self.dest_path()
        exists = osp.exists(dest)
        if osp.islink(dest):
            raise ActionException(self, "Destination is a link")
        else:
            self._makedirs()
            with open(dest, "a+") as destfile:
                if destfile.read() != self.text:
                    if exists and self.ONCE:
                        print "File already exists, not updated: %s" % dest
                    else:
                        print "Updated file contents: %s" % dest
                        destfile.truncate(0)
                        destfile.write(self.text)

    def __repr__(self):
        return "%s: TEXT => %s" % (self.__class__.__name__, self.dest)


class CopyAction(TextAction):
    MATCHED = re.compile(r"\.copy$")

    @classmethod
    def matches(cls, filename):
        if cls.MATCHED.search(filename):
            return cls.MATCHED.sub("", filename)
        return False

    def check(self):
        """
        Retrieve the text from the source file.
        """
        source = self.source_path()
        with open(source, "r") as sourcefile:
            self.text = sourcefile.read()


class CopyOnceAction(CopyAction):
    ONCE = True
    MATCHED = re.compile(r"\.copyonce$")

    @classmethod
    def matches(cls, filename):
        if cls.MATCHED.search(filename):
            return cls.MATCHED.sub("", filename)
        return False


class EmptyAction(CopyOnceAction):
    """
    Ensures the destination file exists.
    Creates an empty one if not.
    """
    MATCHED = re.compile(r"\.empty$")

    @classmethod
    def matches(cls, filename):
        if cls.MATCHED.search(filename):
            return cls.MATCHED.sub("", filename)
        return False

    def check(self):
        self.text = ""

    def __repr__(self):
        return "%s: EMPTY => %s" % (self.__class__.__name__, self.dest)


class Forwarder(ConfmanException):
    """
    Not really an error, it is used to go back to confman with a value
    when executing a ProgrammableAction.
    """
    def get_proxy(self, parent):
        raise NotImplementedError()


class SymlinkForwarder(Forwarder):
    def __init__(self, filename):
        self.filename = filename

    def get_proxy(self, parent):
        return SymlinkAction(parent.config, parent.relpath,
                             self.filename, parent.dest)


class EmptyForwarder(Forwarder):
    def get_proxy(self, parent):
        return EmptyAction(parent.config, parent.relpath,
                           None, parent.dest)


class TextForwarder(Forwarder):
    def __init__(self, text):
        self.text = text

    def get_proxy(self, parent):
        # The text is passed as source
        return TextAction(parent.config, parent.relpath,
                          self.text, parent.dest)


class IgnoreForwarder(Forwarder):
    def get_proxy(self, parent):
        return None


class ProgrammableAction(Action):
    MATCHED = re.compile(r"\.p\.py$")

    @classmethod
    def matches(cls, filename):
        if cls.MATCHED.search(filename):
            return cls.MATCHED.sub("", filename)
        return False

    def get_env(self):
        """
        Get limited environment execution.
        This function could be overloaded to add some custom methods.
        """
        options = self.config.options  # NOQA

        def redirect(filename):
            raise SymlinkForwarder("_%s" % filename)

        def empty():
            raise EmptyForwarder()

        def ignore():
            raise IgnoreForwarder()

        class ConfmanTemplate(Template):
            def render(self, _strict=True, _warning=True, **kwargs):
                if _warning and "warning" not in kwargs:
                    kwargs["warning"] = "WARNING: Do not edit this file, edit the template instead."
                if _strict:
                    raise TextForwarder(self.substitute(kwargs))
                raise TextForwarder(self.safe_substitute(kwargs))

        def template(name):
            source = osp.join(osp.dirname(self.source_path()), "_%s" % name)
            with open(source) as handle:
                text = handle.read()
            return ConfmanTemplate(text)

        def text(text):
            return ConfmanTemplate(text)

        return locals()

    def check(self):
        source = self.source_path()
        try:
            with open(source, "r") as file:
                exec_env = self.get_env()
                exec compile(file.read(), source, "exec") in exec_env
        except Forwarder, e:
            self.proxy = e.get_proxy(self)
        else:
            raise ActionException(self, "Unknown result")

        if self.proxy is not None:
            return self.proxy.check()

    def sync(self):
        if self.proxy is not None:
            return self.proxy.sync()

    def __repr__(self):
        return "%s: %s => PROXY %s" % (self.__class__.__name__,
                                       self.source, repr(self.proxy))


class IgnoreAction(Action):
    IGNORED = re.compile(r"_|\.git$|\.gitignore$")

    @classmethod
    def matches(cls, filename):
        if cls.IGNORED.match(filename):
            return None
        return False

    def __repr__(self):
        return "%s: %s => IGNORED" % (self.__class__.__name__, self.source)


class ConfigSource(object):
    DEFAULT_CLASSES = (
        ProgrammableAction,
        IgnoreAction,
        EmptyAction,
        CopyAction,
        CopyOnceAction,
        SymlinkAction,
    )
    ACT_AS_FILE = re.compile(r"\.F$")

    def __init__(self, source, dest, classes=None, options=None):
        # handle '~'
        self.source = osp.expanduser(source)
        self.dest = osp.expanduser(dest)

        self.classes = classes or self.DEFAULT_CLASSES
        self.options = options

    def sync(self):
        """
        Gather files and synchronize them.
        """
        print "Synchronizing files from %s to %s..." % (self.source, self.dest)
        self.analyze()
        self.execute()

    def analyze(self):
        """
        Gather all files.
        """
        self.tree = {}
        for path, dirs, files in os.walk(self.source, topdown=True):
            relpath = osp.relpath(path, self.source)

            to_remove = []
            for filename in dirs:
                if self.ACT_AS_FILE.search(filename):
                    to_remove.append(filename)
                    self.add_dir(relpath, filename)
            for filename in to_remove:
                # this list can be modified in place
                # but we wust not remove elements when iterating on it!
                dirs.remove(filename)

            for filename in files:
                self.add(relpath, filename)

    def _get_file_class(self, filename):
        """
        Returns the first class that accepts (matches) the file.
        It will fail if no class accepts or tells to ignore.
        """
        for cls in self.classes:
            dest = cls.matches(filename)
            if dest is not False:
                return (cls, dest)
        raise ConfmanException("No class found for %s" % filename)

    def add(self, relpath, filename):
        """
        Add a file if it can be associated to an action.
        filename is the source filename.
        The destination will be deduced, and it will check for conflicts.
        """
        cls, dest = self._get_file_class(filename)
        if dest is not None:
            return self._add(relpath, filename, cls, dest)

    def add_dir(self, relpath, filename):
        """
        Add a directory as if it was a file.
        It will try to associate it with a particular file class,
        though it does not make much sense in most cases.
        filename is the source filename.
        The destination will be deduced, and it will check for conflicts.
        """
        cls, dest = self._get_file_class(filename)
        if dest is not None:
            dest = self.ACT_AS_FILE.sub("", dest)
            return self._add(relpath, filename, cls, dest)

    def _add(self, relpath, filename, cls, dest):
        files = self.tree.setdefault(relpath, {})
        if dest in files:
            raise ConfmanException("Conflict: %s with %s" % (filename, files[dest]))
        files[dest] = cls(self, relpath, filename, dest)

    def execute(self):
        """
        Executes all actions if everything is alright.
        """
        for f in self:
            f.check()
        for f in self:
            f.sync()

    def __iter__(self):
        """
        Iterates over all analyzed files.
        """
        for files in self.tree.itervalues():
            for f in files.itervalues():
                yield f

    def __repr__(self):
        return "\n".join(
            ("%s: %s" % (action.relpath, repr(action)) for action in self))