def ping(self, seconds=5, hook=None, sleep=0.1): """ Check for availability of server. @param seconds: seconds to wait for ping to succeed @type seconds: float (default 5) @param hook: if not None, a function that is called after every ping failure. @type hook: function that takes no arguments @param sleep: seconds to sleep between each ping attempt. @type sleep: float (default 5) @return: True if ping succeeds (otherwise raises exception). @raise: errors.OpenError if ping fails """ timeSlept = 0 while timeSlept < seconds: try: if not self._client.isConnected(): self._client.connect() if not self._client.getSessionId(): self._client.poll() # Raise something in case we don't get a sessionID # by the deadline -- otherwise the re-raise fails. raise errors.OpenError("Couldn't get a session ID") else: return True except: if hook: hook() time.sleep(sleep) timeSlept += sleep raise
def _request(self, method, params): """ API-aware request processing """ if method not in self._methods: raise ApiError, 'cannot find method %s in api' % method methodApi = self._methods[method] methodVersion = methodApi.version frozenParams = list(_freezeParams(methodApi, params, methodVersion)) callData = dict(apiMajorVersion=self._apiMajorVersion, apiMinorVersion=self._apiMinorVersion, methodVersion=methodVersion) args = (callData, ) + tuple(frozenParams) try: passed, rv = self._marshal_call(method, args) except socket.error, err: if len(err.args) == 1: # M2Crypto likes to raise weird socket.error instances msg = err.args[0] else: msg = err.args[1] raise errors.OpenError("Error communicating to server at %s: %s" % (self._address, msg))
def _copyFiles(self): for (sourceFile, targetFile, mode) in self.filesToCopy: log.debug("copying file %s into chroot:%s", sourceFile, targetFile) try: target = self.root + targetFile target = os.path.realpath(target) util.mkdirChain(os.path.dirname(target)) shutil.copy(sourceFile, target) if mode is not None: os.chmod(target, mode) except (IOError, OSError), e: raise errors.OpenError('Could not copy in file %s to %s: %s' % (sourceFile, targetFile, e))
def testFreezeThawException(self): @api(version=1) @api_parameters(1, None) @api_return(1, None) def testFunc(self, foo): return 0 sys.exc_clear() frz = apirpc._freezeException(repoerrors.OpenError('foo')) self.assertEquals( str(apiutils.thaw(*frz)), 'Exception from server:\n' 'conary.repository.errors.OpenError: foo\n' 'None\n') # this is rmake's internal error frz = apirpc._freezeException(errors.OpenError('foo')) self.assertEquals(str(apiutils.thaw(*frz)), 'foo')
def _copyDirs(self): for (sourceDir, targetDir) in self.dirsToCopy: targetDir = self.root + targetDir if os.path.exists(targetDir): if os.path.islink(targetDir): os.unlink(targetDir) else: util.rmtree(targetDir) util.mkdirChain(os.path.realpath(os.path.dirname(targetDir))) log.debug("copying dir %s into chroot:%s", sourceDir, targetDir) try: shutil.copytree(sourceDir, os.path.realpath(targetDir)) except shutil.Error, e: errorList = '\n'.join('cannot copy %s to %s: %s' % x for x in e.args[0]) raise errors.OpenError('Could not copy in directory %s:\n%s' % (sourceDir, errorList))
def testExceptions(self): def genRaiseExceptionFn(exception): def fn(*args, **kw): raise exception return fn def _testException(exception, debugAll=False): self.mock(main.RbuildMain, 'getCommand', genRaiseExceptionFn(exception)) if debugAll: return self.captureOutput(main.main, ['rbuild', 'help', '--debug-all']) return self.captureOutput(main.main, ['rbuild', 'help']) # if rMake isn't running, we get rmake.errors.OpenError self.logFilter.add() rc, _ = _testException( rmakeerrors.OpenError( 'Error communicating to server at unix:///var/lib/rmake/socket: Connection refused' )) self.assertEquals(self.logFilter.records, [ 'error: Error communicating to server at unix:///var/lib/rmake/socket: Connection refused\n\nCould not contact the rMake server. Perhaps the rMake service is not\nrunning. To start the rMake service, as root, try running the command:\nservice rmake restart' ]) self.logFilter.clear() # other rmake errors are displayed verbatim, as they are designed for self.logFilter.add() rc, _ = _testException(rmakeerrors.RmakeError('Dazed and Confused')) self.assertEquals(self.logFilter.records, ['error: Dazed and Confused']) self.logFilter.clear() # robj errors related to authorization and authentication self.logFilter.add() rc, _ = _testException(errors.UnauthorizedActionError('act')) self.assertEquals(self.logFilter.records, ['error: You are not authorized to act']) self.logFilter.clear() # pipe errors generally mean EOF when writing to less, e.g. rc, _ = _testException(IOError(errno.EPIPE, 'Pipe Error')) self.assertEquals(rc, 0) self.assertRaises(IOError, _testException, IOError('Other IO Error')) self.assertRaises(RuntimeError, _testException, RuntimeError('Other IO Error')) self.logFilter.add() rc, _ = _testException(errors.RbuildError('foo')) self.assertEquals(self.logFilter.records, ['error: foo']) self.assertEquals(rc, 1) self.logFilter.remove() # test with --debug-all rc, _ = _testException(errors.RbuildError('foo')) self.assertEquals(rc, 1) self.assertRaises(errors.RbuildError, _testException, errors.RbuildError('foo'), debugAll=True) self.mock(main.RbuildMain, 'main', lambda *args, **kw: None) assert (main.main(['rbuild', 'help']) == 0) self.mock(main.RbuildMain, 'main', lambda *args, **kw: 23) assert (main.main(['rbuild', 'help']) == 23) oldargv = sys.argv try: sys.argv = ['rbuild', 'help'] assert (main.main() == 23) finally: sys.argv = oldargv