Package duplicity :: Module util
[hide private]
[frames] | no frames]

Source Code for Module duplicity.util

  1  # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- 
  2  # 
  3  # Copyright 2002 Ben Escoto <ben@emerose.org> 
  4  # Copyright 2007 Kenneth Loafman <kenneth@loafman.com> 
  5  # 
  6  # This file is part of duplicity. 
  7  # 
  8  # Duplicity is free software; you can redistribute it and/or modify it 
  9  # under the terms of the GNU General Public License as published by the 
 10  # Free Software Foundation; either version 2 of the License, or (at your 
 11  # option) any later version. 
 12  # 
 13  # Duplicity is distributed in the hope that it will be useful, but 
 14  # WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 16  # General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with duplicity; if not, write to the Free Software Foundation, 
 20  # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 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   
36 -def exception_traceback(limit = 50):
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
52 -def escape(string):
53 return "'%s'" % string.encode("string-escape")
54
55 -def maybe_ignore_errors(fn):
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
74 -class FakeTarFile:
75 debug = 0
76 - def __iter__(self):
77 return iter([])
78 - def close(self):
79 pass
80
81 -def make_tarfile(mode, fp):
82 # We often use 'empty' tarfiles for signatures that haven't been filled out 83 # yet. So we want to ignore ReadError exceptions, which are used to signal 84 # this. 85 try: 86 return tarfile.TarFile("arbitrary", mode, fp) 87 except tarfile.ReadError: 88 return FakeTarFile()
89
90 -def get_tarinfo_name(ti):
91 # Python versions before 2.6 ensure that directories end with /, but 2.6 92 # and later ensure they they *don't* have /. ::shrug:: Internally, we 93 # continue to use pre-2.6 method. 94 if ti.isdir() and not ti.name.endswith("/"): 95 return ti.name + "/" 96 else: 97 return ti.name
98
99 -def ignore_missing(fn, filename):
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() #@UnusedVariable 110 if isinstance(type, OSError) and value[0] == errno.ENOENT: 111 pass 112 raise
113