Package conary :: Module callbacks
[hide private]
[frames] | no frames]

Source Code for Module conary.callbacks

  1  # Copyright (c) 2005-2008 rPath, Inc. 
  2  # 
  3  # This program is distributed under the terms of the Common Public License, 
  4  # version 1.0. A copy of this license should have been distributed with this 
  5  # source file in a file called LICENSE. If it is not present, the license 
  6  # is always available at http://www.rpath.com/permanent/licenses/CPL-1.0. 
  7  # 
  8  # This program is distributed in the hope that it will be useful, but 
  9  # without any warranty; without even the implied warranty of merchantability 
 10  # or fitness for a particular purpose. See the Common Public License for 
 11  # full details. 
 12  # 
 13   
 14  import sys 
 15  import traceback 
 16  import types 
 17   
 18  from conary import errors 
 19  from conary.lib import log, api 
 20   
21 -def passExceptions(f):
22 f._passExceptions = True 23 return f
24
25 -def exceptionProtection(method, exceptionCallback):
26 def wrapper(*args, **kwargs): 27 if hasattr(method, '_passExceptions') and method._passExceptions: 28 return method(*args, **kwargs) 29 30 try: 31 return method(*args, **kwargs) 32 except Exception, e: 33 exc_info = sys.exc_info() 34 if errors.exceptionIsUncatchable(e): 35 raise 36 exceptionCallback(exc_info)
37 38 return wrapper 39
40 -class Callback(object):
41
42 - def _exceptionOccured(self, exc_info):
43 etype, e, tb = exc_info 44 # format the exception 45 msg = '%s' % etype.__name__ 46 s = str(e) 47 if s: 48 msg += ': %s' % s 49 # get the line info that raised the exception 50 inner = tb.tb_next 51 while inner.tb_next: 52 inner = inner.tb_next 53 filename = inner.tb_frame.f_code.co_filename 54 linenum = inner.tb_frame.f_lineno 55 log.warning('Unhandled exception occurred when invoking callback:\n' 56 '%s:%s\n' 57 ' %s', filename, linenum, msg) 58 # log the full traceback if debugging (--debug=all) 59 log.debug(''.join(traceback.format_exception(*exc_info))) 60 if not hasattr(self, 'exceptions'): 61 self.exceptions = [] 62 self.exceptions.append(e)
63
64 - def __getattribute__(self, name):
65 item = object.__getattribute__(self, name) 66 if name[0] == '_': 67 return item 68 elif not isinstance(item, types.MethodType): 69 return item 70 71 return exceptionProtection(item, self._exceptionOccured)
72
73 - def cancelOperation(self):
74 """Return True if we should cancel the operation as soon as it is 75 safely possible""" 76 if not hasattr(self, 'exceptions'): 77 return False 78 for exc in self.exceptions: 79 if hasattr(exc, 'cancelOperation'): 80 return exc.cancelOperation 81 return False
82
83 - def __init__(self):
84 self.exceptions = []
85
86 -class ChangesetCallback(Callback):
87
88 - def preparingChangeSet(self):
89 """ 90 Called before an update begins and before it looks for the requested 91 troves. 92 93 @return: None 94 """ 95 pass
96
97 - def requestingChangeSet(self):
98 """ 99 Called right before requesting a changeset from a repository. 100 @return: None 101 """ 102 pass
103
104 - def sendingChangeset(self, sent, total):
105 pass
106
107 - def setRate(self, rate):
108 self.rate = rate
109
110 - def downloadingChangeSet(self, got, need):
111 """ 112 Called when downloading a changeset. 113 @param got: number of bytes received so far. 114 @type got: integer 115 @param need: number of bytes total to be retrieved. 116 @type need: integer 117 @return: None 118 """ 119 pass
120
121 - def requestingFileContents(self):
122 """ 123 Called right before requesting file contents from a repository. 124 @return: None 125 """ 126 pass
127
128 - def downloadingFileContents(self, got, need):
129 """ 130 Called when downloading file contents. 131 @param got: number of bytes received so far. 132 @type got: integer 133 @param need: number of bytes total to be retrieved 134 @type need: integer 135 @return: None 136 """ 137 pass
138
139 - def setChangesetHunk(self, hunk, hunkCount):
140 """ 141 Called when creating changesets, such as when downloading changesets. 142 @param hunk: the number of the changeset being created (starts at 1) 143 @type hunk: integer 144 @param hunkCount: total number of changesets to be created. 145 @type hunkCount: integer 146 @return: None 147 """ 148 pass
149
150 - def checkAbort(self):
151 pass
152
153 - def done(self):
154 pass
155
156 - def error(self, msg, *args, **kwargs):
157 """Error handling callback 158 159 @param msg: A message to display 160 @type msg: str 161 @keyword exc_text: Traceback text that should be printed verbatim 162 @type exc_text: str 163 """ 164 exc_text = kwargs.pop('exc_text', None) 165 # Append the traceback to the message 166 if exc_text: 167 msg += "\n%s" 168 args += (exc_text, ) 169 return log.error(msg, *args, **kwargs)
170
171 - def warning(self, msg, *args, **kwargs):
172 """Warning handling callback 173 174 @param msg: A message to display 175 @type msg: str 176 @keyword exc_text: Traceback text that should be printed verbatim 177 @type exc_text: str 178 """ 179 exc_text = kwargs.pop('exc_text', None) 180 # Append the traceback to the message 181 if exc_text: 182 msg += "\n%s" 183 args += (exc_text, ) 184 return log.warning(msg, *args, **kwargs)
185
186 - def missingFiles(self, missingFiles):
187 """This callback gets called if missing files were detected in the 188 upstream server 189 @param missingFiles: a list of tuples: 190 (troveName, troveVersion, troveFlavor, pathId, path, fileId, version) 191 """ 192 return False
193
194 - def __init__(self):