1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """
23 Miscellaneous utilities.
24 """
25
26 import errno
27 import sys
28 import string
29 import traceback
30
31 from duplicity import tarfile
32
33 import duplicity.globals as globals
34 import duplicity.log as log
35
37 """
38 @return A string representation in typical Python format of the
39 currently active/raised exception.
40 """
41 type, value, tb = sys.exc_info()
42
43 lines = traceback.format_tb(tb, limit)
44 lines.extend(traceback.format_exception_only(type, value))
45
46 str = "Traceback (innermost last):\n"
47 str = str + "%-20s %s" % (string.join(lines[:-1], ""),
48 lines[-1])
49
50 return str
51
53 return "'%s'" % string.encode("string-escape")
54
56 """
57 Execute fn. If the global configuration setting ignore_errors is
58 set to True, catch errors and log them but do continue (and return
59 None).
60
61 @param fn: A callable.
62 @return Whatever fn returns when called, or None if it failed and ignore_errors is true.
63 """
64 try:
65 return fn()
66 except Exception, e:
67 if globals.ignore_errors:
68 log.Warn(_("IGNORED_ERROR: Warning: ignoring error as requested: %s: %s")
69 % (e.__class__.__name__, str(e)))
70 return None
71 else:
72 raise
73
80
89
91
92
93
94 if ti.isdir() and not ti.name.endswith("/"):
95 return ti.name + "/"
96 else:
97 return ti.name
98
100 """
101 Execute fn on filename. Ignore ENOENT errors, otherwise raise exception.
102
103 @param fn: callable
104 @param filename: string
105 """
106 try:
107 fn(filename)
108 except Exception:
109 type, value, tb = sys.exc_info()
110 if isinstance(type, OSError) and value[0] == errno.ENOENT:
111 pass
112 raise
113