summaryrefslogtreecommitdiff
path: root/paste/debug/fsdiff.py
blob: f680bf685f564d7f522e18c437cb8dd24b5e8ed7 (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
# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
"""
Module to find differences over time in a filesystem

Basically this takes a snapshot of a directory, then sees what changes
were made.  The contents of the files are not checked, so you can
detect that the content was changed, but not what the old version of
the file was.
"""

import os
from fnmatch import fnmatch
from datetime import datetime

try:
    # Python 3
    import collections.UserDict as IterableUserDict
except ImportError:
    try:
        # Python 2.5-2.7
        from UserDict import IterableUserDict
    except ImportError:
        # Python <= 2.4
        from paste.util.UserDict24 import IterableUserDict
import operator
import re

__all__ = ['Diff', 'Snapshot', 'File', 'Dir', 'report_expected_diffs',
           'show_diff']

class Diff(object):

    """
    Represents the difference between two snapshots
    """

    def __init__(self, before, after):
        self.before = before
        self.after = after
        self._calculate()

    def _calculate(self):
        before = self.before.data
        after = self.after.data
        self.deleted = {}
        self.updated = {}
        self.created = after.copy()
        for path, f in before.items():
            if path not in after:
                self.deleted[path] = f
                continue
            del self.created[path]
            if f.mtime < after[path].mtime:
                self.updated[path] = after[path]

    def __str__(self):
        return self.report()

    def report(self, header=True, dates=False):
        s = []
        if header:
            s.append('Difference in %s from %s to %s:' %
                     (self.before.base_path,
                      self.before.calculated,
                      self.after.calculated))
        for name, files, show_size in [
            ('created', self.created, True),
            ('deleted', self.deleted, True),
            ('updated', self.updated, True)]:
            if files:
                s.append('-- %s: -------------------' % name)
                files = files.items()
                files.sort()
                last = ''
                for path, f in files:
                    t = '  %s' % _space_prefix(last, path, indent=4,
                                               include_sep=False)
                    last = path
                    if show_size and f.size != 'N/A':
                        t += '  (%s bytes)' % f.size
                    if dates:
                        parts = []
                        if self.before.get(path):
                            parts.append(self.before[path].mtime)
                        if self.after.get(path):
                            parts.append(self.after[path].mtime)
                        t += ' (mtime: %s)' % ('->'.join(map(repr, parts)))
                    s.append(t)
        if len(s) == 1:
            s.append('  (no changes)')
        return '\n'.join(s)

class Snapshot(IterableUserDict):

    """
    Represents a snapshot of a set of files.  Has a dictionary-like
    interface, keyed relative to ``base_path``
    """

    def __init__(self, base_path, files=None, ignore_wildcards=(),
                 ignore_paths=(), ignore_hidden=True):
        self.base_path = base_path
        self.ignore_wildcards = ignore_wildcards
        self.ignore_hidden = ignore_hidden
        self.ignore_paths = ignore_paths
        self.calculated = None
        self.data = files or {}
        if files is None:
            self.find_files()

    ############################################################
    ## File finding
    ############################################################

    def find_files(self):
        """
        Find all the files under the base path, and put them in
        ``self.data``
        """
        self._find_traverse('', self.data)
        self.calculated = datetime.now()

    def _ignore_file(self, fn):
        if fn in self.ignore_paths:
            return True
        if self.ignore_hidden and os.path.basename(fn).startswith('.'):
            return True
        for pat in self.ignore_wildcards:
            if fnmatch(fn, pat):
                return True
        return False

    def _ignore_file(self, fn):
        if fn in self.ignore_paths:
            return True
        if self.ignore_hidden and os.path.basename(fn).startswith('.'):
            return True
        return False

    def _find_traverse(self, path, result):
        full = os.path.join(self.base_path, path)
        if os.path.isdir(full):
            if path:
                # Don't actually include the base path
                result[path] = Dir(self.base_path, path)
            for fn in os.listdir(full):
                fn = os.path.join(path, fn)
                if self._ignore_file(fn):
                    continue
                self._find_traverse(fn, result)
        else:
            result[path] = File(self.base_path, path)

    def __repr__(self):
        return '<%s in %r from %r>' % (
            self.__class__.__name__, self.base_path,
            self.calculated or '(no calculation done)')

    def compare_expected(self, expected, comparison=operator.eq,
                         differ=None, not_found=None,
                         include_success=False):
        """
        Compares a dictionary of ``path: content`` to the
        found files.  Comparison is done by equality, or the
        ``comparison(actual_content, expected_content)`` function given.

        Returns dictionary of differences, keyed by path.  Each
        difference is either noted, or the output of
        ``differ(actual_content, expected_content)`` is given.

        If a file does not exist and ``not_found`` is given, then
        ``not_found(path)`` is put in.
        """
        result = {}
        for path in expected:
            orig_path = path
            path = path.strip('/')
            if path not in self.data:
                if not_found:
                    msg = not_found(path)
                else:
                    msg = 'not found'
                result[path] = msg
                continue
            expected_content = expected[orig_path]
            file = self.data[path]
            actual_content = file.bytes
            if not comparison(actual_content, expected_content):
                if differ:
                    msg = differ(actual_content, expected_content)
                else:
                    if len(actual_content) < len(expected_content):
                        msg = 'differ (%i bytes smaller)' % (
                            len(expected_content) - len(actual_content))
                    elif len(actual_content) > len(expected_content):
                        msg = 'differ (%i bytes larger)' % (
                            len(actual_content) - len(expected_content))
                    else:
                        msg = 'diff (same size)'
                result[path] = msg
            elif include_success:
                result[path] = 'same!'
        return result

    def diff_to_now(self):
        return Diff(self, self.clone())

    def clone(self):
        return self.__class__(base_path=self.base_path,
                              ignore_wildcards=self.ignore_wildcards,
                              ignore_paths=self.ignore_paths,
                              ignore_hidden=self.ignore_hidden)

class File(object):

    """
    Represents a single file found as the result of a command.

    Has attributes:

    ``path``:
        The path of the file, relative to the ``base_path``

    ``full``:
        The full path

    ``stat``:
        The results of ``os.stat``.  Also ``mtime`` and ``size``
        contain the ``.st_mtime`` and ``st_size`` of the stat.

    ``bytes``:
        The contents of the file.

    You may use the ``in`` operator with these objects (tested against
    the contents of the file), and the ``.mustcontain()`` method.
    """

    file = True
    dir = False

    def __init__(self, base_path, path):
        self.base_path = base_path
        self.path = path
        self.full = os.path.join(base_path, path)
        self.stat = os.stat(self.full)
        self.mtime = self.stat.st_mtime
        self.size = self.stat.st_size
        self._bytes = None

    def bytes__get(self):
        if self._bytes is None:
            f = open(self.full, 'rb')
            self._bytes = f.read()
            f.close()
        return self._bytes
    bytes = property(bytes__get)

    def __contains__(self, s):
        return s in self.bytes

    def mustcontain(self, s):
        __tracebackhide__ = True
        bytes = self.bytes
        if s not in bytes:
            print 'Could not find %r in:' % s
            print bytes
            assert s in bytes

    def __repr__(self):
        return '<%s %s:%s>' % (
            self.__class__.__name__,
            self.base_path, self.path)

class Dir(File):

    """
    Represents a directory created by a command.
    """

    file = False
    dir = True

    def __init__(self, base_path, path):
        self.base_path = base_path
        self.path = path
        self.full = os.path.join(base_path, path)
        self.size = 'N/A'
        self.mtime = 'N/A'

    def __repr__(self):
        return '<%s %s:%s>' % (
            self.__class__.__name__,
            self.base_path, self.path)

    def bytes__get(self):
        raise NotImplementedError(
            "Directory %r doesn't have content" % self)

    bytes = property(bytes__get)
    

def _space_prefix(pref, full, sep=None, indent=None, include_sep=True):
    """
    Anything shared by pref and full will be replaced with spaces
    in full, and full returned.

    Example::

        >>> _space_prefix('/foo/bar', '/foo')
        '    /bar'
    """
    if sep is None:
        sep = os.path.sep
    pref = pref.split(sep)
    full = full.split(sep)
    padding = []
    while pref and full and pref[0] == full[0]:
        if indent is None:
            padding.append(' ' * (len(full[0]) + len(sep)))
        else:
            padding.append(' ' * indent)
        full.pop(0)
        pref.pop(0)
    if padding:
        if include_sep:
            return ''.join(padding) + sep + sep.join(full)
        else:
            return ''.join(padding) + sep.join(full)
    else:
        return sep.join(full)

def report_expected_diffs(diffs, colorize=False):
    """
    Takes the output of compare_expected, and returns a string
    description of the differences.
    """
    if not diffs:
        return 'No differences'
    diffs = diffs.items()
    diffs.sort()
    s = []
    last = ''
    for path, desc in diffs:
        t = _space_prefix(last, path, indent=4, include_sep=False)
        if colorize:
            t = color_line(t, 11)
        last = path
        if len(desc.splitlines()) > 1:
            cur_indent = len(re.search(r'^[ ]*', t).group(0))
            desc = indent(cur_indent+2, desc)
            if colorize:
                t += '\n'
                for line in desc.splitlines():
                    if line.strip().startswith('+'):
                        line = color_line(line, 10)
                    elif line.strip().startswith('-'):
                        line = color_line(line, 9)
                    else:
                        line = color_line(line, 14)
                    t += line+'\n'
            else:
                t += '\n' + desc
        else:
            t += ' '+desc
        s.append(t)
    s.append('Files with differences: %s' % len(diffs))
    return '\n'.join(s)

def color_code(foreground=None, background=None):
    """
    0  black
    1  red
    2  green
    3  yellow
    4  blue
    5  magenta (purple)
    6  cyan
    7  white (gray)

    Add 8 to get high-intensity
    """
    if foreground is None and background is None:
        # Reset
        return '\x1b[0m'
    codes = []
    if foreground is None:
        codes.append('[39m')
    elif foreground > 7:
        codes.append('[1m')
        codes.append('[%im' % (22+foreground))
    else:
        codes.append('[%im' % (30+foreground))
    if background is None:
        codes.append('[49m')
    else:
        codes.append('[%im' % (40+background))
    return '\x1b' + '\x1b'.join(codes)

def color_line(line, foreground=None, background=None):
    match = re.search(r'^(\s*)', line)
    return (match.group(1) + color_code(foreground, background)
            + line[match.end():] + color_code())

def indent(indent, text):
    return '\n'.join(
        [' '*indent + l for l in text.splitlines()])

def show_diff(actual_content, expected_content):
    actual_lines = [l.strip() for l in actual_content.splitlines()
                    if l.strip()]
    expected_lines = [l.strip() for l in expected_content.splitlines()
                      if l.strip()]
    if len(actual_lines) == len(expected_lines) == 1:
        return '%r not %r' % (actual_lines[0], expected_lines[0])
    if not actual_lines:
        return 'Empty; should have:\n'+expected_content
    import difflib
    return '\n'.join(difflib.ndiff(actual_lines, expected_lines))