Beispiel #1
0
def checkExceptionPerGreenlet(outfileName=None, ignoreHealthyOnes=True):
    mylog("Trying to detect greenlets...", verboseLevel=-2)
    if not outfileName:
        for ob in gc.get_objects():
            if not hasattr(ob, 'parent_args'):
                continue
            if not ob:
                continue
            if ignoreHealthyOnes and (not ob.exception):
                 continue
            mylog('%s[%s] called with parent arg\n(%s)\n%s' % (ob.name, repr(ob.args), repr(ob.parent_args),
                ''.join(traceback.format_stack(ob.gr_frame))), verboseLevel=-2)
            mylog(ob.exception, verboseLevel=-2)
    else:
        handler = open(outfileName, 'w')
        for ob in gc.get_objects():
            if not hasattr(ob, 'parent_args'):
                continue
            if not ob:
                continue
            if ignoreHealthyOnes and (not ob.exception):
                 continue
            handler.write('%s[%s] called with parent arg\n(%s)\n%s' % (ob.name, repr(ob.args), repr(ob.parent_args),
                ''.join(traceback.format_stack(ob.gr_frame))))
            handler.write(ob.exception)
Beispiel #2
0
 def endrun(self, status):
     """By convention for now, status is one of:
         'DEAD'
         'UNSUBMITTED' (a pseudo-status defined in the ready-loop of alive())
         'EXIT rc'
     """
     name = status.split()[0]
     if name == 'DEAD':
         log.warning(''.join(traceback.format_stack()))
         log.error('Task {}\n is DEAD, meaning no HEARTBEAT, but this can be a race-condition. If it was not killed, then restarting might suffice. Otherwise, you might have excessive clock-skew.'.format(self.brief()))
         self.setTargetStatus(pypeflow.task.TaskFail) # for lack of anything better
     elif name == 'UNSUBMITTED':
         log.warning(''.join(traceback.format_stack()))
         log.error('Task {}\n is UNSUBMITTED, meaning job-submission somehow failed. Possibly a re-start would work. Otherwise, you need to investigate.'.format(self.brief()))
         self.setTargetStatus(pypeflow.task.TaskFail) # for lack of anything better
     elif name != 'EXIT':
         raise Exception('Unexpected status {!r}'.format(name))
     else:
         code = int(status.split()[1])
         if 0 == code:
             self.__target.check_missing()
             # TODO: If missing, just leave the status as TaskInitialized?
         else:
             log.error('Task {} failed with exit-code={}'.format(self.brief(), code))
             self.setTargetStatus(pypeflow.task.TaskFail) # for lack of anything better
     self.__target.finish()
Beispiel #3
0
    def entryExitLoggingWrap(*args, **kwargs):

      if entryExitLogLevel is None:
        enabled = False
      else:
        logger = getLoggerCallback()
        enabled = logger.isEnabledFor(entryExitLogLevel)

      if not enabled:
        return func(*args, **kwargs)
      
      funcName = str(func)
      
      if logArgs:
        argsRepr = ', '.join(
          [repr(a) for a in args] +
          ['%s=%r' % (k,v,) for k,v in kwargs.iteritems()])
      else:
        argsRepr = ''
      
      logger.log(
        entryExitLogLevel, "ENTERING: %s(%s)%s", funcName, argsRepr,
        '' if not logTraceback else '; ' + repr(traceback.format_stack()))

      try:
        return func(*args, **kwargs)
      finally:
        logger.log(
          entryExitLogLevel, "LEAVING: %s(%s)%s", funcName, argsRepr,
          '' if not logTraceback else '; ' + repr(traceback.format_stack()))
def _submit(runConfig):
    if registry.has_run(runConfig.runid):
        msg = "Runid %s does already exist" % runConfig.runid
        return SubmissionResponse(runConfig.runid, SUBM_ALREADY_SUBMITTED, msg)
        
    try:
        pipeline_exec = PipelineExecution(runConfig, server_model.config)
        logger.debug("PipelineExecution object for runid %s created."%runConfig.runid)
        pipeline_exec.initialize()
        logger.debug("PipelineExecution object for runid %s initialized."%runConfig.runid)
        registry.add_run(runConfig.runid, pipeline_exec)
        logger.debug("PipelineExecution object for runid %s registered."%runConfig.runid)
        _ = pipeline_exec.start()
        msg = "Pipeline execution for runid %s started."%runConfig.runid
        logger.info(msg)
    except ConfigurationError as ce:
        msg = 'Submission failed with exception %s.' % str(ce)
        stacktrace= 'Stacktrace: \n%s'%'\n'.join(traceback.format_stack())
        logger.warn(msg)
        return SubmissionResponse(runConfig.runid, SUBM_FAILED, msg, stacktrace)
    except Exception as e:
        msg = 'Submission failed with exception %s.' % str(e)
        stacktrace= 'Stacktrace: \n%s'%'\n'.join(traceback.format_stack())
        logger.error(msg)
        return SubmissionResponse(runConfig.runid, SUBM_ERROR, msg, stacktrace)
    try:
        history.add_entry("Runid %s submitted to the system" % runConfig.runid, datetime.datetime.now())
    except Exception as e:
        logger.warn("Runid %s could not be appended to the history."%runConfig.runid)
        msg=msg+"\n But not entry could be added the server history."

    return SubmissionResponse(runConfig.runid, SUBM_EXECUTING, msg)
Beispiel #5
0
	def deprecated(self, method):
		"""Output a deprecation warning.

		The implementation of WebKit sometimes invokes this method which prints
		a warning that the method you are using has been deprecated.
		This method expects that deprecated methods say so at the beginning of
		their doc string and terminate that msg with @. For example:

			DEPRECATED: Class.foo() on 01/24/01 in ver 0.5. Use Class.bar() instead. @

		Putting this information in the doc string is important for accuracy
		in the generated docs.

		Example call:
			self.deprecated(self.foo)

		"""
		docString = method.__doc__
		if docString:
			msg = docString.split('@')[0]
			msg = '\n'.join(map(lambda s: s.strip(), msg.splitlines()))
		else:
			msg = 'DEPRECATED: %s (no doc string)' % method
		print msg
		try:
			from traceback import format_stack
			print format_stack(limit =3)[0]
		except Exception:
			print 'Could not determine calling function.'
 def __init__(self, *args, **kwargs):
     if len(args) == 1 and not kwargs and isinstance(args[0], Exception):
         # we shall just wrap a non-caused exception
         self.stack = (
             traceback.format_stack()[:-2] +
             traceback.format_tb(sys.exc_info()[2]))
         # ^^^ let's hope the information is still there; caller must take
         # care of this.
         self.wrapped = args[0]
         self._cause = []
         super(CausedException, self).__init__(repr(args[0]))
         # ^^^ to display what it is wrapping, in case it gets printed or similar
         return
     self.wrapped = None
     self.stack = traceback.format_stack()[:-1]  # cut off current frame
     try:
         cause = kwargs['cause']
         del kwargs['cause']
     except KeyError:
         cause = []
     if not isinstance(cause, list):
         cause = [cause]
     self._cause = [CausedException(e) if not isinstance(e, CausedException) else e for e in cause]
     self.kwargs = kwargs
     super(CausedException, self).__init__(*args)
Beispiel #7
0
def error(event, raises=False, e_type=Exception, log_trace=True, limit=None):
    """A level of information that may have caused an error.

    :param event: error event instance
    :param raises: boolean of whether or not an error should be raised
    :param e_type: exception type to be raised
    :param log_trace: boolean of whether or not log traceback
    :param limit: integer of traceback limit
    """
    if raises:
        if hasattr(event, "info"):
            logged_exception = e_type(event.info)
        else:
            logged_exception = e_type(event)

        if log_trace:
            try:
                raise logged_exception
            except Exception as err:
                given_traceback = traceback.format_stack(limit=limit)[:-1]
                given_traceback.append(traceback.format_exc(limit=limit))
                event.info += '\n' + ''.join(given_traceback)
            finally:
                raise logged_exception
        else:
            raise logged_exception

    elif log_trace:
        event.info += ''.join(traceback.format_stack())
Beispiel #8
0
 def trace(self, output=''):
     if output == 'stderr':
         sys.stderr.write(''.join(traceback.format_stack()))
         sys.stderr.flush()
     else:
         sys.stdout.write(''.join(traceback.format_stack()))
         sys.stdout.flush()
Beispiel #9
0
    def _ensure_transport(self):
        """
        Ensures this Channel has been activated with the Node.
        """
#        log.debug("BaseChannel._ensure_transport (current: %s)", self._transport is not None)
        if not self._transport:
            raise ChannelError("No transport attached")

        if not self._lock:
            raise ChannelError("No lock available")

        # is lock already acquired? spit out a notice
        if self._lock._is_owned():
            log.warn("INTERLEAVE DETECTED:\n\nCURRENT STACK:\n%s\n\nSTACK THAT LOCKED: %s\n",
                    "".join(traceback.format_stack()), "".join(self._lock_trace))

        with self._lock:
            # we could wait and wait, and it gets closed, and unless we check again, we'd never know!
            if not self._transport:
                raise ChannelError("No transport attached")

            self._lock_trace = traceback.format_stack()
            try:
                yield
            finally:
                self._lock_trace = None
def queuedb_query_execute( cur, query, values ):
    """
    Execute a query.  If it fails, exit.

    DO NOT CALL THIS DIRECTLY.
    """
    timeout = 1.0
    while True:
        try:
            ret = cur.execute( query, values )
            return ret
        except sqlite3.OperationalError as oe:
            if oe.message == "database is locked":
                timeout = timeout * 2 + timeout * random.random()
                log.error("Query timed out due to lock; retrying in %s: %s" % (timeout, namedb_format_query( query, values )))
                time.sleep(timeout)
            
            else:
                log.exception(oe)
                log.error("FATAL: failed to execute query (%s, %s)" % (query, values))
                log.error("\n".join(traceback.format_stack()))
                os.abort()

        except Exception, e:
            log.exception(e)
            log.error("FATAL: failed to execute query (%s, %s)" % (query, values))
            log.error("\n".join(traceback.format_stack()))
            os.abort()
Beispiel #11
0
    def upsert(self, pgUrl, commit=True, **kwargs):
        if "url" in kwargs and "drive_web" in kwargs["url"]:
            self.log.error("")
            self.log.error("")
            self.log.error("WAT")
            self.log.error("")
            self.log.error(traceback.format_stack())
            self.log.error("")

        if "url" in kwargs and not kwargs["url"].startswith("http"):
            self.log.error("")
            self.log.error("")
            self.log.error("WAT")
            self.log.error("")
            self.log.error(traceback.format_stack())
            self.log.error("")

            # print("Upserting!")

        with self.transaction(commit=commit) as cur:
            # Do everything in one transaction

            try:
                self.insertIntoDb(url=pgUrl, cursor=cur, **kwargs)

            except psycopg2.IntegrityError:
                if kwargs:
                    cur.execute("ROLLBACK")
                    cur.execute("BEGIN")
                    self.updateDbEntry(url=pgUrl, cursor=cur, **kwargs)
  def __formatFailure(self, failure, logMessage, extras):
    """Generates a dict from the given Failure object."""

    parts = ['Traceback (most recent call last):']
    if not failure.frames:
      parts += traceback.format_stack()
    else:
      for functionName, filename, lineNumber, _, _ in failure.frames:
        parts.append('File "%s", line %s, in %s' % (filename, lineNumber, functionName))
    backtrace = '\n'.join(parts)

    result = {
      'project': self.__project,
      'type': failure.type.__module__ + '.' + failure.type.__name__,
      'message': str(failure.value),
      'environment': self.__environment,
      'serverName': self.__serverName,
      'logMessage': logMessage,
      'backtrace': backtrace,
      'loggedFrom': '\n'.join(traceback.format_stack())
    }

    if extras and 'level' in extras:
      result['errorLevel'] = extras['level']
      del extras['level']

    if context and context.all():
      result['context'] = context.all()
      if extras:
        result['context'] = result['context'].copy()
        result['context'].update(extras)
    elif extras:
      result['context'] = extras

    return result
    def drop_transaction(self, transaction):
        """Drops the specified transaction, validating that it is
        actually saved away under the current executing thread.

        """

        thread_id = transaction.thread_id

        if not thread_id in self._cache:
            _logger.error('Runtime instrumentation error. Attempt to '
                    'to drop the transaction but where none is active. '
                    'Report this issue to New Relic support.\n%s',
                    ''.join(traceback.format_stack()[:-1]))

            raise RuntimeError('no active transaction')

        current = self._cache.get(thread_id)

        if transaction != current:
            _logger.error('Runtime instrumentation error. Attempt to '
                    'to drop the transaction when it is not the current '
                    'transaction. Report this issue to New Relic support.\n%s',
                    ''.join(traceback.format_stack()[:-1]))

            raise RuntimeError('not the current transaction')

        transaction._greenlet = None

        del self._cache[thread_id]
Beispiel #14
0
    def __init__(self, *args, **kwargs):
        if len(args) == 1 and not kwargs and isinstance(args[0], Exception):
            # we shall just wrap a non-caused exception
            self.stack = traceback.format_stack()[:-2] + traceback.format_tb(sys.exc_info()[2])
            # ^^^ let's hope the information is still there; caller must take
            #     care of this.
            self.wrapped = args[0]
            self.cause = ()
            super(CausedException, self).__init__(repr(args[0]))
            # ^^^ to display what it is wrapping, in case it gets printed or similar
            return

        self.wrapped = None
        # cut off frames we don't care about
        class_depth = max(ancestor_class_depth(CausedException, self.__class__), 0)
        self.stack = traceback.format_stack()[: -(class_depth + 1)]

        try:
            cause = kwargs["cause"]
            del kwargs["cause"]
        except:
            cause = ()

        if isinstance(cause, Exception) and not isinstance(cause, CausedException):
            cause = CausedException(cause)
        self.cause = cause if isinstance(cause, tuple) else (cause,)
        super(CausedException, self).__init__(*args, **kwargs)
Beispiel #15
0
def dump_stacks():
    dump = []

    # threads
    threads = dict([(th.ident, th.name)
                    for th in threading.enumerate()])

    for thread, frame in sys._current_frames().items():
        if thread not in threads:
            continue
        dump.append('Thread 0x%x (%s)\n' % (thread, threads[thread]))
        dump.append(''.join(traceback.format_stack(frame)))
        dump.append('\n')

    # greenlets
    try:
        from greenlet import greenlet
    except ImportError:
        return dump

    # if greenlet is present, let's dump each greenlet stack
    for ob in gc.get_objects():
        if not isinstance(ob, greenlet):
            continue
        if not ob:
            continue   # not running anymore or not started
        dump.append('Greenlet\n')
        dump.append(''.join(traceback.format_stack(ob.gr_frame)))
        dump.append('\n')

    return dump
Beispiel #16
0
	def updateObject(self, table_name, flt, values, turn_n = None):
		#table_name = '%s_%s'%(table, turn_n) if turn_n else table
		try:
			self.cur.execute('update %s SET %s WHERE %s'%(table_name, ','.join(['%s=%s'%(k,v) for k,v in values.iteritems()]), ' AND '.join(flt)))
			self.conn.commit()
		except sqlite3.Error, e:
			log.error('Error "%s", when executing: delete from %s WHERE %s'%(e, table_name, ' AND '.join(data)))
			print traceback.format_stack()		
Beispiel #17
0
	def eraseObject(self, table_name, data, turn_n = None):
		#table_name = '%s_%s'%(table, turn_n) if turn_n else table
		try:
			self.cur.execute('delete from %s WHERE %s'%(table_name, ' AND '.join(data)))
			self.conn.commit()
		except sqlite3.Error, e:
			log.error('Error "%s", when executing: delete from %s WHERE %s'%(e, table_name, ' AND '.join(data)))
			print traceback.format_stack()
Beispiel #18
0
				def timeout(self):
					try:
						import time

						now = time.strftime('%c')
						self.journal.info("## %s: %s ##" % (now, self.message))
					except:
						import traceback
						traceback.format_stack()
Beispiel #19
0
 def printTrace(self, output=''):
     if output == 'stderr':
         sys.stderr.write(''.join(traceback.format_stack()))
         if self.__log_level >= self.__levels['debug']:
             sys.stderr.flush()
     else:
         sys.stdout.write(''.join(traceback.format_stack()))
         if self.__log_level >= self.__levels['debug']:
             sys.stdout.flush()
Beispiel #20
0
def printTrace(output=''):
    if output == 'stderr':
        sys.stderr.write(''.join(traceback.format_stack()))
        if log_level >= LOG_DEBUG:
            sys.stderr.flush()
    else:
        sys.stdout.write(''.join(traceback.format_stack()))
        if log_level >= LOG_DEBUG:
            sys.stdout.flush()
Beispiel #21
0
def prlk_stack(siPrevio=False):
    import traceback

    if siPrevio:
        prlk("-" * 80 + "\n")
        prlk(traceback.format_stack())
        prlk("\n" + "-" * 80 + "\n")
    for line in traceback.format_stack()[:-1]:
        prlk(line.strip() + "\n")
Beispiel #22
0
def _assert(cond, msg=''):
    """
    Custom C{assert}-like function to verify the assertions
    even on release builds.
    """
    if not cond:
        logger.error('Assertion failed: "%s". %s',
                     msg, traceback.format_stack())
        print msg, traceback.format_stack()
        raise AssertionError(msg)
Beispiel #23
0
 def file_writelines(self, file_path, lines):
     '''写数组到文件中
     '''
     try:
         fw = codecs.open(file_path, 'w', 'utf-8')
         fw.writelines([line + '\n' for line in lines])
         fw.flush()
         fw.close()
     except:
         print traceback.format_stack()
Beispiel #24
0
 def _explain(curs):
     try:
         explain = curs.explain()
     except (TypeError, OperationFailure), e:
         explain = {'error': 'stack:\n\n%s\n\nexception:\n\n%s' %
                             (''.join(traceback.format_stack()),
                              ''.join(traceback.format_exc()))}
         logging.exception('error trying to run explain on curs\n\n'
                           'stack:\n\n%s\n\nexception:\n\n' %
                           (''.join(traceback.format_stack())))
Beispiel #25
0
def log(msg, level = logging.DEBUG):
    logging.log(level, msg)
    print('%s [%s], msg:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), level, msg))

    if level == logging.WARNING or level == logging.ERROR:
        for line in traceback.format_stack():
            print(line.strip())

        for line in traceback.format_stack():
            logging.log(level, line.strip())
Beispiel #26
0
def exception(group, message, *args, exc=None, log_current_callstack=True, frame=DEFAULT, use_format_stack=False, level=LEVEL_EXCEPTION, owner=None):
    if owner:
        message = ('[{owner}] ' + message).format(owner=owner, *args)
    elif args:
        message = message.format(*args)
    if frame is DEFAULT:
        frame = sys._getframe(1)
    if exc is None:
        (exc_type, exc, exc_tb) = sys.exc_info()
        log_current_callstack_prefix = ''
    else:
        exc_type = type(exc)
        exc_tb = exc.__traceback__
        log_current_callstack_prefix = 'Caught and logged:\n'
    if callback_on_error_or_exception is not None:
        callback_on_error_or_exception(message)
    tb = format_exc(exc)
    if use_format_stack:
        dialog_text = ''.join(traceback.format_stack(frame))
    else:
        dialog_text = tb
    if exc is not None:
        try:
            headline = str(exc)
        except:
            headline = '<unprintable exception {}>'.format(type(exc).__name__)
        classname = exc_type.__name__
        if classname in headline:
            headline = ' ({})'.format(headline)
        elif headline:
            headline = ' ({}: {})'.format(classname, headline)
        else:
            headline = ' ({})'.format(classname)
        message += headline
    message_base = message
    tbx = tb.split('\n', 1)
    message += '\n' + tbx[0] + '\n'
    if log_current_callstack:
        message += log_current_callstack_prefix
        message += ''.join(traceback.format_stack(frame))
    message += tbx[1]
    blank_line(group, level, frame, ring_bell=ring_bell_on_exception)
    ConsoleColor.change_color(get_console_color(level, group))
    _trace.trace(TYPE_LOG, message, group, level, get_log_zone(), frame)
    errorLog = '<report><version>2</version><sessionid>%lld</sessionid><type>desync</type>'
    errorLog += '<sku>ea.maxis.sims4.13.pc</sku><createtime>'
    errorLog += datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    errorLog += '</createtime><buildsignature>Local.Unknown.Unknown.0.0.0.0.Debug</buildsignature>'
    errorLog += '<categoryid>%s</categoryid>'
    errorLog += '<desyncid>%lld</desyncid>'
    errorLog += '<systemconfig/><screenshot/>'
    errorLog += '<desyncdata>%s</desyncdata></report>\n'
    _trace.log_exception(errorLog, tbx[1], frame)
    sim_error_dialog(message_base, exc_tb, dialog_text, level=level)
Beispiel #27
0
 def retryWrap(*args, **kwargs):
   numAttempts = 0
   delaySec = initialRetryDelaySec
   startTime = time.time()
   
   # Make sure it gets called at least once
   while True:
     numAttempts += 1
     try:
       result = func(*args, **kwargs)
     except retryExceptions, e:
       if not retryFilter(e, args, kwargs):
         logger = getLoggerCallback()
         if logger.isEnabledFor(logging.DEBUG):
           logger.debug(
             '[%s] Failure in %r; retries aborted by custom retryFilter. '
             'Caller stack:\n%s', clientLabel, func,
             ''.join(traceback.format_stack()), exc_info=True)
         raise
       
       now = time.time()
       # Compensate for negative time adjustment so we don't get stuck
       # waiting way too long (python doesn't provide monotonic time yet)
       if now < startTime:
         startTime = now
       if (now - startTime) >= timeoutSec:
         getLoggerCallback().exception(
           '[%s] Exhausted retry timeout (%s sec.; %s attempts) for %r. '
           'Caller stack:\n%s', clientLabel, timeoutSec, numAttempts, func,
           ''.join(traceback.format_stack()))
         raise
       
       if numAttempts == 1:
         getLoggerCallback().warning(
           '[%s] First failure in %r; initial retry in %s sec.; '
           'timeoutSec=%s. Caller stack:\n%s', clientLabel, func, delaySec,
           timeoutSec, ''.join(traceback.format_stack()), exc_info=True)
       else:
         getLoggerCallback().debug(
           '[%s] %r failed %s times; retrying in %s sec.; timeoutSec=%s. '
           'Caller stack:\n%s',
           clientLabel, func, numAttempts, delaySec, timeoutSec,
           ''.join(traceback.format_stack()), exc_info=True)
           
           
         time.sleep(delaySec)
         
         delaySec = min(delaySec*2, maxRetryDelaySec)
     else:
       if numAttempts > 1:
         getLoggerCallback().info('[%s] %r succeeded on attempt # %d',
                                  clientLabel, func, numAttempts)
         
       return result
  def thread(self, id):
    id = int(id)
    print id  
    print sys._current_frames()
    stack = '<br />'.join(traceback.format_stack(sys._current_frames()[id]))
    stack = traceback.format_stack(sys._current_frames()[id])
    locals = sys._current_frames()[id].f_locals
    globals = sys._current_frames()[id].f_globals
    t = _env.get_template('thread.html')

    return t.render(id=id, stack=stack, locals=locals, globals=globals)
Beispiel #29
0
 def file_write(self, file_path, content):
     '''写入内容到某个文件中
     '''
     try:
         # 普通的write和writelines接口无法写入utf8,需要通过codecs的open函数写入
         fw = codecs.open(file_path, 'w', 'utf-8')
         fw.write(content)
         fw.close()
     except:
         print 'error!:'
         print traceback.format_stack()
         fw.close()
Beispiel #30
0
 def file_writelines(self, file_path, lines):
     '''写入内容到某个文件中,此时的内容是一个数组
     '''
     try:
         fw = codecs.open(file_path, 'w', 'utf-8')
         fw.writelines(lines)
         fw.close()
     except:
         print 'error!:'
         print traceback.format_stack()
         pdb.set_trace()
         fw.close()
Beispiel #31
0
def main(args, net, datadir_path, merged_urls, worker_endpoint):
    try:
        print 'p2pool (version %s)' % (p2pool.__version__, )
        print

        @defer.inlineCallbacks
        def connect_p2p():
            # connect to bitcoind over bitcoin-p2p
            print '''Testing bitcoind P2P connection to '%s:%s'...''' % (
                args.bitcoind_address, args.bitcoind_p2p_port)
            factory = bitcoin_p2p.ClientFactory(net.PARENT)
            reactor.connectTCP(args.bitcoind_address, args.bitcoind_p2p_port,
                               factory)

            def long():
                print '''    ...taking a while. Common reasons for this include all of bitcoind's connection slots being used...'''

            long_dc = reactor.callLater(5, long)
            yield factory.getProtocol()  # waits until handshake is successful
            if not long_dc.called: long_dc.cancel()
            print '    ...success!'
            print
            defer.returnValue(factory)

        if args.testnet:  # establish p2p connection first if testnet so bitcoind can work without connections
            factory = yield connect_p2p()

        # connect to bitcoind over JSON-RPC and do initial getmemorypool
        url = '%s://%s:%i/' % ('https' if args.bitcoind_rpc_ssl else 'http',
                               args.bitcoind_address, args.bitcoind_rpc_port)
        print '''Testing bitcoind RPC connection to '%s' with username '%s'...''' % (
            url, args.bitcoind_rpc_username)
        bitcoind = jsonrpc.HTTPProxy(
            url,
            dict(Authorization='Basic ' +
                 base64.b64encode(args.bitcoind_rpc_username + ':' +
                                  args.bitcoind_rpc_password)),
            timeout=30)
        yield helper.check(bitcoind, net, args)
        temp_work = yield helper.getwork(bitcoind)

        bitcoind_getinfo_var = variable.Variable(None)

        @defer.inlineCallbacks
        def poll_warnings():
            bitcoind_getinfo_var.set(
                (yield deferral.retry('Error while calling getinfo:')(
                    bitcoind.rpc_getnetworkinfo)()))

        yield poll_warnings()
        deferral.RobustLoopingCall(poll_warnings).start(20 * 60)

        print '    ...success!'
        print '    Current block hash: %x' % (temp_work['previous_block'], )
        print '    Current block height: %i' % (temp_work['height'] - 1, )
        print

        if not args.testnet:
            factory = yield connect_p2p()

        print 'Determining payout address...'
        pubkeys = keypool()
        if args.pubkey_hash is None and args.address != 'dynamic':
            address_path = os.path.join(datadir_path, 'cached_payout_address')

            if os.path.exists(address_path):
                with open(address_path, 'rb') as f:
                    address = f.read().strip('\r\n')
                print '    Loaded cached address: %s...' % (address, )
            else:
                address = None

            if address is not None:
                res = yield deferral.retry(
                    'Error validating cached address:',
                    5)(lambda: bitcoind.rpc_getaddressinfo(address))()
                if not res['ismine']:
                    print '    Cached address is either invalid or not controlled by local bitcoind!'
                    address = None

            if address is None:
                print '    Getting payout address from bitcoind...'
                address = yield deferral.retry(
                    'Error getting payout address from bitcoind:',
                    5)(lambda: bitcoind.rpc_getaccountaddress('p2pool'))()

            with open(address_path, 'wb') as f:
                f.write(address)

            my_address = address
            print('    ...success! Payout address: %s' % my_address)
            print()

            pubkeys.addkey({'address': my_address})
        elif args.address != 'dynamic':
            my_address = args.address
            print('    ...success! Payout address: %s' % my_address)
            print()

            pubkeys.addkey({'address': my_address})
        else:
            print '    Entering dynamic address mode.'

            if args.numaddresses < 2:
                print ' ERROR: Can not use fewer than 2 addresses in dynamic mode. Resetting to 2.'
                args.numaddresses = 2
            for i in range(args.numaddresses):
                address = yield deferral.retry(
                    'Error getting a dynamic address from bitcoind:', 5
                )(lambda: bitcoind.rpc_getnewaddress('p2pool', 'legacy'))()
                pubkeys.addkey({'address': address})

            pubkeys.updatestamp(time.time())

            my_address = pubkeys.keys[0]['address']

            for i in range(len(pubkeys.keys)):
                print('    ...payout %d: %s' % (i, pubkeys[i]['address']))

        print "Loading shares..."
        shares = {}
        known_verified = set()

        def share_cb(share):
            share.time_seen = 0  # XXX
            shares[share.hash] = share
            if len(shares) % 1000 == 0 and shares:
                print "    %i" % (len(shares), )

        ss = p2pool_data.ShareStore(os.path.join(datadir_path, 'shares.'), net,
                                    share_cb, known_verified.add)
        print "    ...done loading %i shares (%i verified)!" % (
            len(shares), len(known_verified))
        print

        print 'Initializing work...'

        global gnode
        gnode = node = p2pool_node.Node(factory, bitcoind, shares.values(),
                                        known_verified, net)
        yield node.start()

        for share_hash in shares:
            if share_hash not in node.tracker.items:
                ss.forget_share(share_hash)
        for share_hash in known_verified:
            if share_hash not in node.tracker.verified.items:
                ss.forget_verified_share(share_hash)
        node.tracker.removed.watch(lambda share: ss.forget_share(share.hash))
        node.tracker.verified.removed.watch(
            lambda share: ss.forget_verified_share(share.hash))

        def save_shares():
            for share in node.tracker.get_chain(
                    node.best_share_var.value,
                    min(node.tracker.get_height(node.best_share_var.value),
                        2 * net.CHAIN_LENGTH)):
                ss.add_share(share)
                if share.hash in node.tracker.verified.items:
                    ss.add_verified_hash(share.hash)

        deferral.RobustLoopingCall(save_shares).start(60)

        if len(shares) > net.CHAIN_LENGTH:
            best_share = shares[node.best_share_var.value]
            previous_share = shares[
                best_share.share_data['previous_share_hash']]
            counts = p2pool_data.get_desired_version_counts(
                node.tracker,
                node.tracker.get_nth_parent_hash(previous_share.hash,
                                                 net.CHAIN_LENGTH * 9 // 10),
                net.CHAIN_LENGTH // 10)
            p2pool_data.update_min_protocol_version(counts, best_share)

        print '    ...success!'
        print

        print 'Joining p2pool network using port %i...' % (args.p2pool_port, )

        @defer.inlineCallbacks
        def parse(host):
            port = net.P2P_PORT
            if ':' in host:
                host, port_str = host.split(':')
                port = int(port_str)
            defer.returnValue(((yield reactor.resolve(host)), port))

        addrs = {}
        if os.path.exists(os.path.join(datadir_path, 'addrs')):
            try:
                with open(os.path.join(datadir_path, 'addrs'), 'rb') as f:
                    addrs.update(
                        dict((tuple(k), v) for k, v in json.loads(f.read())))
            except:
                print >> sys.stderr, 'error parsing addrs'
        for addr_df in map(parse, net.BOOTSTRAP_ADDRS):
            try:
                addr = yield addr_df
                if addr not in addrs:
                    addrs[addr] = (0, time.time(), time.time())
            except:
                log.err()

        connect_addrs = set()
        for addr_df in map(parse, args.p2pool_nodes):
            try:
                connect_addrs.add((yield addr_df))
            except:
                log.err()

        node.p2p_node = p2pool_node.P2PNode(
            node,
            port=args.p2pool_port,
            max_incoming_conns=args.p2pool_conns,
            addr_store=addrs,
            connect_addrs=connect_addrs,
            desired_outgoing_conns=args.p2pool_outgoing_conns,
            advertise_ip=args.advertise_ip,
            external_ip=args.p2pool_external_ip,
        )
        node.p2p_node.start()

        def save_addrs():
            with open(os.path.join(datadir_path, 'addrs'), 'wb') as f:
                f.write(json.dumps(node.p2p_node.addr_store.items()))

        deferral.RobustLoopingCall(save_addrs).start(60)

        print '    ...success!'
        print

        if args.upnp:

            @defer.inlineCallbacks
            def upnp_thread():
                while True:
                    try:
                        is_lan, lan_ip = yield ipdiscover.get_local_ip()
                        if is_lan:
                            pm = yield portmapper.get_port_mapper()
                            yield pm._upnp.add_port_mapping(
                                lan_ip, args.p2pool_port, args.p2pool_port,
                                'p2pool', 'TCP')
                    except defer.TimeoutError:
                        pass
                    except:
                        if p2pool.DEBUG:
                            log.err(None, 'UPnP error:')
                    yield deferral.sleep(random.expovariate(1 / 120))

            upnp_thread()

        # start listening for workers with a JSON-RPC server

        print 'Listening for workers on %r port %i...' % (worker_endpoint[0],
                                                          worker_endpoint[1])

        wb = work.WorkerBridge(node, my_address, 0.0, merged_urls,
                               args.worker_fee, args, pubkeys, bitcoind,
                               args.share_rate)
        web_root = web.get_web_root(wb,
                                    datadir_path,
                                    bitcoind_getinfo_var,
                                    static_dir=args.web_static)
        caching_wb = worker_interface.CachingWorkerBridge(wb)
        worker_interface.WorkerInterface(caching_wb).attach_to(
            web_root, get_handler=lambda request: request.redirect('/static/'))
        web_serverfactory = server.Site(web_root)

        serverfactory = switchprotocol.FirstByteSwitchFactory(
            {'{': stratum.StratumServerFactory(caching_wb)}, web_serverfactory)
        deferral.retry('Error binding to worker port:', traceback=False)(
            reactor.listenTCP)(worker_endpoint[1],
                               serverfactory,
                               interface=worker_endpoint[0])

        with open(os.path.join(os.path.join(datadir_path, 'ready_flag')),
                  'wb') as f:
            pass

        print '    ...success!'
        print

        # done!
        print 'Started successfully!'
        print 'Go to http://127.0.0.1:%i/ to view graphs and statistics!' % (
            worker_endpoint[1], )
        print

        if hasattr(signal, 'SIGALRM'):
            signal.signal(
                signal.SIGALRM, lambda signum, frame: reactor.callFromThread(
                    sys.stderr.write, 'Watchdog timer went off at:\n' + ''.
                    join(traceback.format_stack())))
            signal.siginterrupt(signal.SIGALRM, False)
            deferral.RobustLoopingCall(signal.alarm, 30).start(1)

        if args.irc_announce:
            from twisted.words.protocols import irc

            class IRCClient(irc.IRCClient):
                nickname = 'p2pool%02i' % (random.randrange(100), )
                channel = net.ANNOUNCE_CHANNEL

                def lineReceived(self, line):
                    if p2pool.DEBUG:
                        print repr(line)
                    irc.IRCClient.lineReceived(self, line)

                def signedOn(self):
                    self.in_channel = False
                    irc.IRCClient.signedOn(self)
                    self.factory.resetDelay()
                    self.join(self.channel)

                    @defer.inlineCallbacks
                    def new_share(share):
                        if not self.in_channel:
                            return
                        if share.pow_hash <= share.header[
                                'bits'].target and abs(share.timestamp -
                                                       time.time()) < 10 * 60:
                            yield deferral.sleep(random.expovariate(1 / 60))
                            message = '\x02%s BLOCK FOUND by %s! %s%064x' % (
                                net.NAME.upper(),
                                bitcoin_data.script2_to_address(
                                    share.new_script, net.ADDRESS_VERSION, -1,
                                    net.PARENT)
                                if share.VERSION < 34 else share.address,
                                net.PARENT.BLOCK_EXPLORER_URL_PREFIX,
                                share.header_hash)
                            if all('%x' %
                                   (share.header_hash, ) not in old_message
                                   for old_message in self.recent_messages):
                                self.say(self.channel, message)
                                self._remember_message(message)

                    self.watch_id = node.tracker.verified.added.watch(
                        new_share)
                    self.recent_messages = []

                def joined(self, channel):
                    self.in_channel = True

                def left(self, channel):
                    self.in_channel = False

                def _remember_message(self, message):
                    self.recent_messages.append(message)
                    while len(self.recent_messages) > 100:
                        self.recent_messages.pop(0)

                def privmsg(self, user, channel, message):
                    if channel == self.channel:
                        self._remember_message(message)

                def connectionLost(self, reason):
                    node.tracker.verified.added.unwatch(self.watch_id)
                    print 'IRC connection lost:', reason.getErrorMessage()

            class IRCClientFactory(protocol.ReconnectingClientFactory):
                protocol = IRCClient

            reactor.connectTCP("irc.freenode.net",
                               6667,
                               IRCClientFactory(),
                               bindAddress=(worker_endpoint[0], 0))

        @defer.inlineCallbacks
        def status_thread():
            last_str = None
            last_time = 0
            while True:
                yield deferral.sleep(30)
                try:
                    height = node.tracker.get_height(node.best_share_var.value)
                    this_str = 'P2Pool: %i shares in chain (%i verified/%i total) Peers: %i (%i incoming)' % (
                        height,
                        len(node.tracker.verified.items),
                        len(node.tracker.items),
                        len(node.p2p_node.peers),
                        sum(1 for peer in node.p2p_node.peers.itervalues()
                            if peer.incoming),
                    ) + (' FDs: %i R/%i W' %
                         (len(reactor.getReaders()), len(reactor.getWriters()))
                         if p2pool.DEBUG else '')

                    datums, dt = wb.local_rate_monitor.get_datums_in_last()
                    my_att_s = sum(datum['work'] / dt for datum in datums)
                    my_shares_per_s = sum(
                        datum['work'] / dt /
                        bitcoin_data.target_to_average_attempts(
                            datum['share_target']) for datum in datums)
                    this_str += '\n Local: %sH/s in last %s Local dead on arrival: %s Expected time to share: %s' % (
                        math.format(int(my_att_s)),
                        math.format_dt(dt),
                        math.format_binomial_conf(
                            sum(1 for datum in datums if datum['dead']),
                            len(datums), 0.95),
                        math.format_dt(1 / my_shares_per_s)
                        if my_shares_per_s else '???',
                    )

                    if height > 2:
                        (stale_orphan_shares,
                         stale_doa_shares), shares, _ = wb.get_stale_counts()
                        stale_prop = p2pool_data.get_average_stale_prop(
                            node.tracker, node.best_share_var.value,
                            min(60 * 60 // net.SHARE_PERIOD, height))
                        real_att_s = p2pool_data.get_pool_attempts_per_second(
                            node.tracker, node.best_share_var.value,
                            min(height - 1, 60 * 60 //
                                net.SHARE_PERIOD)) / (1 - stale_prop)

                        paystr = ''
                        paytot = 0.0
                        for i in range(len(pubkeys.keys)):
                            curtot = node.get_current_txouts().get(
                                pubkeys.keys[i]['address'], 0)
                            paytot += curtot * 1e-8
                            paystr += "(%.4f)" % (curtot * 1e-8, )
                        paystr += "=%.4f" % (paytot, )
                        this_str += '\n Shares: %i (%i orphan, %i dead) Stale rate: %s Efficiency: %s Current payout: %s %s' % (
                            shares,
                            stale_orphan_shares,
                            stale_doa_shares,
                            math.format_binomial_conf(
                                stale_orphan_shares + stale_doa_shares, shares,
                                0.95),
                            math.format_binomial_conf(
                                stale_orphan_shares + stale_doa_shares, shares,
                                0.95, lambda x: (1 - x) / (1 - stale_prop)),
                            paystr,
                            net.PARENT.SYMBOL,
                        )
                        this_str += '\n Pool: %sH/s Stale rate: %.1f%% Expected time to block: %s' % (
                            math.format(int(real_att_s)),
                            100 * stale_prop,
                            math.format_dt(
                                2**256 /
                                node.bitcoind_work.value['bits'].target /
                                real_att_s),
                        )

                        for warning in p2pool_data.get_warnings(
                                node.tracker, node.best_share_var.value, net,
                                bitcoind_getinfo_var.value,
                                node.bitcoind_work.value):
                            print >> sys.stderr, '#' * 40
                            print >> sys.stderr, '>>> Warning: ' + warning
                            print >> sys.stderr, '#' * 40

                        if gc.garbage:
                            print '%i pieces of uncollectable cyclic garbage! Types: %r' % (
                                len(gc.garbage), map(type, gc.garbage))

                    if this_str != last_str or time.time() > last_time + 15:
                        print this_str
                        last_str = this_str
                        last_time = time.time()
                except:
                    log.err()

        status_thread()
    except:
        reactor.stop()
        log.err(None, 'Fatal error:')
Beispiel #32
0
              'Intended Audience :: Developers',
              'Programming Language :: Python',
              'Programming Language :: C',
              'Programming Language :: Python :: 2',
              'Programming Language :: Python :: 2.7',
              'Programming Language :: Python :: 3',
              'Programming Language :: Python :: 3.3',
              'Programming Language :: Python :: 3.4',
              'Programming Language :: Python :: Implementation :: CPython',
              'Programming Language :: Python :: Implementation :: Jython',
              'Programming Language :: Python :: Implementation :: PyPy',
              'Operating System :: Microsoft :: Windows',
              'Operating System :: POSIX',
              'License :: OSI Approved :: BSD License',
              'Topic :: Software Development :: Libraries :: Python Modules',
              'Topic :: System :: Distributed Computing',
          ],
          **extras)


try:
    run_setup(not (is_jython or is_pypy or is_py3k))
except BaseException:
    if _is_build_command(sys.argv):
        import traceback
        print(BUILD_WARNING % '\n'.join(traceback.format_stack()),
              file=sys.stderr)
        run_setup(False)
    else:
        raise
Beispiel #33
0
def dump_thread_handler(signum, frame):
    # type: (int, FrameType) -> None
    for thread_id, thread_frame in sys._current_frames().items():
        print("-- thread id {}:".format(thread_id))
        print("".join(traceback.format_stack(thread_frame)))
Beispiel #34
0
 def logstacktrace(self):
     self.ui.log(
         "remotefilelog",
         "excess remotefilelog fetching:\n%s\n",
         "".join(traceback.format_stack()),
     )
Beispiel #35
0
    def __init__(self,
                 robot,
                 joystick=None,
                 log_data=True,
                 log_name=None,
                 log_dir=None,
                 debug_prints=False):
        """
        :param robot: a subclass instance of the atlasbuggy.robot.Robot class
        :param joystick: a subclass instance of the atlasbuggy.buggyjoystick.BuggyJoystick class
        :param log_data: enable data logging (debug prints logged even if they are not enabled)
        :param log_name: log file name (today's date is substituted if None (YYYY_MM_DD))
        :param log_dir: log directory (today's date is substituted if None (YYYY_MM_DD))
            Tuple feature: ("some_data", None) -> produces "some_data/YYYY_MM_DD"
        :param debug_prints: print debug messages
        """

        super(RobotRunner, self).__init__(robot, debug_prints, "Interface")

        # clock properties
        self.loop_ups = RobotRunner.loop_updates_per_second
        self.port_ups = RobotRunner.port_updates_per_second
        self.lag_warning_thrown = False  # prevents the terminal from being spammed
        self.clock = Clock(self.loop_ups)
        self.start_time = 0

        # keep the last packet info for crash info
        self.prev_packet_info = [None, None, None]

        # pass around reference to joystick
        self.joystick = joystick
        self.robot.joystick = joystick

        # initialize logger
        self.logger = Logger(log_name, log_dir)
        self.robot.logger = self.logger
        if log_data:
            self.logger.open()
            print("Writing to:", self.logger.full_path)

        for robot_object in self.robot.objects.values():
            robot_object.is_live = True
        self.robot.is_live = True
        self.robot.debug_enabled = self.debug_enabled

        # create a pipe from all port processes to the main loop
        self.packet_queue = Queue()
        self.packet_counter = Value('i', 0)
        self.port_lock = Lock()

        # open all available ports
        self.ports = {}
        threads = []

        # call _configure_port for each available serial port
        self.duplicate_id_error = [False, None]  # [did error occur, whoiam ID]
        for port_info in serial.tools.list_ports.comports():
            config_thread = threading.Thread(target=self._configure_port,
                                             args=(port_info, self.port_ups))
            threads.append(config_thread)
            config_thread.start()

        # wait for threads to finish
        for thread in threads:
            thread.join()

        if self.duplicate_id_error[0]:
            self._close_ports("error")
            raise self._handle_error(
                RobotSerialPortWhoiamIdTaken(
                    "whoiam ID already being used by another port! It's possible "
                    "the same code was uploaded for two boards.",
                    self.prev_packet_info, self.duplicate_id_error[1]),
                traceback.format_stack())

        # throw an error if a port isn't configured correctly
        for port_name, port in self.ports.items():
            if not port.configured:
                self._print_port_info(port)
                raise self._handle_error(
                    RobotSerialPortNotConfiguredError("Port not configured!",
                                                      self.prev_packet_info,
                                                      port),
                    traceback.format_stack())

        self._debug_print("Discovered ports:", list(self.ports.keys()))

        self._check_objects()  # check that all objects are assigned a port
        self._check_ports()  # check that all ports are assigned an object

        for whoiam in self.ports.keys():
            self._debug_print("[%s] has ID '%s'" %
                              (self.ports[whoiam].address, whoiam))

        # notify user of disabled robot objects
        if len(self.robot.inactive_ids) > 0 and self.debug_enabled:
            self._debug_print("Ignored IDs:")
            for whoiam in self.robot.inactive_ids:
                self._debug_print(whoiam)

        self._send_first_packets()  # distribute initialization packets
def g():
    for line in traceback.format_stack():
        print(line.strip())
Beispiel #37
0
    def fetch_enterprise_learner_data(self, user):
        """
        Fetch information related to enterprise from the Enterprise Service.

        Example:
            fetch_enterprise_learner_data(user)

        Argument:
            user: (User) django auth user

        Returns:
            dict:
            {
                "count": 1,
                "num_pages": 1,
                "current_page": 1,
                "next": null,
                "start": 0,
                "previous": null
                "results": [
                    {
                        "enterprise_customer": {
                            "uuid": "cf246b88-d5f6-4908-a522-fc307e0b0c59",
                            "name": "TestShib",
                            "active": true,
                            "site": {
                                "domain": "example.com",
                                "name": "example.com"
                            },
                            "enable_data_sharing_consent": true,
                            "enforce_data_sharing_consent": "at_login",
                            "branding_configuration": {
                                "enterprise_customer": "cf246b88-d5f6-4908-a522-fc307e0b0c59",
                                "logo": "https://open.edx.org/sites/all/themes/edx_open/logo.png"
                            },
                            "enterprise_customer_entitlements": [
                                {
                                    "enterprise_customer": "cf246b88-d5f6-4908-a522-fc307e0b0c59",
                                    "entitlement_id": 69
                                }
                            ],
                            "replace_sensitive_sso_username": False,
                        },
                        "user_id": 5,
                        "user": {
                            "username": "******",
                            "first_name": "",
                            "last_name": "",
                            "email": "*****@*****.**",
                            "is_staff": true,
                            "is_active": true,
                            "date_joined": "2016-09-01T19:18:26.026495Z"
                        },
                        "data_sharing_consent_records": [
                            {
                                "username": "******",
                                "enterprise_customer_uuid": "cf246b88-d5f6-4908-a522-fc307e0b0c59",
                                "exists": true,
                                "course_id": "course-v1:edX DemoX Demo_Course",
                                "consent_provided": true,
                                "consent_required": false
                            }
                        ]
                    }
                ],
            }

        Raises:
            ConnectionError: requests exception "ConnectionError", raised if if ecommerce is unable to connect
                to enterprise api server.
            SlumberBaseException: base slumber exception "SlumberBaseException", raised if API response contains
                http error status like 4xx, 5xx etc.
            Timeout: requests exception "Timeout", raised if enterprise API is taking too long for returning
                a response. This exception is raised for both connection timeout and read timeout.

        """
        if not user.is_authenticated:
            return None

        api_resource_name = 'enterprise-learner'

        try:
            endpoint = getattr(self.client, api_resource_name)
            querystring = {'username': user.username}
            response = endpoint().get(**querystring)
        except (HttpClientError, HttpServerError):
            LOGGER.exception(
                u'Failed to get enterprise-learner for user [%s] with client user [%s]. Caller: %s, Request PATH: %s',
                user.username,
                self.user.username,
                "".join(traceback.format_stack()),
                get_current_request().META['PATH_INFO'],
            )
            return None

        return response
Beispiel #38
0
def ShowExceptionTupleClient( etype, value, tb, do_wait = True ):
    
    if etype is None:
        
        etype = HydrusExceptions.UnknownException
        
    
    if value is None:
        
        value = 'Unknown error'
        
    
    if tb is None:
        
        trace = 'No error trace--here is the stack:' + os.linesep + ''.join( traceback.format_stack() )
        
    else:
        
        trace = ''.join( traceback.format_exception( etype, value, tb ) )
        
    
    pretty_value = str( value )
    
    if os.linesep in pretty_value:
        
        ( first_line, anything_else ) = pretty_value.split( os.linesep, 1 )
        
        trace = trace + os.linesep + anything_else
        
    else:
        
        first_line = pretty_value
        
    
    job_key = ClientThreading.JobKey()
    
    if etype == HydrusExceptions.ShutdownException:
        
        return
        
    else:
        
        title = str( getattr( etype, '__name__', etype ) )
        
        job_key.SetStatusTitle( title )
        
        job_key.SetVariable( 'popup_text_1', first_line )
        job_key.SetTraceback( trace )
        
    
    text = job_key.ToString()
    
    HydrusData.Print( 'Exception:' )
    
    HydrusData.DebugPrint( text )
    
    HG.client_controller.pub( 'message', job_key )
    
    if do_wait:
        
        time.sleep( 1 )
Beispiel #39
0
 def callback(sig, frame):
     txt = ''.join(traceback.format_stack(frame))
     print()
     print(txt)
Beispiel #40
0
 def fmt():
     return traceback.format_stack()
		def func_wrapper(*args, **kwargs):
			exc_info = sys.exc_info()
			try:
				return func(*args, **kwargs)
			except Exception as e:
				exc_type, exc_obj, exc_tb = sys.exc_info()
				erStr = ("%s ERROR - Prism_Plugin_Nuke_Integration - Core: %s - Plugin: %s:\n%s\n\n%s" % (time.strftime("%d/%m/%y %X"), args[0].core.version, args[0].plugin.version, ''.join(traceback.format_stack()), traceback.format_exc()))
				if hasattr(args[0].core, "writeErrorLog"):
					args[0].core.writeErrorLog(erStr)
				else:
					QMessageBox.warning(args[0].core.messageParent, "Prism Integration", erStr)
Beispiel #42
0
 def fn_ngettext(a, b, c) -> None:  # type: ignore
     print("".join(traceback.format_stack()[-2]))
     print(
         "ngettext global will break in the future; please see anki/lang.py"
     )
     return b
Beispiel #43
0
 def fn__(arg) -> None:  # type: ignore
     print("".join(traceback.format_stack()[-2]))
     print("_ global will break in the future; please see anki/lang.py")
     return arg
Beispiel #44
0
 def debug(sig, frame):
     logger.critical("Signal received: printing stack trace")
     logger.critical(str("").join(format_stack(frame)))
Beispiel #45
0
 def _signal_handler(self, sig, frame):
     self._handle_failure(f"Terminated with signal {sig}\n" +
                          "".join(traceback.format_stack(frame)))
     sys.exit(sig + 128)
Beispiel #46
0
 def not_to_be_wrapped(self, limit):
     return format_stack(limit=limit)
 def debug(*args, force=False, love_u=Config.TQDM, end='\n', ex=None):
     Tools.pyout(
         "DEBUG ->", traceback.format_stack()[-2].split('\n')[0])
     Tools.pyout("------->  ", *args, force=force, love_u=love_u, end=end)
     if ex is not None:
         sys.exit(ex)
Beispiel #48
0
 def __init__(self, driver, index, description):
     self.screenshot_binary = driver.get_screenshot_as_png()
     self.description = description
     self.index = index
     self.exc = traceback.format_exc()
     self.stack = traceback.format_stack()
Beispiel #49
0
def qt_message_handler(msg_type, context, msg):
    """Qt message handler to redirect qWarning etc. to the logging system.

    Args:
        QtMsgType msg_type: The level of the message.
        QMessageLogContext context: The source code location of the message.
        msg: The message text.
    """
    # Mapping from Qt logging levels to the matching logging module levels.
    # Note we map critical to ERROR as it's actually "just" an error, and fatal
    # to critical.
    qt_to_logging = {
        QtCore.QtDebugMsg: logging.DEBUG,
        QtCore.QtWarningMsg: logging.WARNING,
        QtCore.QtCriticalMsg: logging.ERROR,
        QtCore.QtFatalMsg: logging.CRITICAL,
    }
    try:
        qt_to_logging[QtCore.QtInfoMsg] = logging.INFO
    except AttributeError:
        # While we don't support Qt < 5.5 anymore, logging still needs to work
        pass

    # Change levels of some well-known messages to debug so they don't get
    # shown to the user.
    #
    # If a message starts with any text in suppressed_msgs, it's not logged as
    # error.
    suppressed_msgs = [
        # PNGs in Qt with broken color profile
        # https://bugreports.qt.io/browse/QTBUG-39788
        ('libpng warning: iCCP: Not recognizing known sRGB profile that has '
         'been edited'),
        'libpng warning: iCCP: known incorrect sRGB profile',
        # Hopefully harmless warning
        'OpenType support missing for script ',
        # Error if a QNetworkReply gets two different errors set. Harmless Qt
        # bug on some pages.
        # https://bugreports.qt.io/browse/QTBUG-30298
        ('QNetworkReplyImplPrivate::error: Internal problem, this method must '
         'only be called once.'),
        # Sometimes indicates missing text, but most of the time harmless
        'load glyph failed ',
        # Harmless, see https://bugreports.qt.io/browse/QTBUG-42479
        ('content-type missing in HTTP POST, defaulting to '
         'application/x-www-form-urlencoded. '
         'Use QNetworkRequest::setHeader() to fix this problem.'),
        # https://bugreports.qt.io/browse/QTBUG-43118
        'Using blocking call!',
        # Hopefully harmless
        ('"Method "GetAll" with signature "s" on interface '
         '"org.freedesktop.DBus.Properties" doesn\'t exist'),
        ('"Method \\"GetAll\\" with signature \\"s\\" on interface '
         '\\"org.freedesktop.DBus.Properties\\" doesn\'t exist\\n"'),
        'WOFF support requires QtWebKit to be built with zlib support.',
        # Weird Enlightment/GTK X extensions
        'QXcbWindow: Unhandled client message: "_E_',
        'QXcbWindow: Unhandled client message: "_ECORE_',
        'QXcbWindow: Unhandled client message: "_GTK_',
        # Happens on AppVeyor CI
        'SetProcessDpiAwareness failed:',
        # https://bugreports.qt.io/browse/QTBUG-49174
        ('QObject::connect: Cannot connect (null)::stateChanged('
         'QNetworkSession::State) to '
         'QNetworkReplyHttpImpl::_q_networkSessionStateChanged('
         'QNetworkSession::State)'),
        # https://bugreports.qt.io/browse/QTBUG-53989
        ("Image of format '' blocked because it is not considered safe. If "
         "you are sure it is safe to do so, you can white-list the format by "
         "setting the environment variable QTWEBKIT_IMAGEFORMAT_WHITELIST="),
        # Installing Qt from the installer may cause it looking for SSL3 or
        # OpenSSL 1.0 which may not be available on the system
        "QSslSocket: cannot resolve ",
        "QSslSocket: cannot call unresolved function ",
        # When enabling debugging with QtWebEngine
        ("Remote debugging server started successfully. Try pointing a "
         "Chromium-based browser to "),
        # https://github.com/qutebrowser/qutebrowser/issues/1287
        "QXcbClipboard: SelectionRequest too old",
        # https://github.com/qutebrowser/qutebrowser/issues/2071
        'QXcbWindow: Unhandled client message: ""',
        # https://codereview.qt-project.org/176831
        "QObject::disconnect: Unexpected null parameter",
    ]
    # not using utils.is_mac here, because we can't be sure we can successfully
    # import the utils module here.
    if sys.platform == 'darwin':
        suppressed_msgs += [
            'libpng warning: iCCP: known incorrect sRGB profile',
            # https://bugreports.qt.io/browse/QTBUG-47154
            ('virtual void QSslSocketBackendPrivate::transmit() SSLRead '
             'failed with: -9805'),
        ]

    if not msg:
        msg = "Logged empty message!"

    if any(msg.strip().startswith(pattern) for pattern in suppressed_msgs):
        level = logging.DEBUG
    else:
        level = qt_to_logging[msg_type]

    if context.function is None:
        func = 'none'
    elif ':' in context.function:
        func = '"{}"'.format(context.function)
    else:
        func = context.function

    if context.category is None or context.category == 'default':
        name = 'qt'
    else:
        name = 'qt-' + context.category
    if msg.splitlines()[0] == ('This application failed to start because it '
                               'could not find or load the Qt platform plugin '
                               '"xcb".'):
        # Handle this message specially.
        msg += ("\n\nOn Archlinux, this should fix the problem:\n"
                "    pacman -S libxkbcommon-x11")
        faulthandler.disable()

    if _args.debug:
        stack = ''.join(traceback.format_stack())
    else:
        stack = None
    record = qt.makeRecord(name,
                           level,
                           context.file,
                           context.line,
                           msg,
                           None,
                           None,
                           func,
                           sinfo=stack)
    qt.handle(record)
Beispiel #50
0
async def sendat(context):
    if not context.parameter:
        await context.edit(helpmsg)
        return
    mem_id = len(mem)
    mem.append("[NULL]")
    chat = await context.get_chat()
    args = " ".join(context.parameter).split("|")
    if not imported:
        await context.edit("请先安装依赖:`python3 -m pip install dateparser`\n随后,请重启 pagermaid。")
        return
    await context.edit(f"debug: tz data: {time.timezone} {time.tzname} {sign}{offset}")
    if len(args) != 2:
        if args[0].find("rm ") == 0:
            # clear timer
            target_ids = args[0][3:].strip().split(" ")
            for target_id in target_ids:
                if target_id.isnumeric():
                    if len(mem) > int(target_id):
                        mem[int(target_id)] = ""
                        await context.edit(f"id {target_id} successfully removed.")
                    else:
                        await context.edit("id out of range.")
                        return
                else:
                    await context.edit("id should be a integer.")
                    return
        else:
            await context.edit("Invalid argument counts. Expecting: 2")
        return
    if offset is None:
        await context.edit("Failed to get your server's timezone.")
        return
    try:
        if args[0].find("every ") == 0:
            # at this point, let's assume args[0] contains relative time
            # i.e. -sendat every 3 minutes
            time_str = args[0][6:]
            if time_str.find(":") != -1:
                # then it should be absolute time
                target = dateparser.parse(time_str, settings=settings).timestamp() % DAY_SECS
                if target >= DAY_SECS - 6:
                    # 太接近午夜,小概率直接 sleep 过头,特殊处理
                    target = 0
                mem[mem_id] = "|".join(args)
                await sendmsg(context, chat, f"{args[0]} -> {target} sec after 00:00:00 UTC+0")
                await context.edit(f"Registered: id {mem_id}. You can use this id to cancel the timer.")
                last_sent = 0
                while mem[mem_id] != "":
                    if time.time() % DAY_SECS < target:
                        # 时间没到
                        await asyncio.sleep(2)
                        continue
                    if time.time() % DAY_SECS >= target and time.time() - last_sent < DAY_SECS - 10:
                        # 时间过了,第二天的没到
                        await asyncio.sleep(2)
                        continue
                    if mem[mem_id] != "":
                        await sendmsg(context, chat, args[1])
                        last_sent = time.time()
                mem[mem_id] = ""
                return
            sleep_time = time.time() - dateparser.parse(time_str, settings=settings).timestamp()
            if sleep_time < 5:
                await context.edit(f"Sleep time too short. Should be longer than 5 seconds. Got {sleep_time}") 
                return
            mem[mem_id] = "|".join(args)
            await context.edit(f"Registered: id {mem_id}. You can use this id to cancel the timer.")
            while mem[mem_id] != "":
                last_time = time.time()
                while time.time() < last_time + sleep_time and mem[mem_id] != "":
                    await asyncio.sleep(2)
                if mem[mem_id] != "":
                    await sendmsg(context, chat, args[1])
            mem[mem_id] = ""
            return
        elif args[0].find("*") == 0:
            times = int(args[0][1:].split(" ")[0])
            rest = " ".join(args[0][1:].split(" ")[1:])
            if rest.find(":") != -1:
                # then it should be absolute time
                target = dateparser.parse(rest, settings=settings).timestamp() % DAY_SECS
                if target >= DAY_SECS - 6:
                    # 太接近午夜,小概率直接 sleep 过头,特殊处理
                    target = 0
                count = 0
                mem[mem_id] = "|".join(args)
                await context.edit(f"Registered: id {mem_id}. You can use this id to cancel the timer.")
                last_sent = 0
                while count <= times and mem[mem_id] != "":
                    if time.time() % DAY_SECS < target:
                        # 时间没到
                        await asyncio.sleep(2)
                        continue
                    if time.time() % DAY_SECS >= target and time.time() - last_sent < DAY_SECS - 10:
                        # 时间过了,第二天的没到
                        await asyncio.sleep(2)
                        continue
                    if mem[mem_id] != "":
                        await sendmsg(context, chat, args[1])
                        count += 1
                        last_sent = time.time()
                mem[mem_id] = ""
                return
            sleep_time = time.time() - dateparser.parse(rest, settings=settings).timestamp()
            if sleep_time < 5:
                await context.edit(f"Sleep time too short. Should be longer than 5 seconds. got {sleep_time}")
                return
            count = 0
            mem[mem_id] = "|".join(args)
            await context.edit(f"Registered: id {mem_id}. You can use this id to cancel the timer.")
            while count <= times and mem[mem_id] != "":
                last_time = time.time()
                while time.time() < last_time + sleep_time and mem[mem_id] != "":
                    await asyncio.sleep(2)
                if mem[mem_id] != "":
                    await sendmsg(context, chat, args[1])
                    count += 1
            mem[mem_id] = ""
            return

        if args[0].find(":") == -1:
            # relative time
            dt = dateparser.parse(args[0])
            delta = time.time() - dt.timestamp()
            if delta < 3:
                await context.edit("Target time before now.")
                return
            mem[mem_id] = "|".join(args)
            await context.edit(f"Registered: id {mem_id}. You can use this id to cancel the timer.")
            while dt.timestamp() + 2*delta > time.time() and mem[mem_id] != "":
                await asyncio.sleep(2)
            if mem[mem_id] != "":
                await sendmsg(context, chat, args[1])
            mem[mem_id] = ""
            return

        # absolute time
        target_time = dateparser.parse(args[0], settings=settings).timestamp()
        if target_time - time.time() < 3:
            await context.edit("Target time before now.")
            return
        mem[mem_id] = "|".join(args)

        await context.edit(f"Registered: id {mem_id}. You can use this id to cancel the timer.")
        while target_time - time.time() > 0 and mem[mem_id] != "":
            await asyncio.sleep(2)
        if mem[mem_id] != "":
            await sendmsg(context, chat, args[1])
        mem[mem_id] = ""
    except Exception as e:
        await log(str(e))
        await log(str(traceback.format_stack()))
        return
	def func_wrapper(*args, **kwargs):
		try:
			return func(*args, **kwargs)
		except Exception as e:
			exc_type, exc_obj, exc_tb = sys.exc_info()
			erStr = ("%s ERROR - Pandora PySide2 Setup %s:\n\n%s" % (time.strftime("%d/%m/%y %X"), ''.join(traceback.format_stack()), traceback.format_exc()))
			print erStr
			QMessageBox.warning(QWidget(), "Pandora", erStr)
Beispiel #52
0
    def report_exception(self, msg, notify_user=True, level='error',
                         error_frame=None):
        """
        Report details of an exception to the user.
        This should only be called within an except: block Details of the
        exception are reported eg filename, line number and exception type.

        Because stack trace information outside of py3status or it's modules is
        not helpful in actually finding and fixing the error, we try to locate
        the first place that the exception affected our code.

        Alternatively if the error occurs in a module via a Py3 call that
        catches and reports the error then we receive an error_frame and use
        that as the source of the error.

        NOTE: msg should not end in a '.' for consistency.
        """
        # Get list of paths that our stack trace should be found in.
        py3_paths = [os.path.dirname(__file__)]
        user_paths = self.config.get('include_paths', [])
        py3_paths += [os.path.abspath(path) + '/' for path in user_paths]
        traceback = None

        try:
            # We need to make sure to delete tb even if things go wrong.
            exc_type, exc_obj, tb = sys.exc_info()
            stack = extract_tb(tb)
            error_str = '{}: {}\n'.format(exc_type.__name__, exc_obj)
            traceback = [error_str]

            if error_frame:
                # The error occurred in a py3status module so the traceback
                # should be made to appear correct.  We caught the exception
                # but make it look as though we did not.
                traceback += format_stack(error_frame, 1) + format_tb(tb)
                filename = os.path.basename(error_frame.f_code.co_filename)
                line_no = error_frame.f_lineno
            else:
                # This is a none module based error
                traceback += format_tb(tb)
                # Find first relevant trace in the stack.
                # it should be in py3status or one of it's modules.
                found = False
                for item in reversed(stack):
                    filename = item[0]
                    for path in py3_paths:
                        if filename.startswith(path):
                            # Found a good trace
                            filename = os.path.basename(item[0])
                            line_no = item[1]
                            found = True
                            break
                    if found:
                        break
            # all done!  create our message.
            msg = '{} ({}) {} line {}.'.format(
                msg, exc_type.__name__, filename, line_no)
        except:
            # something went wrong report what we can.
            msg = '{}.'.format(msg)
        finally:
            # delete tb!
            del tb
        # log the exception and notify user
        self.py3_wrapper.log(msg, 'warning')
        if traceback:
            # if debug is not in the config  then we are at an early stage of
            # running py3status and logging is not yet available so output the
            # error to STDERR so it can be seen
            if 'debug' not in self.config:
                print_stderr('\n'.join(traceback))
            elif self.config.get('log_file'):
                self.py3_wrapper.log(''.join(['Traceback\n'] + traceback))
        if notify_user:
            self.py3_wrapper.notify_user(msg, level=level)
Beispiel #53
0
def print_trace(msg):
    print(msg)
    for line in traceback.format_stack():
        print line.strip()
    print("\n\n")
Beispiel #54
0
    def nlog(self, var, l=None, v=True):
        """ new logging, varname and content separate """
        if (not v or not self.mainswitch):
            return

        ### traceback check relevant line and match
        tracelines = traceback.format_stack()
        self.f.write('trace_lnr '+str(self.tr_lnr)+"\n")
        s = tracelines[self.tr_lnr] 
        self.f.write(str("\n".join(tracelines)))
        self.f.write("s: "+str(s))
        m = self.expr.match( s )
        if not m:
            line = "dlog:err eval strack expr # "
            line += var +  "\n"
            self.f.write(line)
            return

        ### check which module calls and if logging should be done
        fn = m.group(1)
        basef = os.path.splitext(fn)[0]
        if basef not in self.modules_tolog: return

        ### variable check
        out = ''
        try:
            if type(var) == type(u''):
                out += var.encode('utf8')
                out += " - (dlog:to UTF8)"
            elif type(var) == type(''):
                out += var
                out += " - (dlog:string)"
            else:
                try:
                    pp = PrettyPrinter()
                    out += pp.pprint( var )
                except:
                    out += str(var)

        except:
            out += " dlog:exception "

        # prefix with label if given
        if l:
            out = l +': \t'+out

        method = m.group(2)
        mth = method.split(" ")
        lnr = mth[1][:-1]
        mout = mth[3]

        tstamp = strftime("%H:%M:%S",localtime())
        llen1 = 9+len(fn) #  
        ltab1 = (6-int( llen1/8+2 ))
        # build log line
        line1 = "\n%s %s %s %s: %s" %(
            tstamp,
            fn,
            "\t"*ltab1,
            lnr,
            mout,
        ) 
        # tab calculation 
        llen = len(lnr+mout)+2+ llen1+ 8*(ltab1-1)
        ltab =(10-int( llen/8 ))

        # for long texts
        out_len = len(out)
        if out_len < 100:
            line1 += "\t"*ltab
            line = line1+out
        elif out_len <200:
            line = "%s\n%s" %(line1, out)
        else:
            line = "%s\n%s" %(line1, out)
            line += "\n"+"_"*90

        ### WRITE TO FILE
        self.f.write( line )
    def _TryEndModal(self, value):

        if not self.isModal(
        ):  # in some rare cases (including spammy AutoHotkey, looks like), this can be fired before the dialog can clean itself up

            return False

        if not self._ReadyToClose(value):

            return False

        if value == QW.QDialog.Rejected:

            if not self._CanCancel():

                return False

            self.SetCancelled(True)

        if value == QW.QDialog.Accepted:

            if not self._CanOK():

                return False

            self._SaveOKPosition()

        self.CleanBeforeDestroy()

        try:

            self.done(value)

        except Exception as e:

            HydrusData.ShowText(
                'This dialog seems to have been unable to close for some reason. I am printing the stack to the log. The dialog may have already closed, or may attempt to close now. Please inform hydrus dev of this situation. I recommend you restart the client if you can. If the UI is locked, you will have to kill it via task manager.'
            )

            HydrusData.PrintException(e)

            import traceback

            HydrusData.DebugPrint(''.join(traceback.format_stack()))

            try:

                self.close()

            except:

                HydrusData.ShowText('The dialog would not close on command.')

            try:

                self.deleteLater()

            except:

                HydrusData.ShowText('The dialog would not destroy on command.')

        return True
Beispiel #56
0
 def __call__(self, *args, **kwargs):
     stacktrace = "".join(traceback.format_stack())
     LOG.error(_LE('No db access allowed in jacket-controller: %s'),
               stacktrace)
     raise exception.DBNotAllowed('jacket-controller')
Beispiel #57
0
def _lazy_call(callable):
    if is_initialized():
        callable()
    else:
        # Don't store the actual traceback to avoid memory cycle
        _queued_calls.append((callable, traceback.format_stack()))
Beispiel #58
0
    def _write_integrated_tool_panel_config_file(self):
        """
        Write the current in-memory version of the integrated_tool_panel.xml file to disk.  Since Galaxy administrators
        use this file to manage the tool panel, we'll not use xml_to_string() since it doesn't write XML quite right.
        """
        destination = os.path.abspath(self._integrated_tool_panel_config)
        tracking_directory = self._integrated_tool_panel_tracking_directory
        if tracking_directory:
            if not os.path.exists(tracking_directory):
                os.makedirs(tracking_directory)
            name = "integrated_tool_panel_%.10f.xml" % time.time()
            filename = os.path.join(tracking_directory, name)
        else:
            filename = destination
        template = string.Template("""<?xml version="1.0"?>
<toolbox>
    <!--
    $INTEGRATED_TOOL_PANEL_DESCRIPTION
    -->
$INTEGRATED_TOOL_PANEL
</toolbox>
""")
        integrated_tool_panel = []
        for key, item_type, item in self._integrated_tool_panel.panel_items_iter(
        ):
            if item:
                if item_type == panel_item_types.TOOL:
                    integrated_tool_panel.append('    <tool id="%s" />\n' %
                                                 item.id)
                elif item_type == panel_item_types.WORKFLOW:
                    integrated_tool_panel.append('    <workflow id="%s" />\n' %
                                                 item.id)
                elif item_type == panel_item_types.LABEL:
                    label_id = item.id or ''
                    label_text = item.text or ''
                    label_version = item.version or ''
                    integrated_tool_panel.append(
                        '    <label id="%s" text="%s" version="%s" />\n' %
                        (label_id, label_text, label_version))
                elif item_type == panel_item_types.SECTION:
                    section_id = item.id or ''
                    section_name = item.name or ''
                    section_version = item.version or ''
                    integrated_tool_panel.append(
                        '    <section id="%s" name="%s" version="%s">\n' %
                        (escape(section_id), escape(section_name),
                         section_version))
                    for section_key, section_item_type, section_item in item.panel_items_iter(
                    ):
                        if section_item_type == panel_item_types.TOOL:
                            if section_item:
                                integrated_tool_panel.append(
                                    '        <tool id="%s" />\n' %
                                    section_item.id)
                        elif section_item_type == panel_item_types.WORKFLOW:
                            if section_item:
                                integrated_tool_panel.append(
                                    '        <workflow id="%s" />\n' %
                                    section_item.id)
                        elif section_item_type == panel_item_types.LABEL:
                            if section_item:
                                label_id = section_item.id or ''
                                label_text = section_item.text or ''
                                label_version = section_item.version or ''
                                integrated_tool_panel.append(
                                    '        <label id="%s" text="%s" version="%s" />\n'
                                    % (label_id, label_text, label_version))
                    integrated_tool_panel.append('    </section>\n')
        tool_panel_description = '\n    '.join(
            [l for l in INTEGRATED_TOOL_PANEL_DESCRIPTION.split("\n") if l])
        tp_string = template.substitute(
            INTEGRATED_TOOL_PANEL_DESCRIPTION=tool_panel_description,
            INTEGRATED_TOOL_PANEL='\n'.join(integrated_tool_panel))
        with RenamedTemporaryFile(filename, mode='w') as f:
            f.write(tp_string)
        if tracking_directory:
            with open(filename + ".stack", "w") as f:
                f.write(''.join(traceback.format_stack()))
            shutil.copy(filename, filename + ".copy")
            shutil.move(filename + ".copy", destination)
        try:
            os.chmod(destination, 0o644)
        except OSError:
            # That can happen if multiple threads are simultaneously moving/chmod'ing this file
            # Should be harmless, though this race condition should be avoided.
            pass
Beispiel #59
0
    def update_history(
            self,
            status_name,
            size,
            task=None,
            tmp_status=None,
            fault=None,
            deploy_fault_message=None,
            deploy_fault_trace=None,
            first_update=False):
        """
        Given the status name and size, look up the previous history object
        If nothing has changed: return (False, last_history)
        else: end date previous history object, start new history object.
              return (True, new_history)
        """
        # FIXME: Move this call so that it happens inside InstanceStatusHistory to avoid circ.dep.
        from core.models import InstanceStatusHistory
        import traceback
        # 1. Get status name
        status_name = _get_status_name_for_provider(
            self.source.provider,
            status_name,
            task,
            tmp_status)
        activity = self.esh_activity()
        status = self.esh_status()
        extra = InstanceStatusHistory._build_extra(
            status_name=status_name,
            fault=fault,
            deploy_fault_message=deploy_fault_message,
            deploy_fault_trace=deploy_fault_trace)

        # 2. Get the last history (or Build a new one if no other exists)
        has_history = self.instancestatushistory_set.all().count()
        if not has_history:
            last_history = InstanceStatusHistory.create_history(
                status_name, self, size,
                start_date=self.start_date,
                activity=activity,
                extra=extra)
            last_history.save()
            logger.debug("STATUSUPDATE - FIRST - Instance:%s Old Status: %s - %s New\
                Status: %s Tmp Status: %s" % (self.provider_alias,
                                              status,
                                              activity,
                                              status_name,
                                              tmp_status))
            logger.debug("STATUSUPDATE - Traceback: %s"
                         % traceback.format_stack())
        last_history = self.get_last_history()
        # 2. Size and name must match to continue using last history
        if last_history.status.name == status_name \
                and last_history.size.id == size.id:
            # logger.info("status_name matches last history:%s " %
            #        last_history.status.name)
            return (False, last_history)
        logger.debug("STATUSUPDATE - Instance:%s Old Status: %s - %s New Status: %s\
            Tmp Status: %s" % (self.provider_alias,
                               status,
                               activity,
                               status_name,
                               tmp_status))
        logger.debug("STATUSUPDATE - Traceback: %s" % traceback.format_stack())
        # 3. ASSERT: A new history item is required due to a State or Size
        # Change
        now_time = timezone.now()
        try:
            new_history = InstanceStatusHistory.transaction(
                status_name, activity, self, size,
                extra=extra,
                start_time=now_time,
                last_history=last_history)
            return (True, new_history)
        except ValueError:
            logger.exception("Bad transaction")
            return (False, last_history)
Beispiel #60
0
def stack_trace() -> str:
    import traceback

    return "".join(traceback.format_stack())