Example #1
0
    def __init__(self, sess, gpcs, objectID, pa, refID):
        try:
            self.__dict__['session'] = weakref.proxy(sess)
        except TypeError:
            self.__dict__['session'] = sess
            sys.exc_clear()

        self.__dict__['objectID'] = objectID
        self.__dict__['sessionCheck'] = None
        self.__dict__['persistantObjectID'] = objectID
        self.__dict__['gpcs'] = weakref.proxy(gpcs)
        self.__dict__['__publicattributes__'] = pa
        self.__dict__['objectChangeHandlers'] = weakref.WeakKeyDictionary({})
        mask = sess.Masquerade()
        try:
            self.__dict__['clisid'] = gpcs.machoNet.GetClientSessionID()
        finally:
            mask.UnMask()

        if objectID[0] == 'C':
            self.__dict__['blocking'] = 0
            cid, oid = objectID.split(':')
            cid = cid.split('=')[1]
            self.__dict__['destination'] = macho.MachoAddress(clientID=long(cid))
        else:
            self.__dict__['blocking'] = 1
            nid, oid = objectID.split(':')
            nid = nid.split('=')[1]
            self.__dict__['destination'] = macho.MachoAddress(nodeID=long(nid))
        self.__dict__['deleted'] = 0
        sess.RegisterMachoObjectConnection(objectID, self, refID)
Example #2
0
 def push(self):
     """Binds the app context to the current context."""
     self._refcnt += 1
     if hasattr(sys, 'exc_clear'):
         sys.exc_clear()
     _app_ctx_stack.push(self)
     appcontext_pushed.send(self.app)
Example #3
0
File: os.py Project: gevent/gevent
    def nb_read(fd, n):
        """
        Read up to *n* bytes from file descriptor *fd*. Return a
        byte string containing the bytes read, which may be shorter than
        *n*. If end-of-file is reached, an empty string is returned.

        The descriptor must be in non-blocking mode.
        """
        hub = None
        event = None
        try:
            while 1:
                try:
                    result = _read(fd, n)
                    return result
                except OSError as e:
                    if e.errno not in ignored_errors:
                        raise
                    if not PY3:
                        sys.exc_clear()
                if hub is None:
                    hub = get_hub()
                    event = hub.loop.io(fd, 1)
                hub.wait(event)
        finally:
            if event is not None:
                event.close()
                event = None
                hub = None
Example #4
0
 def UnRegisterObjectChangeHandler(self, callback):
     try:
         del self.objectChangeHandlers[callback]
     except ReferenceError as e:
         sys.exc_clear()
     except KeyError as e:
         sys.exc_clear()
Example #5
0
 def send(self, data, flags=0, timeout=timeout_default):
     sock = self._sock
     if timeout is timeout_default:
         timeout = self.timeout
     try:
         return sock.send(data, flags)
     except error:
         ex = sys.exc_info()[1]
         if ex.args[0] != EWOULDBLOCK or timeout == 0.0:
             raise
         sys.exc_clear()
         try:
             self._wait(self._write_event)
         except error:
             ex = sys.exc_info()[1]
             if ex.args[0] == EBADF:
                 return 0
             raise
         try:
             return sock.send(data, flags)
         except error:
             ex2 = sys.exc_info()[1]
             if ex2.args[0] == EWOULDBLOCK:
                 return 0
             raise
Example #6
0
    def LoadMotd(self, hidden = False):
        ip = self.serverIP
        try:
            extraParam = WebUtils.GetWebRequestParameters()
            if self.IsChina():
                url = WEB_EVE + '/motd.html?server=%s&%s'
                encoding = 'gbk'
            else:
                url = WEB_EVE + '/motd/%s?%s'
                encoding = 'utf-8'
            ret = corebrowserutil.GetStringFromURL(url % (ip, extraParam)).read()
        except Exception as e:
            log.LogError('Failed to fetch motd', e)
            sys.exc_clear()
            ret = ''

        if self.motdParent and not self.motdParent.destroyed:
            if ret and ret.startswith('MOTD '):
                ret = ret[5:]
                self.motdText = ret.decode(encoding, 'replace')
                if hidden:
                    self.motdParent.state = uiconst.UI_HIDDEN
                else:
                    self.motdParent.state = uiconst.UI_NORMAL
                self.motdLabel.text = self.motdText
                self.motdParent.height = max(32, self.motdLabel.textheight + 10)
            else:
                self.motdParent.state = uiconst.UI_HIDDEN
Example #7
0
File: os.py Project: gevent/gevent
    def nb_write(fd, buf):
        """
        Write some number of bytes from buffer *buf* to file
        descriptor *fd*. Return the number of bytes written, which may
        be less than the length of *buf*.

        The file descriptor must be in non-blocking mode.
        """
        hub = None
        event = None
        try:
            while 1:
                try:
                    result = _write(fd, buf)
                    return result
                except OSError as e:
                    if e.errno not in ignored_errors:
                        raise
                    if not PY3:
                        sys.exc_clear()
                if hub is None:
                    hub = get_hub()
                    event = hub.loop.io(fd, 2)
                hub.wait(event)
        finally:
            if event is not None:
                event.close()
                event = None
                hub = None
Example #8
0
def _extractStuffAndWhy(eventDict):
    """
    Removes all possible *_why*s and *_stuff*s, analyzes exc_info and returns
    a tuple of `(_stuff, _why, eventDict)`.

    **Modifies** *eventDict*!
    """
    _stuff = eventDict.pop("_stuff", None)
    _why = eventDict.pop("_why", None)
    event = eventDict.pop("event", None)
    if isinstance(_stuff, _FAIL_TYPES) and isinstance(event, _FAIL_TYPES):
        raise ValueError("Both _stuff and event contain an Exception/Failure.")
    # `log.err('event', _why='alsoEvent')` is ambiguous.
    if _why and isinstance(event, string_types):
        raise ValueError("Both `_why` and `event` supplied.")
    # Two failures are ambiguous too.
    if not isinstance(_stuff, _FAIL_TYPES) and isinstance(event, _FAIL_TYPES):
        _why = _why or "error"
        _stuff = event
    if isinstance(event, string_types):
        _why = event
    if not _stuff and sys.exc_info() != (None, None, None):
        _stuff = Failure()
    # Either we used the error ourselves or the user supplied one for
    # formatting.  Avoid log.err() to dump another traceback into the log.
    if isinstance(_stuff, BaseException):
        _stuff = Failure(_stuff)
    if PY2:  # pragma: nocover  we don't care about the implicit else
        sys.exc_clear()
    return _stuff, _why, eventDict
Example #9
0
 def __iowait(self, io_func, *args, **kwargs):
     timeout = self._sock.gettimeout() or 0.1
     fd = self._sock.fileno()
     time_start = time.time()
     while True:
         try:
             return io_func(*args, **kwargs)
         except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantX509LookupError):
             sys.exc_clear()
             _, _, errors = select.select([fd], [], [fd], timeout)
             if errors:
                 raise
             time_now = time.time()
             if time_now - time_start > timeout:
                 break
         except OpenSSL.SSL.WantWriteError:
             sys.exc_clear()
             _, _, errors = select.select([], [fd], [fd], timeout)
             if errors:
                 raise
             time_now = time.time()
             if time_now - time_start > timeout:
                 break
         except Exception as e:
             #xlog.exception("e:%r", e)
             raise e
Example #10
0
def run(droid):
	global KEEP_POLLING, POLL_THREAD_LOCK, POLL_THREAD
	scheduleNextRead(droid,POLL_TIME)
	while KEEP_POLLING:
		try:
			POLL_THREAD_LOCK.acquire()
			if not POLL_THREAD.isAlive():
				getProcessWriteResults(droid)
				print "Uh-oh the no longer polling scan results is dead..exitiing"
				break
			POLL_THREAD_LOCK.release()
			time.sleep(3)
		except KeyboardInterrupt:
			print("Exitting the loop.")
			KEEP_POLLING = False
			POLL_THREAD_LOCK.acquire()
			POLL_THREAD.cancel()
			POLL_THREAD_LOCK.release()
			break
		except:
			print ("The following Exception Occurred:")
			traceback.print_exc(file=sys.stdout)
			sys.exc_clear()
			pass	
	POLL_THREAD.join()
Example #11
0
    def _job_store_checker(self):
        while not self._stopped:
            LOG.debug(
                "Starting Scheduler Job Store checker [scheduler=%s]...", self
            )

            try:
                self._process_store_jobs()
            except Exception:
                LOG.exception(
                    "Scheduler failed to process delayed calls"
                    " due to unexpected exception."
                )

                # For some mysterious reason (probably eventlet related)
                # the exception is not cleared from the context automatically.
                # This results in subsequent log.warning calls to show invalid
                # info.
                if sys.version_info < (3,):
                    sys.exc_clear()

            eventlet.sleep(
                self._fixed_delay +
                random.Random().randint(0, self._random_delay * 1000) * 0.001
            )
Example #12
0
    def handle(self, conn, _address): # pylint: disable=method-hidden
        """
        Interact with one remote user.

        .. versionchanged:: 1.1b2 Each connection gets its own
            ``locals`` dictionary. Previously they were shared in a
            potentially unsafe manner.
        """
        fobj = conn.makefile(mode="rw")
        fobj = _fileobject(conn, fobj, self.stderr)
        getcurrent()._fileobj = fobj

        getcurrent().switch_in()
        try:
            console = InteractiveConsole(self._create_interactive_locals())
            if sys.version_info[:3] >= (3, 6, 0):
                # Beginning in 3.6, the console likes to print "now exiting <class>"
                # but probably our socket is already closed, so this just causes problems.
                console.interact(banner=self.banner, exitmsg='') # pylint:disable=unexpected-keyword-arg
            else:
                console.interact(banner=self.banner)
        except SystemExit:  # raised by quit()
            if hasattr(sys, 'exc_clear'): # py2
                sys.exc_clear()
        finally:
            conn.close()
            fobj.close()
Example #13
0
    def close(self, reason = None):
        try:
            self.socket.close()
        except socket.error as e:
            sys.exc_clear()

        self.socket = None
Example #14
0
  def _writeDirections(self, args):
    # defaults
    q = {
   'fname' : 'directions.xml',
      'sz' : '27.1',
      'sa' : '32.6'
    }
    for a in args:
      q[a] = args[a]
    ddata = '''
<DartFile version="5.4.3">
    <Directions exactDate="2" ifCosWeighted="1" numberOfPropagationDirections="100">
        <ExpertModeZone numberOfAngularSector="10" numberOfLayers="0"/>
        <SunViewingAngles sunViewingAzimuthAngle="%s" sunViewingZenithAngle="%s"/>
        <HotSpotProperties hotSpotParallelPlane="0"
            hotSpotPerpendicularPlane="0" oversampleDownwardRegion="0" oversampleUpwardRegion="0"/>
        <AddedDirections directionType="0" ifSquareShape="1" imageDirection="1">
            <ZenithAzimuth directionAzimuthalAngle="207.2" directionZenithalAngle="5.75"/>
            <Square widthDefinition="0">
                <DefineOmega omega="0.0010"/>
            </Square>
        </AddedDirections>
    </Directions>
</DartFile>
'''[1:-1] % (q['sa'], q['sz'])
    try:
      VLAB.copyfile(q['fname'], '%s_%s' % (q['fname'], time.time()))
    except Exception:
      if not sys.platform.startswith('java'):
        sys.exc_clear()
    fp = open(q['fname'], 'w')
    fp.write(ddata)
    fp.close()
Example #15
0
  def _writePhase(self, args):
    # defaults
    q = {
       'fname' : 'phase.xml',
      'sensor' : 'MSI',
       'bands' : '1, 2, 3, 4, 5, 6, 7, 8, 9, 10'
    }
    for a in args:
      q[a] = args[a]
    pdata = '''
<?xml version="1.0" encoding="UTF-8"?>
<DartFile version="5.4.3">
    <Phase expertMode="0">
        <DartInputParameters calculatorMethod="0">
            <nodefluxtracking gaussSiedelAcceleratingTechnique="1" numberOfIteration="4"/>
            <SpectralDomainTir temperatureMode="0">
                <Atmosphere_1 SKYLForTemperatureAssignation="0.0"/>
            </SpectralDomainTir>
            <SpectralIntervals>
                <SpectralIntervalsProperties bandNumber="0"
                    deltaLambda="0.005493" meanLambda="0.56925"
                    radiativeBudgetProducts="0" spectralDartMode="0"/>
            </SpectralIntervals>
            <ExpertModeZone albedoThreshold="1.0E-5"
                illuminationRepartitionMode="2"
                lightPropagationThreshold="1.0E-4"
                nbRandomPointsPerInteceptionAtmosphere="10"
                nbSubSubcenterTurbidEmission="40"
                nbSubcenterIllumination="10" nbSubcenterVolume="2"/>
            <nodeIlluminationMode illuminationMode="0" irradianceMode="1">
                <irradianceDatabaseNode irradianceDatabase="TOASolarIrradiance.txt"/>
            </nodeIlluminationMode>
        </DartInputParameters>
        <DartProduct>
            <dartModuleProducts allIterationsProducts="0"
                brfProducts="1" lidarProducts="0"
                order1Products="0" radiativeBudgetProducts="0">
                <BrfProductsProperties brfProduct="1" extrapolation="1"
                    horizontalOversampling="1" image="1"
                    luminanceProducts="1" maximalThetaImages="0.1"
                    nb_scene="1" outputHapkeFile="0" projection="1"
                    sensorOversampling="1" sensorPlaneprojection="0">
                    <ExpertModeZone_Etalement etalement="2"/>
                    <ExpertModeZone_maskProjection mask_projection="0"/>
                </BrfProductsProperties>
            </dartModuleProducts>
            <maketModuleProducts MNEProducts="0" areaMaketProducts="0"
                coverRateProducts="0" laiProducts="0"/>
        </DartProduct>
    </Phase>
</DartFile>
'''[1:-1] % ()
    try:
      VLAB.copyfile(q['fname'], '%s_%s' % (q['fname'], time.time()))
    except Exception:
      if not sys.platform.startswith('java'):
        sys.exc_clear()
    fp = open(q['fname'], 'w')
    fp.write(pdata)
    fp.close()
Example #16
0
    def print_debug_header(self):
        if not self.params.get('verbose'):
            return
        write_string(u'[debug] youtube-dl version ' + __version__ + u'\n')
        try:
            sp = subprocess.Popen(
                ['git', 'rev-parse', '--short', 'HEAD'],
                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                cwd=os.path.dirname(os.path.abspath(__file__)))
            out, err = sp.communicate()
            out = out.decode().strip()
            if re.match('[0-9a-f]+', out):
                write_string(u'[debug] Git HEAD: ' + out + u'\n')
        except:
            try:
                sys.exc_clear()
            except:
                pass
        write_string(u'[debug] Python version %s - %s' %
                     (platform.python_version(), platform_name()) + u'\n')

        proxy_map = {}
        for handler in self._opener.handlers:
            if hasattr(handler, 'proxies'):
                proxy_map.update(handler.proxies)
        write_string(u'[debug] Proxy map: ' + compat_str(proxy_map) + u'\n')
Example #17
0
    def _request(self, url, data=None, referrer='http://google.com/', ajax=False):
        handlers = [self.cookie_handler]
        if self.proxy:
            handlers.append(self.proxy)
        opener = urllib.request.build_opener(*handlers)
        self._add_headers(opener, referrer, ajax)

        html = ''
        try:
            req = urllib.request.Request(url, data)
            res = opener.open(req, timeout=30)
            html = res.read()
        except Exception as e:
            print("Something went terribly wrong {e}".format(e=e))
            return False, {}, {}

        headers = res.info()
        if ('Content-Encoding' in list(headers.keys()) and headers['Content-Encoding'] == 'gzip') or \
                ('content-encoding' in list(headers.keys()) and headers['content-encoding'] == 'gzip'):
            data = io.BytesIO(html)
            gzipper = gzip.GzipFile(fileobj=data)
            try:
                html_unzipped = gzipper.read()
            except Exception:
                sys.exc_clear()
            else:
                html = html_unzipped

        cookies = {cookie.name: cookie.value for cookie in self.cookie_jar}
        self.csrf_token = cookies['csrftoken']

        return html, headers, cookies
    def SendInfoWorker(self):
        while self.isEnabled:
            try:
                eventsToSend = copy.deepcopy(self.loggedEvents)
                self.loggedEvents = {}
                if len(eventsToSend):
                    self.LogInfo('Information Gatherer is shipping collected data over the wire... Interval = %d milliseconds.' % self.clientWorkerInterval)
                    ret = sm.RemoteSvc('infoGatheringMgr').LogInfoEventsFromClient(eventsToSend)
                    for eventTypeID, status in ret.iteritems():
                        if status == const.INFOSERVICE_OFFLINE:
                            self.isEnabled = 0
                            break
                        elif status == const.INFOTYPE_OFFLINE:
                            self.serverEvents.remove(eventTypeID)
                            self.infoTypeParameters.remove(eventTypeID)
                            if eventTypeID in self.serverOncePerRunEvents:
                                self.serverOncePerRunEvents.remove(eventTypeID)
                            if eventTypeID in self.logTypeAggregates:
                                self.logTypeAggregates.remove(eventTypeID)

            except Exception:
                log.LogException('Error while shipping data to server...')
                sys.exc_clear()

            blue.pyos.synchro.SleepWallclock(self.clientWorkerInterval)
 def setUp(self):
     sys.exc_clear()
     self.driver = get_driver(Settings.site_url)
     self.project_exists = False
     self.provider_exists = False
     self.resource_exists = False
     self.driver.implicitly_wait(BaseSettings.implicitly_wait)
Example #20
0
 def recv_into(self, buffer, nbytes=None, flags=0):
     if buffer and (nbytes is None):
         nbytes = len(buffer)
     elif nbytes is None:
         nbytes = 1024
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to recv_into() on %s" %
                 self.__class__)
         while True:
             try:
                 tmp_buffer = self.read(nbytes)
                 v = len(tmp_buffer)
                 buffer[:v] = tmp_buffer
                 return v
             except SSLError as x:
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     if self.timeout == 0.0:
                         raise
                     sys.exc_clear()
                     self._wait(self._read_event)
                     continue
                 else:
                     raise
     else:
         return socket.recv_into(self, buffer, nbytes, flags)
Example #21
0
    def accept(self):
        """Accepts a new connection from a remote client, and returns
        a tuple containing that new connection wrapped with a server-side
        SSL channel, and the address of the remote client."""
        sock = self._sock
        while True:
            try:
                client_socket, address = sock.accept()
                break
            except socket_error as ex:
                if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0:
                    raise
                sys.exc_clear()
            self._wait(self._read_event)

        sslobj = SSLSocket(client_socket,
                           keyfile=self.keyfile,
                           certfile=self.certfile,
                           server_side=True,
                           cert_reqs=self.cert_reqs,
                           ssl_version=self.ssl_version,
                           ca_certs=self.ca_certs,
                           do_handshake_on_connect=self.do_handshake_on_connect,
                           suppress_ragged_eofs=self.suppress_ragged_eofs,
                           server_hostname=self.server_hostname,
                           ciphers=self.ciphers)

        return sslobj, address
Example #22
0
    def iter_messages(self):
        """ Yields tuples of (watcher, subtopic, stat)"""
        recv = self.pubsub_socket.recv_multipart
        with self:
            while True:
                try:
                    events = dict(self.poller.poll(self.timeout * 1000))
                except zmq.ZMQError as e:
                    if e.errno == errno.EINTR:
                        continue
                    raise

                if len(events) == 0:
                    continue

                try:
                    topic, stat = recv()
                except zmq.core.error.ZMQError as e:
                    if e.errno != errno.EINTR:
                        raise
                    else:
                        try:
                            sys.exc_clear()
                        except Exception:
                            pass
                        continue

                topic = s(topic).split('.')
                if len(topic) == 3:
                    __, watcher, subtopic = topic
                    yield watcher, subtopic, json.loads(stat)
                elif len(topic) == 2:
                    __, watcher = topic
                    yield watcher, None, json.loads(stat)
    def Reprocess(self, items, activeShipID):
        fromLocation = self._GetFromLocationID(items)
        itemIDs = []
        try:
            for item in items:
                itemID = item.itemID
                self._CheckIsRefiningShip(activeShipID, itemID)
                if not self._IsInSameLocation(fromLocation, item):
                    continue
                self._LockItem(item, itemIDs)

            try:
                if len(itemIDs):
                    ownerID, flag = session.charid, invconst.flagHangar
                    skipChecks = []
                    while True:
                        try:
                            self._DoReprocess(itemIDs, ownerID, fromLocation, flag, skipChecks)
                        except UserError as e:
                            sys.exc_clear()
                            if self.AskToContinue(e):
                                skipChecks.append(e.msg)
                                continue

                        break

            except:
                sys.exc_clear()

        finally:
            self._UnlockItems(itemIDs)
Example #24
0
	def run(self):
		self.logger.info("Starting Snort alert processor main thread...")
		
		try:
			os.setuid(int(self.options.uid))
		except OSError:
			self.logger.error("Could not change user for Snort alert processor")
			return
		
		try:
			os.remove(self.options.unsock)
		except OSError:
			pass
		
		self.queue_processor.start()
		
		self.logger.debug("Binding UNIX socket for Snort alerts at " + self.options.unsock)
		
		try:
			self.server = SnortAlertServer(self.logger, self.options.unsock, SnortAlertHandler)
			self.server.serve_forever()
		except:
			self.logger.error("Could not start Snort processor (detailed log message follows):")
			self.logger.error(sys.exc_info())
			sys.exc_clear()
			self.stop()
Example #25
0
def NameValueListToDict(name_value_list):
  """
  Takes an array of strings of the form 'NAME=VALUE' and creates a dictionary
  of the pairs. If a string is simply NAME, then the value in the dictionary
  is set to True. If VALUE can be converted to a boolean or integer, it is.
  """
  result = {}
  for item in name_value_list:
    tokens = item.split('=', 1)
    if len(tokens) == 2:
      token_value = tokens[1]
      if token_value.lower() == 'true':
        token_value = True
      elif token_value.lower() == 'false':
        token_value = False
      else:
        # If we can make it an int, use that, otherwise, use the string.
        try:
          token_value = int(token_value)
        except ValueError:
          sys.exc_clear()
      # Set the variable to the supplied value.
      result[tokens[0]] = token_value
    else:
      # No value supplied, treat it as a boolean and set it.
      result[tokens[0]] = True
  return result
  def testMemoryIsFreed(self):
    # Note: we use `set` values for components and metadata because we need
    # to construct weakrefs to them.  Other builtin types, such as `list` and
    # `tuple`, do not support weakrefs.
    ct1 = CT(set([1, 2]), set(['no', 'leaks']))
    ct2 = CT(set([3, 4]), set(['no', 'leaks']))
    ct3 = CT(set([5, 6]), set(['other', 'metadata']))

    # Note: map_structure exercises flatten, pack_sequence_as, and
    # assert_same_structure.
    func = lambda x, y: x | y
    ct4 = nest.map_structure(func, ct1, ct2, expand_composites=True)

    # Check that the exception-raising path in assert_same_structure
    # doesn't leak any objects.
    with self.assertRaisesRegexp(ValueError,
                                 ".*don't have the same nested structure.*"):
      nest.map_structure(func, ct2, ct3, expand_composites=True)
    if hasattr(sys, 'exc_clear'):
      sys.exc_clear()  # Remove any references in exception stack traces.

    refs = []
    for ct in [ct1, ct2, ct3, ct4]:
      refs.append(weakref.ref(ct))
      refs.append(weakref.ref(ct.components))
      refs.append(weakref.ref(ct.metadata))
    del ct  # pylint: disable=undefined-loop-variable

    for ref in refs:
      self.assertIsNotNone(ref())

    del ct1, ct2, ct3, ct4
    gc.collect()
    for ref in refs:
      self.assertIsNone(ref())
Example #27
0
 def send(self,raw_data,retry_timeout=1):
     """ Writes raw outgoing data. Blocks until done.
         If supplied data is unicode string, encodes it to utf-8 before send."""
     if type(raw_data)==type(u''): raw_data = raw_data.encode('utf-8')
     elif type(raw_data)<>type(''): raw_data = ustr(raw_data).encode('utf-8')
     try:
         sent = 0
         while not sent:
             try:
                 self._send(raw_data)
                 sent = 1
             except socket.sslerror, e:
                 if e[0]==socket.SSL_ERROR_WANT_READ:
                     sys.exc_clear()
                     self.DEBUG("SSL_WANT_READ while sending data, wating to retry",'warn')
                     select.select([self._sock],[],[],retry_timeout)
                     continue
                 if e[0]==socket.SSL_ERROR_WANT_WRITE:
                     sys.exc_clear()
                     self.DEBUG("SSL_WANT_WRITE while sending data, waiting to retry",'warn')
                     select.select([],[self._sock],[],retry_timeout)
                     continue
                 raise
         # Avoid printing messages that are empty keepalive packets.
         if raw_data.strip():
             self.DEBUG(raw_data,'sent')
             if hasattr(self._owner, 'Dispatcher'): # HTTPPROXYsocket will send data before we have a Dispatcher
                 self._owner.Dispatcher.Event('', DATA_SENT, raw_data)
Example #28
0
def _actual_start():
  """Another layer in the starting stack."""
  # Get raw traceback
  tb = None
  try:
    raise ZeroDivisionError('')
  except ZeroDivisionError:
    tb = sys.exc_info()[2]
  assert tb

  # Look at previous stack frame's previous stack frame (previous
  # frame is run() or start(); the frame before that should be the
  # frame of the original caller, which should be __main__ or appcommands
  prev_prev_frame = tb.tb_frame.f_back.f_back
  if not prev_prev_frame:
    return
  prev_prev_name = prev_prev_frame.f_globals.get('__name__', None)
  if (prev_prev_name != '__main__'
      and not prev_prev_name.endswith('.appcommands')):
    return
  # just in case there's non-trivial stuff happening in __main__
  del tb
  sys.exc_clear()

  try:
    really_start()
  except SystemExit, e:
    raise
def _chunk_read(response, chunk_size=32768, report_hook=None, filename=None):
    file_data = []
    if response.info().has_key('Content-Length'):
        total_size = response.info().getheader('Content-Length').strip()
        total_size = int(total_size)
    else:
        # No size
        total_size = None
        if report_hook:
            print '* Warning: No total file size available.'
    if (filename == None) and (response.info().has_key('Content-Disposition')):
        # If the response has Content-Disposition, we take file name from it
        try:
            filename = response.info()['Content-Disposition'].split('filename=')[1]
            if filename[0] == '"' or filename[0] == "'":
                filename = filename[1:-1]
        except Exception:
            sys.exc_clear()
            filename = 'output'
    if (filename == None):
        if report_hook:
            print "* No detected filename, using 'output'"
        filename = 'output'
    bytes_so_far = 0
    while True:
        chunk = response.read(chunk_size)
        bytes_so_far += len(chunk)
        if not chunk:
            break
        else:
            file_data.append(chunk)
        report_hook(bytes_so_far, chunk_size, total_size)
    return (file_data, filename)
Example #30
0
 def send(self, data, flags=0, timeout=timeout_default):
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to send() on %s" %
                 self.__class__)
         while True:
             try:
                 v = self._sslobj.write(data)
             except SSLError as x:
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     if self.timeout == 0.0:
                         return 0
                     sys.exc_clear()
                     self._wait(self._read_event)
                 elif x.args[0] == SSL_ERROR_WANT_WRITE:
                     if self.timeout == 0.0:
                         return 0
                     sys.exc_clear()
                     self._wait(self._write_event)
                 else:
                     raise
             else:
                 return v
     else:
         return socket.send(self, data, flags, timeout)
def clearException():
    try:  # python 2 needs manual clear of exception information after the exception handler
        sys.exc_clear()
    except Exception:  # python 3 removed this function
        pass
Example #32
0
#!C:\Users\xcpro\ve1\Scripts\python.exe
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
import sys

try:
    import _preamble
except ImportError:
    sys.exc_clear()

from twisted.scripts.htmlizer import run
run()
Example #33
0
        def wrapper(*args, **kwargs):
            tries_left = 3
            while tries_left:
                # clear exception state so our re-raise can't reraise something
                # old. This isn't strictly necessary, but feels safer.
                sys.exc_clear()
                try:
                    if not self.transport.isOpen():
                        self.transport.open()
                    st = time.time()

                    str_args = _unpack_guid_secret_in_handle(repr(args))
                    logging.debug("Thrift call: %s.%s(args=%s, kwargs=%s)" %
                                  (str(self.wrapped.__class__), attr, str_args,
                                   repr(kwargs)))

                    ret = res(*args, **kwargs)

                    if ENABLE_SMART_THRIFT_POOL.get(
                    ) and 'OpenSession' == attr and 'http_addr' in repr(ret):
                        coordinator_host = re.search(
                            'http_addr\':\ \'(.*:[0-9]{2,})\', \'', repr(ret))
                        self.coordinator_host = coordinator_host.group(1)

                    log_msg = _unpack_guid_secret_in_handle(repr(ret))

                    # Truncate log message, increase output in DEBUG mode
                    log_limit = 2000 if settings.DEBUG else 1000
                    log_msg = log_msg[:log_limit] + (log_msg[log_limit:]
                                                     and '...')

                    duration = time.time() - st

                    # Log the duration at different levels, depending on how long it took.
                    logmsg = "Thrift call: %s.%s(args=%s, kwargs=%s) returned in %dms: %s" % (
                        str(self.wrapped.__class__), attr, str_args,
                        repr(kwargs), duration * 1000, log_msg)
                    log_if_slow_call(duration=duration, message=logmsg)

                    return ret
                except socket.error as e:
                    pass
                except TTransportException as e:
                    pass
                except Exception as e:
                    logging.exception(
                        "Thrift saw exception (this may be expected).")
                    raise

                self.transport.close()

                if isinstance(
                        e,
                        socket.timeout) or 'read operation timed out' in str(
                            e):  # Can come from ssl.SSLError
                    logging.warn(
                        "Not retrying thrift call %s due to socket timeout" %
                        attr)
                    raise
                else:
                    tries_left -= 1
                    if tries_left:
                        logging.info("Thrift exception; retrying: " + str(e),
                                     exc_info=0)
                        if 'generic failure: Unable to find a callback: 32775' in str(
                                e):
                            logging.warn(
                                "Increase the sasl_max_buffer value in hue.ini"
                            )
            logging.warn("Out of retries for thrift call: " + attr)
            raise
Example #34
0
def run_script(script_path, fabric_env=None, process=None, **kwargs):

    if not process:
        process = {}
    process = _create_process_config(process, kwargs)
    base_dir = process.get('base_dir', DEFAULT_BASE_DIR)
    ctx_server_port = process.get('ctx_server_port')

    proxy_client_path = proxy_client.__file__
    if proxy_client_path.endswith('.pyc'):
        proxy_client_path = proxy_client_path[:-1]
    local_ctx_sh_path = os.path.join(_get_bin_dir(), 'ctx-sh')
    local_ctx_py_path = os.path.join(
        os.path.dirname(cloudify.ctx_wrappers.__file__), 'ctx-py.py')
    local_script_path = get_script(ctx.download_resource, script_path)
    base_script_path = os.path.basename(local_script_path)
    remote_ctx_dir = base_dir
    remote_ctx_path = '{0}/ctx'.format(remote_ctx_dir)
    remote_ctx_sh_path = '{0}/ctx-sh'.format(remote_ctx_dir)
    remote_ctx_py_path = '{0}/cloudify.py'.format(remote_ctx_dir)
    remote_scripts_dir = '{0}/scripts'.format(remote_ctx_dir)
    remote_work_dir = '{0}/work'.format(remote_ctx_dir)
    remote_path_suffix = '{0}-{1}'.format(base_script_path,
                                          utils.id_generator(size=8))
    remote_env_script_path = '{0}/env-{1}'.format(remote_scripts_dir,
                                                  remote_path_suffix)
    remote_script_path = '{0}/{1}'.format(remote_scripts_dir,
                                          remote_path_suffix)

    env = process.get('env', {})
    cwd = process.get('cwd', remote_work_dir)
    args = process.get('args')
    command_prefix = process.get('command_prefix')

    command = remote_script_path
    if command_prefix:
        command = '{0} {1}'.format(command_prefix, command)
    if args:
        command = ' '.join([command] + args)

    with fabric_api.settings(**_fabric_env(fabric_env, warn_only=False)):
        # the remote host must have ctx and any related files before
        # running any fabric scripts
        if not fabric_files.exists(remote_ctx_path):

            # there may be race conditions with other operations that
            # may be running in parallel, so we pass -p to make sure
            # we get 0 exit code if the directory already exists
            fabric_api.run('mkdir -p {0}'.format(remote_scripts_dir))
            fabric_api.run('mkdir -p {0}'.format(remote_work_dir))
            # this file has to be present before using ctx
            fabric_api.put(local_ctx_sh_path, remote_ctx_sh_path)
            fabric_api.put(proxy_client_path, remote_ctx_path)
            fabric_api.put(local_ctx_py_path, remote_ctx_py_path)

        actual_ctx = ctx._get_current_object()

        actual_ctx.is_script_exception_defined = ScriptException is not None

        def abort_operation(message=None):
            if actual_ctx._return_value is not None:
                actual_ctx._return_value = ILLEGAL_CTX_OPERATION_ERROR
                raise actual_ctx._return_value
            if actual_ctx.is_script_exception_defined:
                actual_ctx._return_value = ScriptException(message)
            else:
                actual_ctx._return_value = UNSUPPORTED_SCRIPT_FEATURE_ERROR
                raise actual_ctx
            return actual_ctx._return_value

        def retry_operation(message=None, retry_after=None):
            if actual_ctx._return_value is not None:
                actual_ctx._return_value = ILLEGAL_CTX_OPERATION_ERROR
                raise actual_ctx._return_value
            actual_ctx.operation.retry(message=message,
                                       retry_after=retry_after)
            if actual_ctx.is_script_exception_defined:
                actual_ctx._return_value = ScriptException(message, retry=True)
            else:
                actual_ctx._return_value = UNSUPPORTED_SCRIPT_FEATURE_ERROR
                raise actual_ctx._return_value
            return actual_ctx._return_value

        actual_ctx.abort_operation = abort_operation
        actual_ctx.retry_operation = retry_operation

        def returns(_value):
            if actual_ctx._return_value is not None:
                actual_ctx._return_value = ILLEGAL_CTX_OPERATION_ERROR
                raise actual_ctx._return_value
            actual_ctx._return_value = _value

        actual_ctx.returns = returns

        actual_ctx._return_value = None
        original_download_resource = actual_ctx.download_resource

        def download_resource(resource_path, target_path=None):
            local_target_path = original_download_resource(resource_path)
            return fabric_put_in_remote_path(local_target_path, target_path)

        actual_ctx.download_resource = download_resource

        original_download_resource_and_render = \
            actual_ctx.download_resource_and_render

        def download_resource_and_render(resource_path,
                                         target_path=None,
                                         template_variables=None):
            local_target_path = original_download_resource_and_render(
                resource_path, template_variables=template_variables)

            return fabric_put_in_remote_path(local_target_path, target_path)

        actual_ctx.download_resource_and_render = download_resource_and_render

        def fabric_put_in_remote_path(local_target_path, target_path):
            if target_path:
                remote_target_path = target_path
            else:
                remote_target_path = '{0}/{1}'.format(
                    remote_work_dir, os.path.basename(local_target_path))
            fabric_api.put(local_target_path, remote_target_path)
            return remote_target_path

        def handle_script_result(script_result):
            if (actual_ctx.is_script_exception_defined
                    and isinstance(script_result, ScriptException)):
                if script_result.retry:
                    return script_result
                else:
                    raise NonRecoverableError(str(script_result))
            # this happens when more than 1 ctx operation is invoked or
            # the plugin runs an unsupported feature on older Cloudify
            elif isinstance(script_result, RuntimeError):
                raise NonRecoverableError(str(script_result))
            # determine if this code runs during exception handling
            current_exception = sys.exc_info()[1]
            if current_exception:
                raise
            else:
                return script_result

        env_script = StringIO()
        env['PATH'] = '{0}:$PATH'.format(remote_ctx_dir)
        env['PYTHONPATH'] = '{0}:$PYTHONPATH'.format(remote_ctx_dir)
        env_script.write('chmod +x {0}\n'.format(remote_script_path))
        env_script.write('chmod +x {0}\n'.format(remote_ctx_path))
        fabric_api.put(local_script_path, remote_script_path)
        proxy = proxy_server.HTTPCtxProxy(actual_ctx, port=ctx_server_port)
        try:
            with fabric_context.cd(cwd):
                local_port = proxy.port
                with tunnel.remote(local_port=local_port) as remote_port:
                    env[CTX_SOCKET_URL] = proxy.socket_url.replace(
                        str(local_port), str(remote_port))
                    env['LOCAL_{0}'.format(CTX_SOCKET_URL)] = proxy.socket_url
                    for key, value in env.iteritems():
                        env_script.write('export {0}={1}\n'.format(key, value))
                    fabric_api.put(env_script, remote_env_script_path)
                    # invoke sys.exc_clear() because handle_script_result
                    # invokes sys.exc_info()
                    sys.exc_clear()
                    try:
                        fabric_api.run('source {0} && {1}'.format(
                            remote_env_script_path, command))
                    except FabricTaskError:
                        return handle_script_result(actual_ctx._return_value)
            return handle_script_result(actual_ctx._return_value)
        finally:
            proxy.close()
Example #35
0
    def __setattr__(self, name, value):
        if name == "client_args":
            raise AttributeError("'history' object attribute '{0}' "
                                 "is read-only.".format(name))

        if not name.startswith("operation_"):
            return object.__setattr__(self, name, value)

        ops = object.__getattribute__(self, "_History__operations")
        if name == "operation_name":
            if not ops:
                ops = []
                object.__setattr__(self, "_History__operations", ops)

            ops.append({"pathname": None, "operation": _HistoryOperation()})
        elif not ops:
            raise AttributeError(
                "'history' object attribute '{0}' "
                "cannot be set before 'operation_name'.".format(name))

        op = ops[-1]["operation"]
        setattr(op, name[len("operation_"):], value)

        # Access to the class attributes is done through object instead
        # of just referencing self to avoid any of the special logic in
        # place interfering with logic here.
        if name == "operation_name":
            # Before a new operation starts, clear exception state
            # for the current one so that when this one ends, the
            # last operation's exception won't be recorded to this
            # one.  If the error hasn't been recorded by now, it
            # doesn't matter anyway, so should be safe to clear.
            # sys.exc_clear() isn't supported in Python 3, and
            # couldn't find a replacement.
            try:
                sys.exc_clear()
            except:
                pass

            # Mark the operation as having started and record
            # other, relevant information.
            op.start_time = misc.time_to_timestamp(None)
            try:
                op.username = portable.get_username()
            except KeyError:
                op.username = "******"
            op.userid = portable.get_userid()

            ca = None
            if sys.argv[0]:
                ca = [sys.argv[0]]
            else:
                # Fallback for clients that provide no value.
                ca = [self.client_name]

            ca.extend(sys.argv[1:])
            object.__setattr__(self, "client_args", ca)
            object.__setattr__(self, "client_version", pkg.VERSION)

        elif name == "operation_result":
            # Record when the operation ended.
            op.end_time = misc.time_to_timestamp(None)

            # Some operations shouldn't be saved -- they're merely
            # included in the stack for completeness or to support
            # client functionality.
            if op.name not in DISCARDED_OPERATIONS and \
                value != RESULT_NOTHING_TO_DO:
                # Write current history and last operation to a
                # file.
                self.__save()

            # Discard it now that it is no longer needed.
            del ops[-1]
 def __exit__(self, *args):
     if hasattr(sys, "exc_clear"):
         # Python 3 (PyPy3) doesn't have exc_clear
         sys.exc_clear()
Example #37
0
def main():
    argument_spec = url_argument_spec()
    argument_spec.update(
        dest=dict(type='path'),
        url_username=dict(type='str', aliases=['user']),
        url_password=dict(type='str', aliases=['password'], no_log=True),
        body=dict(type='raw'),
        body_format=dict(
            type='str',
            default='raw',
            choices=['form-urlencoded', 'json', 'raw', 'form-multipart']),
        src=dict(type='path'),
        method=dict(type='str', default='GET'),
        return_content=dict(type='bool', default=False),
        follow_redirects=dict(
            type='str',
            default='safe',
            choices=['all', 'no', 'none', 'safe', 'urllib2', 'yes']),
        creates=dict(type='path'),
        removes=dict(type='path'),
        status_code=dict(type='list', elements='int', default=[200]),
        timeout=dict(type='int', default=30),
        headers=dict(type='dict', default={}),
        unix_socket=dict(type='path'),
        remote_src=dict(type='bool', default=False),
        ca_path=dict(type='path', default=None),
        unredirected_headers=dict(type='list', elements='str', default=[]),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        add_file_common_args=True,
        mutually_exclusive=[['body', 'src']],
    )

    url = module.params['url']
    body = module.params['body']
    body_format = module.params['body_format'].lower()
    method = module.params['method'].upper()
    dest = module.params['dest']
    return_content = module.params['return_content']
    creates = module.params['creates']
    removes = module.params['removes']
    status_code = [int(x) for x in list(module.params['status_code'])]
    socket_timeout = module.params['timeout']
    ca_path = module.params['ca_path']
    dict_headers = module.params['headers']
    unredirected_headers = module.params['unredirected_headers']

    if not re.match('^[A-Z]+$', method):
        module.fail_json(
            msg=
            "Parameter 'method' needs to be a single word in uppercase, like GET or POST."
        )

    if body_format == 'json':
        # Encode the body unless its a string, then assume it is pre-formatted JSON
        if not isinstance(body, string_types):
            body = json.dumps(body)
        if 'content-type' not in [header.lower() for header in dict_headers]:
            dict_headers['Content-Type'] = 'application/json'
    elif body_format == 'form-urlencoded':
        if not isinstance(body, string_types):
            try:
                body = form_urlencoded(body)
            except ValueError as e:
                module.fail_json(
                    msg='failed to parse body as form_urlencoded: %s' %
                    to_native(e),
                    elapsed=0)
        if 'content-type' not in [header.lower() for header in dict_headers]:
            dict_headers['Content-Type'] = 'application/x-www-form-urlencoded'
    elif body_format == 'form-multipart':
        try:
            content_type, body = prepare_multipart(body)
        except (TypeError, ValueError) as e:
            module.fail_json(msg='failed to parse body as form-multipart: %s' %
                             to_native(e))
        dict_headers['Content-Type'] = content_type

    if creates is not None:
        # do not run the command if the line contains creates=filename
        # and the filename already exists.  This allows idempotence
        # of uri executions.
        if os.path.exists(creates):
            module.exit_json(stdout="skipped, since '%s' exists" % creates,
                             changed=False)

    if removes is not None:
        # do not run the command if the line contains removes=filename
        # and the filename does not exist.  This allows idempotence
        # of uri executions.
        if not os.path.exists(removes):
            module.exit_json(stdout="skipped, since '%s' does not exist" %
                             removes,
                             changed=False)

    # Make the request
    start = datetime.datetime.utcnow()
    r, info = uri(module, url, dest, body, body_format, method, dict_headers,
                  socket_timeout, ca_path, unredirected_headers)

    elapsed = (datetime.datetime.utcnow() - start).seconds

    if r and dest is not None and os.path.isdir(dest):
        filename = get_response_filename(r) or 'index.html'
        dest = os.path.join(dest, filename)

    if r and r.fp is not None:
        # r may be None for some errors
        # r.fp may be None depending on the error, which means there are no headers either
        content_type, main_type, sub_type, content_encoding = parse_content_type(
            r)
    else:
        content_type = 'application/octet-stream'
        main_type = 'aplication'
        sub_type = 'octet-stream'
        content_encoding = 'utf-8'

    maybe_json = content_type and any(candidate in sub_type
                                      for candidate in JSON_CANDIDATES)
    maybe_output = maybe_json or return_content or info[
        'status'] not in status_code

    if maybe_output:
        try:
            if PY3 and (r.fp is None or r.closed):
                raise TypeError
            content = r.read()
        except (AttributeError, TypeError):
            # there was no content, but the error read()
            # may have been stored in the info as 'body'
            content = info.pop('body', b'')
    elif r:
        content = r
    else:
        content = None

    resp = {}
    resp['redirected'] = info['url'] != url
    resp.update(info)

    resp['elapsed'] = elapsed
    resp['status'] = int(resp['status'])
    resp['changed'] = False

    # Write the file out if requested
    if r and dest is not None:
        if resp['status'] in status_code and resp['status'] != 304:
            write_file(module, dest, content, resp)
            # allow file attribute changes
            resp['changed'] = True
            module.params['path'] = dest
            file_args = module.load_file_common_arguments(module.params,
                                                          path=dest)
            resp['changed'] = module.set_fs_attributes_if_different(
                file_args, resp['changed'])
        resp['path'] = dest

    # Transmogrify the headers, replacing '-' with '_', since variables don't
    # work with dashes.
    # In python3, the headers are title cased.  Lowercase them to be
    # compatible with the python2 behaviour.
    uresp = {}
    for key, value in iteritems(resp):
        ukey = key.replace("-", "_").lower()
        uresp[ukey] = value

    if 'location' in uresp:
        uresp['location'] = absolute_location(url, uresp['location'])

    # Default content_encoding to try
    if isinstance(content, binary_type):
        u_content = to_text(content, encoding=content_encoding)
        if maybe_json:
            try:
                js = json.loads(u_content)
                uresp['json'] = js
            except Exception:
                if PY2:
                    sys.exc_clear(
                    )  # Avoid false positive traceback in fail_json() on Python 2
    else:
        u_content = None

    if module.no_log_values:
        uresp = sanitize_keys(uresp, module.no_log_values, NO_MODIFY_KEYS)

    if resp['status'] not in status_code:
        uresp['msg'] = 'Status code was %s and not %s: %s' % (
            resp['status'], status_code, uresp.get('msg', ''))
        if return_content:
            module.fail_json(content=u_content, **uresp)
        else:
            module.fail_json(**uresp)
    elif return_content:
        module.exit_json(content=u_content, **uresp)
    else:
        module.exit_json(**uresp)
Example #38
0
    def HandleConnection(self, ep):
        if not hasattr(self, 'caching'):
            ClockThis('HTTP::Handle::Init', self.Init)
        request = Request(self, ep)
        lastRequest = ''
        requestCount = 0
        sess = None
        self.openConnectionsInHttpService.Add()
        try:
            while 1:
                sys.exc_clear()
                if request.method != 'HEAD':
                    response = Response(self, ep)
                else:
                    response = HeadResponse(self, ep)
                errfile = None
                lastRequest = request.path
                try:
                    ClockThis('HTTP::Handle::request.ParseHeader', request.ParseHeader)
                    if request.method == 'OPTION':
                        tmp = request.DumpRequestToList()
                        self.LogError('OPTION REQUEST, sorry not really an error')
                        self.LogError('The client %s made this request' % ep.address)
                        for s in tmp:
                            self.LogError(s)

                        response.SendNotImplemented()
                        continue
                    requestCount += 1
                    sess = self.GetSession(request, response)
                    if sess:
                        sess.requestCount += 1
                    else:
                        continue
                    if self.HandleCaching(ep, request, response):
                        continue
                    response.cookie['flatkaka'] = sess.esps.GetFlatkaka()
                    filename, files = self.GetFileFromRequest(request)
                    if not files:
                        errfile = filename
                        raise IOError('file %s not found in www roots' % filename)
                    try:
                        self.HandleRequestFile(request, response, filename, files)
                    finally:
                        for _, f in files:
                            f.Close()

                except GPSTransportClosed:
                    self.LogInfo('closed retrieving [%s], before that [%s].' % (request.path, lastRequest))
                    self.LogInfo('Total requests served with this connection: %d' % requestCount)
                    break
                except UnknownProtocol as e:
                    self.LogWarn('Unknown protocol: %s', e)
                    break
                except UnknownVerb as e:
                    self.LogWarn('Unknown verb: %s', e)
                    break
                except Exception as ex:
                    self.HandleException(sess, request, response, ex, errfile)

                try:
                    response.Flush()
                    if request.proto == 'HTTP/1.0':
                        break
                except GPSTransportClosed:
                    sys.exc_clear()
                    self.LogWarn('Trying to send response for [%s] but the connection was closed, prev: %s' % (request.path, lastRequest))
                    self.LogWarn('Total requests served with this connection: %d' % requestCount)
                    break

        finally:
            if not getattr(ep.socket, 'isFake', False):
                ep.Close()
            self.openConnectionsInHttpService.Dec()
            if sess:
                if sess.esps.contents.has_key('timeoutTimer'):
                    if sess.esps.contents['timeoutTimer'] == None:
                        sess.esps.contents['timeoutTimer'] = 1
                        uthread.new(self.CheckSessionTimeout, sess, sess.requestCount)
Example #39
0
def upkeep():
    """Does upkeep (like flushing, garbage collection, etc.)"""
    # Just in case, let's clear the exception info.
    try:
        sys.exc_clear()
    except AttributeError:
        # Python 3 does not have sys.exc_clear. The except statement clears
        # the info itself (and we've just entered an except statement)
        pass
    if os.name == 'nt':
        try:
            import msvcrt
            msvcrt.heapmin()
        except ImportError:
            pass
        except IOError: # Win98
            pass
    if conf.daemonized:
        # If we're daemonized, sys.stdout has been replaced with a StringIO
        # object, so let's see if anything's been printed, and if so, let's
        # log.warning it (things shouldn't be printed, and we're more likely
        # to get bug reports if we make it a warning).
        if not hasattr(sys.stdout, 'getvalue'):
            # Stupid twisted sometimes replaces our stdout with theirs, because
            # "The Twisted Way Is The Right Way" (ha!).  So we're stuck simply
            # returning.
            log.warning('Expected cStringIO as stdout, got %r.', sys.stdout)
            return
        s = sys.stdout.getvalue()
        if s:
            log.warning('Printed to stdout after daemonization: %s', s)
            sys.stdout.seek(0)
            sys.stdout.truncate() # Truncates to current offset.
        s = sys.stderr.getvalue()
        if s:
            log.error('Printed to stderr after daemonization: %s', s)
            sys.stderr.seek(0)
            sys.stderr.truncate() # Truncates to current offset.
    doFlush = conf.supybot.flush() and not starting
    if doFlush:
        flush()
        # This is so registry._cache gets filled.
        # This seems dumb, so we'll try not doing it anymore.
        #if registryFilename is not None:
        #    registry.open(registryFilename)
    if not dying:
        if minisix.PY2:
            log.debug('Regexp cache size: %s', len(re._cache))
        log.debug('Pattern cache size: %s', len(ircutils._patternCache))
        log.debug('HostmaskPatternEqual cache size: %s',
                  len(ircutils._hostmaskPatternEqualCache))
        #timestamp = log.timestamp()
        if doFlush:
            log.info('Flushers flushed and garbage collected.')
        else:
            log.info('Garbage collected.')
    collected = gc.collect()
    if gc.garbage:
        log.warning('Noncollectable garbage (file this as a bug on SF.net): %s',
                    gc.garbage)
    return collected
Example #40
0
 def clearit():
     try:
         if sys.version_info[0] < 3:
             sys.exc_clear()
     except KeyError:
         pass
Example #41
0
    def getEpisode(self,
                   callback,
                   name,
                   begin,
                   end=None,
                   service=None,
                   future=False,
                   today=False,
                   elapsed=False,
                   block=False,
                   rename=False):

        if config.plugins.seriesplugin.skip_during_records.value:
            try:
                import NavigationInstance
                if NavigationInstance.instance.RecordTimer.isRecording():
                    msg = _("Skip check during running records") + "\n\n" + _(
                        "Can be configured within the setup")
                    log.warning(msg)
                    if callable(callback):
                        callback(msg)
                    return msg
            except:
                pass

        # Check for episode information in title
        match = self.compiledRegexpSeries.match(name)
        if match:
            #log.debug(match.group(0))     # Entire match
            #log.debug(match.group(1))     # First parenthesized subgroup
            if not rename and config.plugins.seriesplugin.skip_pattern_match.value:
                msg = _("Skip check because of pattern match"
                        ) + "\n" + name + "\n\n" + _(
                            "Can be configured within the setup")
                log.warning(msg)
                if callable(callback):
                    callback(msg)
                return msg
            if match.group(1):
                name = match.group(1)

        if elapsed:
            identifier = self.identifier_elapsed
        elif today:
            identifier = self.identifier_today
        elif future:
            identifier = self.identifier_future
        else:
            identifier = self.modules and self.instantiateModule(
                self.modules.itervalues().next())

        if not identifier:
            msg = _("No identifier available") + "\n\n" + _(
                "Please check Your installation")
            log.error(msg)
            if callable(callback):
                callback(msg)
            return msg

        elif self.channelsEmpty():
            msg = _("Channels are not matched") + "\n\n" + _(
                "Please open the channel editor (setup) and match them")
            log.error(msg)
            if callable(callback):
                callback(msg)
            return msg

        else:
            # Reset title search depth on every new request
            identifier.search_depth = 0

            # Reset the knownids on every new request
            identifier.knownids = []

            try:
                serviceref = service.toString()
            except:
                sys.exc_clear()
                serviceref = str(service)
            serviceref = re.sub('::.*', ':', serviceref)

            if block == False:

                self.thread.add(
                    ThreadItem(identifier, callback, name, begin, end,
                               serviceref))

            else:

                result = None

                try:
                    result = identifier.getEpisode(name, begin, end,
                                                   serviceref)
                except Exception, e:
                    log.exception("Worker:", str(e))

                    # Exception finish job with error
                    result = str(e)

                config.plugins.seriesplugin.lookup_counter.value += 1

                data = normalizeResult(result)

                if callable(callback):
                    callback(data)

                return data
Example #42
0
def GetSession(parent, request, response, sessionsBySID, sessionsByFlatkaka):
    parent.LogInfo('GetSession')
    if request.cookie.has_key('flatkaka'):
        flatkaka = request.cookie['flatkaka']
        if sessionsByFlatkaka.has_key(flatkaka):
            sess = sessionsByFlatkaka[flatkaka]
            if macho.mode == 'client':
                return sess
            uspa = request.Authorization()
            if uspa != None:
                u = sess.esps.contents['username']
                p = sess.esps.contents['password']
                if uspa[0] != u or uspa[1] != p:
                    parent.LogWarn("User %s is trying to hijack %s's session, with sessID=%d" % (uspa[0], u, sess.sid))
                else:
                    parent.LogInfo('cookie information verified')
                    return sess
    sess = None
    success = False
    if macho.mode == 'client':
        sess = base.CreateSession(None, const.session.SESSION_TYPE_ESP, None)
        sess.esps = ESPSession(parent, sess.sid)
        success = True
    else:
        usernameAndPassword = request.Authorization()
        reason = 'Access denied'
        statusCode = '401 Unauthorized'
        if usernameAndPassword != None:
            parent.LogInfo('GetSession uap<>n')
            username = usernameAndPassword[0]
            password = usernameAndPassword[1]
            for s in sessionsBySID.itervalues():
                if hasattr(s, 'esps') and s.esps.contents['username'] == username:
                    if s.userid and s.esps.contents['password'] == password:
                        return s
                    break

            if macho.mode == 'server' and ('authentication' not in sm.services or sm.services['authentication'].state != service.SERVICE_RUNNING):
                blue.pyos.synchro.SleepWallclock(3000)
                raise UserError('AutClusterStarting')
            try:
                if sm.services['http'].session.ConnectToProxyServerService('machoNet').CheckACL(request.ep.address, espCheck=True):
                    blue.pyos.synchro.SleepWallclock(3000)
                    raise UserError('AutClusterStarting')
            except UnMachoDestination:
                blue.pyos.synchro.SleepWallclock(3000)
                raise UserError('AutClusterStarting')

            sessionID = base.GetNewSid()
            sess = base.CreateSession(sessionID, const.session.SESSION_TYPE_ESP)
            sess.esps = ESPSession(parent, sess.sid)
            auth = base.GetServiceSession('httpService').ConnectToAnyService('authentication')
            try:
                try:
                    sessstuff, _ = auth.Login(sessionID, username, password, None, const.userConnectTypeServerPages, request.ep.address)
                    sessstuff['role'] |= sess.role
                except UserError as e:
                    if e.msg != 'CharacterInDifferentRegion':
                        raise
                    sys.exc_clear()

                for each in base.FindSessions('userid', [sessstuff['userid']]):
                    each.LogSessionHistory('Usurped by user %s via HTTP using local authentication' % username)
                    base.CloseSession(each)

                sess.LogSessionHistory('Authenticating user %s via HTTP using local authentication' % username)
                sess.SetAttributes(sessstuff)
                sess.LogSessionHistory('Authenticated user %s via HTTP using local authentication' % username)
                success = True
            except UnMachoDestination:
                reason = 'The proxy server was unable to connect to any Sol Server Node to handle your authentication request.'
                statusCode = '500 No Sol Server available'
                sys.exc_clear()
            except UserError as e:
                if e.msg != 'LoginAuthFailed':
                    raise
                sys.exc_clear()

            if not success:
                sess.LogSessionHistory('Session closed due to local authentication failure')
                base.CloseSession(sess)
    parent.LogInfo('GetSession done auth %s' % success)
    if success:
        sessID = sess.sid
        while sessionsBySID.has_key(sessID):
            parent.LogWarn('Session %d already exits, adding 1 to it' % sessID)
            sessID += 1

        sessionsBySID[sessID] = sess
        sessionsByFlatkaka[sess.esps.GetFlatkaka()] = sess
        parent.OnSessionBegin(sessID)
        session = sess
        session.cacheList = []
        session.requestCount = 0
        session.esps.contents['timeoutTimer'] = None
        if macho.mode != 'client':
            session.esps.contents['username'] = username
            session.esps.contents['password'] = password
        return session
    else:
        response.Clear()
        response.status = statusCode
        response.Write(reason)
        response.authenticate = 1
        response.Flush()
        return
Example #43
0
            else:
                if isinstance(value, bytes):
                    value = value.encode('utf-8')
        except TypeError:  #in java, unicode is a function
            pass

        xmlValue = ' value="%s"' % (makeValidXmlValue(quote(value, '/>_= ')))
    else:
        xmlValue = ''

    if is_exception_on_eval:
        xmlCont = ' isErrorOnEval="True"'
    else:
        if resolver is not None:
            xmlCont = ' isContainer="True"'
        else:
            xmlCont = ''

    return ''.join((xml, xmlValue, xmlCont, additionalInXml, ' />\n'))


if USE_PSYCO_OPTIMIZATION:
    try:
        import psyco

        varToXML = psyco.proxy(varToXML)
    except ImportError:
        if hasattr(sys, 'exc_clear'):  #jython does not have it
            sys.exc_clear(
            )  #don't keep the traceback -- clients don't want to see it
Example #44
0
def getInstance():
    global instance

    if instance is None:

        log.reinit()

        from plugin import VERSION

        log.debug(" SERIESPLUGIN NEW INSTANCE " + VERSION)
        log.debug(" ", strftime("%a, %d %b %Y %H:%M:%S", localtime()))

        try:
            from Tools.HardwareInfo import HardwareInfo
            log.debug(" DeviceName " +
                      HardwareInfo().get_device_name().strip())
        except:
            sys.exc_clear()

        try:
            from Components.About import about
            log.debug(" EnigmaVersion " +
                      about.getEnigmaVersionString().strip())
            log.debug(" ImageVersion " + about.getVersionString().strip())
        except:
            sys.exc_clear()

        try:
            #http://stackoverflow.com/questions/1904394/python-selecting-to-read-the-first-line-only
            log.debug(" dreamboxmodel " +
                      open("/proc/stb/info/model").readline().strip())
            log.debug(" imageversion " +
                      open("/etc/image-version").readline().strip())
            log.debug(" imageissue " +
                      open("/etc/issue.net").readline().strip())
        except:
            sys.exc_clear()

        try:
            for key, value in config.plugins.seriesplugin.dict().iteritems():
                log.debug(" config..%s = %s" % (key, str(value.value)))
        except Exception as e:
            sys.exc_clear()

        global CompiledRegexpReplaceChars
        try:
            if config.plugins.seriesplugin.replace_chars.value:
                CompiledRegexpReplaceChars = re.compile(
                    '[' +
                    config.plugins.seriesplugin.replace_chars.value.replace(
                        "\\", "\\\\\\\\") + ']')
        except:
            log.exception(
                " Config option 'Replace Chars' is no valid regular expression"
            )
            CompiledRegexpReplaceChars = re.compile("[:\!/\\,\(\)'\?]")

        # Check autotimer
        try:
            from Plugins.Extensions.AutoTimer.plugin import autotimer
            deprecated = False
            try:
                from Plugins.Extensions.AutoTimer.plugin import AUTOTIMER_VERSION
                if int(AUTOTIMER_VERSION[0]) < 4:
                    deprecated = True
            except ImportError:
                AUTOTIMER_VERSION = "deprecated"
                deprecated = True
            log.debug(" AutoTimer: " + AUTOTIMER_VERSION)
            if deprecated:
                log.warning(
                    _("Your autotimer is deprecated") + "\n" +
                    _("Please update it"))
        except ImportError:
            log.debug(" AutoTimer: Not found")

        # Check dependencies
        start = True
        from imp import find_module
        dependencies = ["difflib", "json", "re", "xml", "xmlrpclib"]
        for dependency in dependencies:
            try:
                find_module(dependency)
            except ImportError:
                start = False
                log.error(
                    _("Error missing dependency") + "\n" + "python-" +
                    dependency + "\n\n" +
                    _("Please install missing python paket manually"))
        if start:
            instance = SeriesPlugin()

    return instance
Example #45
0
    def run(self):
        """run method"""

        pyfile = self.active
        retry = False

        try:
            self.m.log.info(_("Decrypting starts: %s") % self.active.name)
            self.active.plugin.preprocessing(self)

        except NotImplementedError:
            self.m.log.error(
                _("Plugin %s is missing a function.") % self.active.pluginname)
            return

        except Fail as e:
            msg = e.args[0]

            if msg == "offline":
                self.active.setStatus("offline")
                self.m.log.warning(
                    _("Download is offline: %s") % self.active.name)
            else:
                self.active.setStatus("failed")
                self.m.log.error(
                    _("Decrypting failed: %(name)s | %(msg)s") % {
                        "name": self.active.name,
                        "msg": msg
                    })
                self.active.error = msg

            return

        except Abort:
            self.m.log.info(_("Download aborted: %s") % pyfile.name)
            pyfile.setStatus("aborted")

            return

        except Retry:
            self.m.log.info(_("Retrying %s") % self.active.name)
            retry = True
            return self.run()

        except Exception as e:
            self.active.setStatus("failed")
            self.m.log.error(
                _("Decrypting failed: %(name)s | %(msg)s") % {
                    "name": self.active.name,
                    "msg": str(e)
                })
            self.active.error = str(e)

            if self.m.core.debug:
                print_exc()
                self.writeDebugReport(pyfile)

            return

        finally:
            if not retry:
                self.active.release()
                self.active = False
                self.m.core.files.save()
                self.m.localThreads.remove(self)
                try:
                    sys.exc_clear()
                except Exception:
                    pass

        # get_hook_manager().downloadFinished(pyfile)

        # self.m.localThreads.remove(self)
        # self.active.finishIfDone()
        if not retry:
            pyfile.delete()
Example #46
0
def ProcessPcapFile(filename, dConnections, oOutput, oLogfile, options):
    # https://en.wikipedia.org/wiki/List_of_IP_Protocol_numbers
    dIPProtocolNumbers = {
        0x00: 'HOPOPT',
        0x01: 'ICMP',
        0x02: 'IGMP',
        0x03: 'GGP',
        0x04: 'IP-in-IP',
        0x05: 'ST',
        0x06: 'TCP',
        0x07: 'CBT',
        0x08: 'EGP',
        0x09: 'IGP',
        0x0A: 'BBN-RCC-MON',
        0x0B: 'NVP-II',
        0x0C: 'PUP',
        0x0D: 'ARGUS',
        0x0E: 'EMCON',
        0x0F: 'XNET',
        0x10: 'CHAOS',
        0x11: 'UDP',
        0x12: 'MUX',
        0x13: 'DCN-MEAS',
        0x14: 'HMP',
        0x15: 'PRM',
        0x16: 'XNS-IDP',
        0x17: 'TRUNK-1',
        0x18: 'TRUNK-2',
        0x19: 'LEAF-1',
        0x1A: 'LEAF-2',
        0x1B: 'RDP',
        0x1C: 'IRTP',
        0x1D: 'ISO-TP4',
        0x1E: 'NETBLT',
        0x1F: 'MFE-NSP',
        0x20: 'MERIT-INP',
        0x21: 'DCCP',
        0x22: '3PC',
        0x23: 'IDPR',
        0x24: 'XTP',
        0x25: 'DDP',
        0x26: 'IDPR-CMTP',
        0x27: 'TP++',
        0x28: 'IL',
        0x29: 'IPv6',
        0x2A: 'SDRP',
        0x2B: 'IPv6-Route',
        0x2C: 'IPv6-Frag',
        0x2D: 'IDRP',
        0x2E: 'RSVP',
        0x2F: 'GREs',
        0x30: 'DSR',
        0x31: 'BNA',
        0x32: 'ESP',
        0x33: 'AH',
        0x34: 'I-NLSP',
        0x35: 'SwIPe',
        0x36: 'NARP',
        0x37: 'MOBILE',
        0x38: 'TLSP',
        0x39: 'SKIP',
        0x3A: 'IPv6-ICMP',
        0x3B: 'IPv6-NoNxt',
        0x3C: 'IPv6-Opts',
        #0x3D: '',
        0x3E: 'CFTP',
        0x3F: '',
        0x40: 'SAT-EXPAK',
        0x41: 'KRYPTOLAN',
        0x42: 'RVD',
        0x43: 'IPPC',
        #0x44: '',
        0x45: 'SAT-MON',
        0x46: 'VISA',
        0x47: 'IPCU',
        0x48: 'CPNX',
        0x49: 'CPHB',
        0x4A: 'WSN',
        0x4B: 'PVP',
        0x4C: 'BR-SAT-MON',
        0x4D: 'SUN-ND',
        0x4E: 'WB-MON',
        0x4F: 'WB-EXPAK',
        0x50: 'ISO-IP',
        0x51: 'VMTP',
        0x52: 'SECURE-VMTP',
        0x53: 'VINES',
        0x54: 'TTP',
        0x54: 'IPTM',
        0x55: 'NSFNET-IGP',
        0x56: 'DGP',
        0x57: 'TCF',
        0x58: 'EIGRP',
        0x59: 'OSPF',
        0x5A: 'Sprite-RPC',
        0x5B: 'LARP',
        0x5C: 'MTP',
        0x5D: 'AX.25',
        0x5E: 'OS',
        0x5F: 'MICP',
        0x60: 'SCC-SP',
        0x61: 'ETHERIP',
        0x62: 'ENCAP',
        #0x63: '',
        0x64: 'GMTP',
        0x65: 'IFMP',
        0x66: 'PNNI',
        0x67: 'PIM',
        0x68: 'ARIS',
        0x69: 'SCPS',
        0x6A: 'QNX',
        0x6B: 'A/N',
        0x6C: 'IPComp',
        0x6D: 'SNP',
        0x6E: 'Compaq-Peer',
        0x6F: 'IPX-in-IP',
        0x70: 'VRRP',
        0x71: 'PGM',
        #0x72: '',
        0x73: 'L2TP',
        0x74: 'DDX',
        0x75: 'IATP',
        0x76: 'STP',
        0x77: 'SRP',
        0x78: 'UTI',
        0x79: 'SMP',
        0x7A: 'SM',
        0x7B: 'PTP',
        0x7C: 'IS-IS over IPv4',
        0x7D: 'FIRE',
        0x7E: 'CRTP',
        0x7F: 'CRUDP',
        0x80: 'SSCOPMCE',
        0x81: 'IPLT',
        0x82: 'SPS',
        0x83: 'PIPE',
        0x84: 'SCTP',
        0x85: 'FC',
        0x86: 'RSVP-E2E-IGNORE',
        0x87: 'Mobility Header',
        0x88: 'UDPLite',
        0x89: 'MPLS-in-IP',
        0x8A: 'manet',
        0x8B: 'HIP',
        0x8C: 'Shim6',
        0x8D: 'WESP',
        0x8E: 'ROHC',
        0x8F: 'Ethernet',
    }
    with PcapFile(filename, oLogfile) as fIn:
        try:
            for timestamp, buffer in dpkt.pcap.Reader(fIn):
                # ----- Put your line processing code here -----
                try:
                    frame = dpkt.ethernet.Ethernet(buffer)
                except KeyboardInterrupt:
                    raise
                except:
                    continue

                if not isinstance(frame.data, dpkt.ip.IP) and not isinstance(
                        frame.data, dpkt.ip6.IP6):
                    continue
                ipPacket = frame.data
                protocolPacket = ipPacket.data
                if isinstance(frame.data.data, dpkt.tcp.TCP):
                    protocol = 'TCP'
                elif isinstance(frame.data.data, dpkt.udp.UDP):
                    protocol = 'UDP'
                else:
                    protocol = dIPProtocolNumbers.get(ipPacket.p,
                                                      str(ipPacket.p))

                if protocol in ['TCP', 'UDP']:
                    connectionid = (
                        protocol, '%s:%d-%s:%d' %
                        (IP2String(ipPacket.src), protocolPacket.sport,
                         IP2String(ipPacket.dst), protocolPacket.dport))
                else:
                    connectionid = (
                        protocol, '%s-%s' %
                        (IP2String(ipPacket.src), IP2String(ipPacket.dst)))

                if not connectionid in dConnections:
                    dConnections[connectionid] = {
                        ENTRY_HEAD: b'',
                        ENTRY_PREVALENCE: {iter: 0
                                           for iter in range(0x100)}
                    }

                if sys.version_info[0] < 3:
                    data = protocolPacket.data
                    while not isinstance(data, str):  # necessary for ICMP, ...
                        data = data.data
                    for char in data:
                        dConnections[connectionid][ENTRY_PREVALENCE][ord(
                            char)] += 1
                        if len(dConnections[connectionid][ENTRY_HEAD]) < 4:
                            dConnections[connectionid][ENTRY_HEAD] += char
                else:
                    data = protocolPacket.data
                    while not isinstance(data,
                                         bytes):  # necessary for ICMP, ...
                        data = data.data
                    for byte in data:
                        dConnections[connectionid][ENTRY_PREVALENCE][byte] += 1
                        if len(dConnections[connectionid][ENTRY_HEAD]) < 4:
                            dConnections[connectionid][ENTRY_HEAD] += bytes(
                                [byte])
                # ----------------------------------------------
        except:
            oLogfile.LineError('Processing file %s %s' %
                               (filename, repr(sys.exc_info()[1])))
            if sys.exc_info()[0] == KeyboardInterrupt:
                raise
            if not options.ignoreprocessingerrors:
                raise
            if sys.version_info[0] < 3:
                sys.exc_clear()
Example #47
0
def main():
    """Prepare the configuration options and start requesting tasks."""
    logs.configure('run_bot')

    root_directory = environment.get_value('ROOT_DIR')
    if not root_directory:
        print(
            'Please set ROOT_DIR environment variable to the root of the source '
            'checkout before running. Exiting.')
        print('For an example, check init.bash in the local directory.')
        return

    dates.initialize_timezone_from_environment()
    environment.set_bot_environment()
    monitor.initialize()

    if not profiler.start_if_needed('python_profiler_bot'):
        sys.exit(-1)

    fuzzers_init.run()

    if environment.is_trusted_host(ensure_connected=False):
        from bot.untrusted_runner import host
        host.init()

    if environment.is_untrusted_worker():
        # Track revision since we won't go into the task_loop.
        update_task.track_revision()

        from bot.untrusted_runner import untrusted as untrusted_worker
        untrusted_worker.start_server()
        assert False, 'Unreachable code'

    while True:
        # task_loop should be an infinite loop,
        # unless we run into an exception.
        error_stacktrace, clean_exit, task_payload = task_loop()

        # Print the error trace to the console.
        if not clean_exit:
            print('Exception occurred while running "%s".' % task_payload)
            print('-' * 80)
            print(error_stacktrace)
            print('-' * 80)

        should_terminate = (clean_exit or errors.error_in_list(
            error_stacktrace, errors.BOT_ERROR_TERMINATION_LIST))
        if should_terminate:
            return

        logs.log_error('Task exited with exception (payload="%s").' %
                       task_payload,
                       error_stacktrace=error_stacktrace)

        should_hang = errors.error_in_list(error_stacktrace,
                                           errors.BOT_ERROR_HANG_LIST)
        if should_hang:
            logs.log('Start hanging forever.')
            while True:
                # Sleep to avoid consuming 100% of CPU.
                time.sleep(60)

        # See if our run timed out, if yes bail out.
        if data_handler.bot_run_timed_out():
            return

        # Clear the current exception.
        sys.exc_clear()
Example #48
0
def main():
    argument_spec = url_argument_spec()
    argument_spec.update(
        dest=dict(type='path'),
        url_username=dict(type='str', aliases=['user']),
        url_password=dict(type='str', aliases=['password'], no_log=True),
        body=dict(type='raw'),
        body_format=dict(type='str',
                         default='raw',
                         choices=['form-urlencoded', 'json', 'raw']),
        src=dict(type='path'),
        method=dict(type='str',
                    default='GET',
                    choices=[
                        'CONNECT', 'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH',
                        'POST', 'PUT', 'REFRESH', 'TRACE'
                    ]),
        return_content=dict(type='bool', default=False),
        follow_redirects=dict(
            type='str',
            default='safe',
            choices=['all', 'no', 'none', 'safe', 'urllib2', 'yes']),
        creates=dict(type='path'),
        removes=dict(type='path'),
        status_code=dict(type='list', default=[200]),
        timeout=dict(type='int', default=30),
        headers=dict(type='dict', default={}),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        # TODO: Remove check_invalid_arguments in 2.9
        check_invalid_arguments=False,
        add_file_common_args=True,
        mutually_exclusive=[['body', 'src']],
    )

    url = module.params['url']
    body = module.params['body']
    body_format = module.params['body_format'].lower()
    method = module.params['method']
    dest = module.params['dest']
    return_content = module.params['return_content']
    creates = module.params['creates']
    removes = module.params['removes']
    status_code = [int(x) for x in list(module.params['status_code'])]
    socket_timeout = module.params['timeout']

    dict_headers = module.params['headers']

    if body_format == 'json':
        # Encode the body unless its a string, then assume it is pre-formatted JSON
        if not isinstance(body, string_types):
            body = json.dumps(body)
        if 'content-type' not in [header.lower() for header in dict_headers]:
            dict_headers['Content-Type'] = 'application/json'
    elif body_format == 'form-urlencoded':
        if not isinstance(body, string_types):
            try:
                body = form_urlencoded(body)
            except ValueError as e:
                module.fail_json(
                    msg='failed to parse body as form_urlencoded: %s' %
                    to_native(e),
                    elapsed=0)
        if 'content-type' not in [header.lower() for header in dict_headers]:
            dict_headers['Content-Type'] = 'application/x-www-form-urlencoded'

    # TODO: Deprecated section.  Remove in Ansible 2.9
    # Grab all the http headers. Need this hack since passing multi-values is
    # currently a bit ugly. (e.g. headers='{"Content-Type":"application/json"}')
    for key, value in iteritems(module.params):
        if key.startswith("HEADER_"):
            module.deprecate(
                'Supplying headers via HEADER_* is deprecated. Please use `headers` to'
                ' supply headers for the request',
                version='2.9')
            skey = key.replace("HEADER_", "")
            dict_headers[skey] = value
    # End deprecated section

    if creates is not None:
        # do not run the command if the line contains creates=filename
        # and the filename already exists.  This allows idempotence
        # of uri executions.
        if os.path.exists(creates):
            module.exit_json(stdout="skipped, since '%s' exists" % creates,
                             changed=False)

    if removes is not None:
        # do not run the command if the line contains removes=filename
        # and the filename does not exist.  This allows idempotence
        # of uri executions.
        if not os.path.exists(removes):
            module.exit_json(stdout="skipped, since '%s' does not exist" %
                             removes,
                             changed=False)

    # Make the request
    start = datetime.datetime.utcnow()
    resp, content, dest = uri(module, url, dest, body, body_format, method,
                              dict_headers, socket_timeout)
    resp['elapsed'] = (datetime.datetime.utcnow() - start).seconds
    resp['status'] = int(resp['status'])

    # Write the file out if requested
    if dest is not None:
        if resp['status'] == 304:
            resp['changed'] = False
        else:
            write_file(module, url, dest, content, resp)
            # allow file attribute changes
            resp['changed'] = True
            module.params['path'] = dest
            file_args = module.load_file_common_arguments(module.params)
            file_args['path'] = dest
            resp['changed'] = module.set_fs_attributes_if_different(
                file_args, resp['changed'])
        resp['path'] = dest
    else:
        resp['changed'] = False

    # Transmogrify the headers, replacing '-' with '_', since variables don't
    # work with dashes.
    # In python3, the headers are title cased.  Lowercase them to be
    # compatible with the python2 behaviour.
    uresp = {}
    for key, value in iteritems(resp):
        ukey = key.replace("-", "_").lower()
        uresp[ukey] = value

    if 'location' in uresp:
        uresp['location'] = absolute_location(url, uresp['location'])

    # Default content_encoding to try
    content_encoding = 'utf-8'
    if 'content_type' in uresp:
        content_type, params = cgi.parse_header(uresp['content_type'])
        if 'charset' in params:
            content_encoding = params['charset']
        u_content = to_text(content, encoding=content_encoding)
        if any(candidate in content_type for candidate in JSON_CANDIDATES):
            try:
                js = json.loads(u_content)
                uresp['json'] = js
            except Exception:
                if PY2:
                    sys.exc_clear(
                    )  # Avoid false positive traceback in fail_json() on Python 2
    else:
        u_content = to_text(content, encoding=content_encoding)

    if resp['status'] not in status_code:
        uresp['msg'] = 'Status code was %s and not %s: %s' % (
            resp['status'], status_code, uresp.get('msg', ''))
        module.fail_json(content=u_content, **uresp)
    elif return_content:
        module.exit_json(content=u_content, **uresp)
    else:
        module.exit_json(**uresp)
Example #49
0
    def Load(self, node):
        self.sr.node = node
        data = node
        self.name = data.info.name
        self.sr.namelabel.text = data.label
        self.charid = self.id = data.itemID = data.charID
        self.picloaded = 0
        self.sr.parwnd = data.Get('dad', None)
        self.fleetCandidate = data.Get('fleetster',
                                       1) and not data.info.IsNPC()
        self.confirmOnDblClick = data.Get('dblconfirm', 1)
        self.leaveDadAlone = data.Get('leavedad', 0)
        self.slimuser = data.Get('slimuser', False)
        self.data = {'Name': self.name, 'charid': data.charID}
        self.inWatchlist = sm.GetService('addressbook').IsInWatchlist(
            data.charID)
        self.isContactList = data.Get('contactType', None)
        self.applicationDate = data.Get('applicationDate', None)
        self.contactLevel = None
        if self.isContactList:
            self.contactLevel = data.contactLevel
            self.sr.namelabel.top = 2
            self.SetLabelText()
        else:
            self.sr.namelabel.SetAlign(uiconst.CENTERLEFT)
        self.isCorpOrAllianceContact = data.contactType and data.contactType != 'contact'
        data.listvalue = [data.info.name, data.charID]
        level = self.sr.node.Get('sublevel', 0)
        subLevelOffset = 16
        self.sr.picture.left = 2 + max(0, subLevelOffset * level)
        self.LoadPortrait()
        if data.IsCharacter:
            uthread.new(self.SetRelationship, data)
            self.sr.statusIcon.state = uiconst.UI_HIDDEN
            if data.charID != eve.session.charid:
                if not util.IsNPC(
                        data.charID
                ) and not self.isCorpOrAllianceContact and self.inWatchlist:
                    try:
                        self.SetOnline(
                            sm.GetService('onlineStatus').GetOnlineStatus(
                                data.charID, fetch=False))
                    except IndexError:
                        sys.exc_clear()

            if node.isDustCharacter:
                self.ShowDustBackground()
        else:
            self.sr.statusIcon.state = uiconst.UI_HIDDEN
            if data.charID != eve.session.charid:
                uthread.new(self.SetRelationship, data)
        self.sr.namelabel.left = 40 + max(0, subLevelOffset * level)
        self.sr.contactLabels.left = 40
        if self.sr.node.Get('selected', 0):
            self.Select()
        else:
            self.Deselect()
        if not self.isCorpOrAllianceContact:
            self.contactLevel = sm.GetService('addressbook').GetStandingsLevel(
                self.charid, 'contact')
            self.SetBlocked(1)
        else:
            self.SetBlocked(0)
        if self.isCorpOrAllianceContact:
            self.SetStandingText(self.contactLevel)
        if self.applicationDate:
            self.sr.corpApplicationLabel.SetText(
                localization.GetByLabel('UI/Corporations/Applied',
                                        applydate=self.applicationDate))
            self.sr.corpApplicationLabel.Show()
            data.Set('sort_' + localization.GetByLabel('UI/Common/Date'),
                     self.applicationDate)
            data.Set('sort_' + localization.GetByLabel('UI/Common/Name'),
                     data.info.name)
        else:
            self.sr.corpApplicationLabel.SetText('')
            self.sr.corpApplicationLabel.Hide()
Example #50
0
    def run(self):
        """run method"""
        pyfile = None

        while True:
            del pyfile
            self.active = self.queue.get()
            pyfile = self.active

            if self.active == "quit":
                self.active = False
                self.m.threads.remove(self)
                return True

            hook_manager = get_hook_manager()

            try:
                if not pyfile.hasPlugin(): continue
                #this pyfile was deleted while queueing

                pyfile.plugin.checkForSameFiles(starting=True)
                self.m.log.info(_("Download starts: %s" % pyfile.name))

                # start download
                hook_manager.downloadPreparing(pyfile)
                pyfile.plugin.preprocessing(self)

                self.m.log.info(_("Download finished: %s") % pyfile.name)
                hook_manager.downloadFinished(pyfile)
                self.m.core.files.checkPackageFinished(pyfile)

            except NotImplementedError:
                self.m.log.error(
                    _("Plugin %s is missing a function.") % pyfile.pluginname)
                pyfile.setStatus("failed")
                pyfile.error = "Plugin does not work"
                self.clean(pyfile)
                continue

            except Abort:
                try:
                    self.m.log.info(_("Download aborted: %s") % pyfile.name)
                except:
                    pass

                pyfile.setStatus("aborted")

                self.clean(pyfile)
                continue

            except Reconnect:
                self.queue.put(pyfile)
                #pyfile.req.clearCookies()

                while self.m.reconnecting.isSet():
                    sleep(0.5)

                continue

            except Retry as e:
                reason = e.args[0]
                self.m.log.info(
                    _("Download restarted: %(name)s | %(msg)s") % {
                        "name": pyfile.name,
                        "msg": reason
                    })
                self.queue.put(pyfile)
                continue

            except Fail as e:
                msg = e.args[0]

                if msg == "offline":
                    pyfile.setStatus("offline")
                    self.m.log.warning(
                        _("Download is offline: %s") % pyfile.name)
                elif msg == "temp. offline":
                    pyfile.setStatus("temp. offline")
                    self.m.log.warning(
                        _("Download is temporary offline: %s") % pyfile.name)
                else:
                    pyfile.setStatus("failed")
                    self.m.log.warning(
                        _("Download failed: %(name)s | %(msg)s") % {
                            "name": pyfile.name,
                            "msg": msg
                        })
                    pyfile.error = msg

                hook_manager.downloadFailed(pyfile)
                self.clean(pyfile)
                continue

            except error as e:
                if len(e.args) == 2:
                    code, msg = e.args
                else:
                    code = 0
                    msg = e.args

                self.m.log.debug("pycurl exception %s: %s" % (code, msg))

                if code in (7, 18, 28, 52, 56):
                    self.m.log.warning(
                        _("Couldn't connect to host or connection reset, waiting 1 minute and retry."
                          ))
                    wait = time() + 60

                    pyfile.waitUntil = wait
                    pyfile.setStatus("waiting")
                    while time() < wait:
                        sleep(1)
                        if pyfile.abort:
                            break

                    if pyfile.abort:
                        self.m.log.info(
                            _("Download aborted: %s") % pyfile.name)
                        pyfile.setStatus("aborted")

                        self.clean(pyfile)
                    else:
                        self.queue.put(pyfile)

                    continue

                else:
                    pyfile.setStatus("failed")
                    self.m.log.error("pycurl error %s: %s" % (code, msg))
                    if self.m.core.debug:
                        print_exc()
                        self.writeDebugReport(pyfile)

                    hook_manager.downloadFailed(pyfile)

                self.clean(pyfile)
                continue

            except SkipDownload as e:
                pyfile.setStatus("skipped")

                self.m.log.info(
                    _("Download skipped: %(name)s due to %(plugin)s") % {
                        "name": pyfile.name,
                        "plugin": e.message
                    })

                self.clean(pyfile)

                self.m.core.files.checkPackageFinished(pyfile)

                self.active = False
                self.m.core.files.save()

                continue

            except Exception as e:
                pyfile.setStatus("failed")
                self.m.log.warning(
                    _("Download failed: %(name)s | %(msg)s") % {
                        "name": pyfile.name,
                        "msg": str(e)
                    })
                pyfile.error = str(e)

                if self.m.core.debug:
                    print_exc()
                    self.writeDebugReport(pyfile)

                hook_manager.downloadFailed(pyfile)
                self.clean(pyfile)
                continue

            finally:
                self.m.core.files.save()
                pyfile.checkIfProcessed()
                try:
                    sys.exc_clear()
                except Exception:
                    pass

            #pyfile.plugin.req.clean()

            self.active = False
            pyfile.finishIfDone()
            self.m.core.files.save()
Example #51
0
 def h1():
     sys.exc_clear()
Example #52
0
def reload_all():
    """
    Resets all modules to the state they were in right after import_all
    returned.
    """

    if mobile:
        raise Exception("Reloading is not supported on mobile platforms.")

    import renpy.style
    import renpy.display

    # Clear all pending exceptions.
    sys.exc_clear()

    # Reset the styles.
    renpy.style.reset()  # @UndefinedVariable

    # Shut down the cache thread.
    renpy.display.im.cache.quit()

    # Shut down the importer.
    renpy.loader.quit_importer()

    # Free memory.
    renpy.exports.free_memory()

    # GC renders.
    renpy.display.render.screen_render = None
    renpy.display.render.mark_sweep()

    # Get rid of the draw module and interface.
    renpy.display.draw.quit()
    renpy.display.draw = None
    renpy.display.interface = None

    py_compile_cache = renpy.python.py_compile_cache
    reload_modules = renpy.config.reload_modules

    # Delete the store modules.
    for i in sys.modules.keys():
        if issubmodule(i, "store") or i == "renpy.store":
            m = sys.modules[i]

            if m is not None:
                m.__dict__.reset()

            del sys.modules[i]

        elif any(issubmodule(i, m) for m in reload_modules):
            m = sys.modules[i]

            if m is not None:
                m.__dict__.clear()

            del sys.modules[i]

    # Restore the state of all modules from backup.
    backup.restore()

    renpy.python.old_py_compile_cache = py_compile_cache

    renpy.display.im.reset_module()

    post_import()

    # Re-initialize the importer.
    renpy.loader.init_importer()
Example #53
0
    def GetLocation(self, element, directive):
        try:
            parent = element
            while hasattr(parent, 'parent'):
                if parent is None or parent.state == uiconst.UI_HIDDEN:
                    return (-999, -999, False, False, False, False)
                parent = parent.parent

            width = 0
            if directive == 'shipui' and element.parent.name == 'slotsContainer':
                if element.parent.name == 'slotsContainer':
                    slotsContainer = element.parent
                    cumTop, cumLeft = slotsContainer.absoluteTop + element.top, slotsContainer.absoluteLeft + element.left + 5
            elif directive == 'bracket':
                parent = element.parent
                cumTop, cumLeft = parent.absoluteTop + element.top, parent.absoluteLeft + element.left - 2
            elif directive == 'neocom':
                cumTop, cumLeft = element.absoluteTop, element.absoluteLeft
            elif hasattr(element, 'absoluteTop') and hasattr(
                    element, 'absoluteLeft'):
                cumTop, cumLeft = element.absoluteTop - 2, element.absoluteLeft - 2
            else:
                cumTop, cumLeft = element.parent.absoluteTop + element.top - 2, element.parent.absoluteLeft + element.left - 2
            pointLeft = True
            height = element.height
            if height == 0:
                height = element.absoluteBottom - element.absoluteTop
            cumTop += height / 2 - UIPOINTER_HEIGHT / 2
            if cumLeft - UIPOINTER_WIDTH <= 0:
                if directive == 'neocom':
                    neocom = sm.GetService('neocom').neocom
                    if neocom is not None:
                        width = neocom.width
                    else:
                        width = element.width
                        if width == 0:
                            width = element.absoluteRight - element.absoluteLeft
                else:
                    width = element.width
                    if width == 0:
                        width = element.absoluteRight - element.absoluteLeft
                cumLeft = cumLeft + width + 2
            else:
                pointLeft = False
                cumLeft -= UIPOINTER_WIDTH - 2
            pointUp = False
            pointDown = False
            arrowPos = 0
            if cumTop < 0:
                if directive != 'neocom':
                    pointUp = True
                    cumTop += height / 2 + UIPOINTER_HEIGHT / 2
                    if hasattr(element, 'absoluteLeft') and hasattr(
                            element, 'absoluteRight'):
                        cumLeft = element.absoluteLeft - UIPOINTER_WIDTH / 2 + (
                            element.absoluteRight - element.absoluteLeft) / 2
                    else:
                        cumLeft = element.parent.absoluteLeft + element.left - UIPOINTER_WIDTH / 2
                    if cumLeft < 0:
                        cumLeft = 0
                    elif cumLeft + UIPOINTER_WIDTH > uicore.desktop.width:
                        cumLeft = uicore.desktop.width - UIPOINTER_WIDTH
                    if directive == 'bracket':
                        cumTop -= 8
                else:
                    additionalSpace = 8
                    arrowPos = cumTop - additionalSpace
                    cumTop = additionalSpace
            elif cumTop + UIPOINTER_HEIGHT > uicore.desktop.height:
                uiPointerElement = self.currentPointer['uiPointerElement']
                currentPointerHeight = uiPointerElement.height
                if currentPointerHeight < 1:
                    currentPointerHeight = UIPOINTER_HEIGHT
                pointDown = True
                cumTop = element.absoluteTop - currentPointerHeight
                cumLeft = element.absoluteLeft - UIPOINTER_WIDTH / 2 + (
                    element.absoluteRight - element.absoluteLeft) / 2
                if cumLeft < 0:
                    cumLeft = 0
                elif cumLeft + UIPOINTER_WIDTH > uicore.desktop.width:
                    cumLeft = uicore.desktop.width - UIPOINTER_WIDTH
            elif pointLeft == False:
                if directive == 'bracket':
                    cumTop -= 3
            elif directive == 'bracket':
                cumLeft += UIPOINTER_ARROW_WIDTH
                cumTop -= 3
            isObscured = self.CheckIsElementObscured(cumTop, cumLeft,
                                                     pointLeft, element)
            return (cumTop, cumLeft, pointLeft, pointUp, pointDown, isObscured,
                    arrowPos)
        except:
            log.LogException()
            sys.exc_clear()
            return (-999, -999, False, False, False, False)
Example #54
0
 def f():
     try:
         1 / 0
     except ZeroDivisionError:
         sys.exc_clear()
         raise
Example #55
0
def _LogException(exc_info, extraText, channel, toConsole, toLogServer,
                  toAlertSvc, toMsgWindow, severity, show_locals):
    if raven_client:
        raven_client(message=extraText, exc_info=exc_info)
    exctype, exc, tb = exc_info
    exception_list = traceback2.extract_tb(tb, extract_locals=show_locals)
    if tb:
        caught_list = traceback2.extract_stack(tb.tb_frame)
    else:
        caught_list = traceback2.extract_stack(up=2)
    stack, stackID = GetStack(exception_list,
                              caught_list,
                              show_locals=show_locals)
    if severity is None:
        severity = (ERR, WARN)[isinstance(exc, UserError)]
    if toAlertSvc is None:
        toAlertSvc = severity in (ERR, )
    if toMsgWindow and isinstance(exc, UserError) and boot.role == 'client':
        toMsgWindow = 0
        uiMessageFunc(*exc.args)
    multiplexToConsole = False if toLogServer else toConsole
    out = GetMultiplex(channel, severity, multiplexToConsole, toLogServer,
                       toMsgWindow, toAlertSvc, stackID)
    formatted_exception = traceback2.format_exception_only(exctype, exc)
    if not extraText:
        try:
            extraText = 'Info: %s' % formatted_exception[-1].strip()
        except:
            extraText = 'Info: <none>'

    prefix = 'REMOTE ' if channel == 'remote.exc' else ''
    traceID = NextTraceID()
    print >> out, '%sEXCEPTION #%d logged at %s %s : %s ' % (
        prefix, traceID, blue.os.FormatUTC()[0], blue.os.FormatUTC()[2],
        extraText)
    print >> out, ' '
    print >> out, 'Formatted exception info:',
    for line in formatted_exception:
        print >> out, line,

    print >> out, ' '
    for line in stack:
        print >> out, line,

    print >> out, ' '
    if exctype is MemoryError:
        try:
            DumpMemoryStatus(out)
            DumpMemHistory(out)
        except:
            pass

    try:
        _LogThreadLocals(out)
    except MemoryError:
        pass

    if boot.role != 'client':
        try:
            ram = blue.sysinfo.GetMemory().pageFile / 1024 / 1024
            cpuLoad = sm.GetService('machoNet').GetCPULoad()
            memLeft = blue.sysinfo.GetMemory().availablePhysical / 1024 / 1024
            txt = 'System Information: '
            txt += ' Node ID: %s' % sm.GetService('machoNet').GetNodeID()
            if boot.role == 'server':
                txt += ' | Node Name: %s' % sm.GetService(
                    'machoNet').GetLocalHostName()
            txt += ' | Total CPU load: %s%%' % int(cpuLoad)
            txt += ' | Process memory in use: %s MB' % ram
            txt += ' | Physical memory left: %s MB' % memLeft
            print >> out, txt
        except Exception as e:
            sys.exc_clear()

    try:
        print >> out, 'Stackhash: %s' % stackID[0]
    except Exception:
        pass

    print >> out, 'Reported from: ', __name__
    print >> out, '%sEXCEPTION END' % (prefix, )
    out.flush()
    if toConsole:
        if toLogServer:
            print >> sys.stderr, '#nolog: An exception has occurred. It has been logged in the log server as exception #%d' % traceID
        else:
            print >> sys.stderr, 'There is no useful information accompanying this exception in the log server'
Example #56
0
 def __exit__(self, exc_type, exc_value, tb):
     sys.exc_clear()
 def __exit__(self, *args):
     sys.exc_clear()
Example #58
0
    def perform_action(self, resource, definition):
        result = {'changed': False, 'result': {}}
        state = self.params.get('state', None)
        force = self.params.get('force', False)
        name = definition['metadata'].get('name')
        namespace = definition['metadata'].get('namespace')
        existing = None
        wait = self.params.get('wait')
        wait_timeout = self.params.get('wait_timeout')

        self.remove_aliases()

        if definition['kind'].endswith('List'):
            result['result'] = resource.get(namespace=namespace).to_dict()
            result['changed'] = False
            result['method'] = 'get'
            return result

        try:
            # ignore append_hash for resources other than ConfigMap and Secret
            if self.append_hash and definition['kind'] in [
                    'ConfigMap', 'Secret'
            ]:
                name = '%s-%s' % (name, generate_hash(definition))
                definition['metadata']['name'] = name
            params = dict(name=name, namespace=namespace)
            existing = resource.get(**params)
        except NotFoundError:
            # Remove traceback so that it doesn't show up in later failures
            try:
                sys.exc_clear()
            except AttributeError:
                # no sys.exc_clear on python3
                pass
        except ForbiddenError as exc:
            if definition['kind'] in ['Project', 'ProjectRequest'
                                      ] and state != 'absent':
                return self.create_project_request(definition)
            self.fail_json(
                msg='Failed to retrieve requested object: {0}'.format(
                    exc.body),
                error=exc.status,
                status=exc.status,
                reason=exc.reason)
        except DynamicApiError as exc:
            self.fail_json(
                msg='Failed to retrieve requested object: {0}'.format(
                    exc.body),
                error=exc.status,
                status=exc.status,
                reason=exc.reason)

        if state == 'absent':
            result['method'] = "delete"
            if not existing:
                # The object already does not exist
                return result
            else:
                # Delete the object
                if not self.check_mode:
                    try:
                        k8s_obj = resource.delete(**params)
                        result['result'] = k8s_obj.to_dict()
                    except DynamicApiError as exc:
                        self.fail_json(
                            msg="Failed to delete object: {0}".format(
                                exc.body),
                            error=exc.status,
                            status=exc.status,
                            reason=exc.reason)
                result['changed'] = True
                if wait:
                    success, resource, duration = self.wait(
                        resource, definition, wait_timeout, 'absent')
                    result['duration'] = duration
                    if not success:
                        self.fail_json(msg="Resource deletion timed out",
                                       **result)
                return result
        else:
            if not existing:
                if self.check_mode:
                    k8s_obj = definition
                else:
                    try:
                        k8s_obj = resource.create(
                            definition, namespace=namespace).to_dict()
                    except ConflictError:
                        # Some resources, like ProjectRequests, can't be created multiple times,
                        # because the resources that they create don't match their kind
                        # In this case we'll mark it as unchanged and warn the user
                        self.warn(
                            "{0} was not found, but creating it returned a 409 Conflict error. This can happen \
                                  if the resource you are creating does not directly create a resource of the same kind."
                            .format(name))
                        return result
                    except DynamicApiError as exc:
                        msg = "Failed to create object: {0}".format(exc.body)
                        if self.warnings:
                            msg += "\n" + "\n    ".join(self.warnings)
                        self.fail_json(msg=msg,
                                       error=exc.status,
                                       status=exc.status,
                                       reason=exc.reason)
                success = True
                result['result'] = k8s_obj
                if wait:
                    success, result['result'], result['duration'] = self.wait(
                        resource, definition, wait_timeout)
                result['changed'] = True
                result['method'] = 'create'
                if not success:
                    self.fail_json(msg="Resource creation timed out", **result)
                return result

            match = False
            diffs = []

            if existing and force:
                if self.check_mode:
                    k8s_obj = definition
                else:
                    try:
                        k8s_obj = resource.replace(
                            definition,
                            name=name,
                            namespace=namespace,
                            append_hash=self.append_hash).to_dict()
                    except DynamicApiError as exc:
                        msg = "Failed to replace object: {0}".format(exc.body)
                        if self.warnings:
                            msg += "\n" + "\n    ".join(self.warnings)
                        self.fail_json(msg=msg,
                                       error=exc.status,
                                       status=exc.status,
                                       reason=exc.reason)
                match, diffs = self.diff_objects(existing.to_dict(), k8s_obj)
                success = True
                result['result'] = k8s_obj
                if wait:
                    success, result['result'], result['duration'] = self.wait(
                        resource, definition, wait_timeout)
                match, diffs = self.diff_objects(existing.to_dict(),
                                                 result['result'].to_dict())
                result['changed'] = not match
                result['method'] = 'replace'
                result['diff'] = diffs
                if not success:
                    self.fail_json(msg="Resource replacement timed out",
                                   **result)
                return result

            # Differences exist between the existing obj and requested params
            if self.check_mode:
                k8s_obj = dict_merge(existing.to_dict(), definition)
            else:
                if LooseVersion(
                        self.openshift_version) < LooseVersion("0.6.2"):
                    k8s_obj, error = self.patch_resource(
                        resource, definition, existing, name, namespace)
                else:
                    for merge_type in self.params['merge_type'] or [
                            'strategic-merge', 'merge'
                    ]:
                        k8s_obj, error = self.patch_resource(
                            resource,
                            definition,
                            existing,
                            name,
                            namespace,
                            merge_type=merge_type)
                        if not error:
                            break
                if error:
                    self.fail_json(**error)

            success = True
            result['result'] = k8s_obj
            if wait:
                success, result['result'], result['duration'] = self.wait(
                    resource, definition, wait_timeout)
            match, diffs = self.diff_objects(existing.to_dict(),
                                             result['result'])
            result['result'] = k8s_obj
            result['changed'] = not match
            result['method'] = 'patch'
            result['diff'] = diffs

            if not success:
                self.fail_json(msg="Resource update timed out", **result)
            return result
Example #59
0
    def GetMessage(self,
                   key,
                   dict=None,
                   onNotFound='return',
                   onDictMissing='error'):
        if key not in self.messages:
            if onNotFound == 'return':
                return self.GetMessage('ErrMessageNotFound', {
                    'msgid': key,
                    'args': dict
                })
            if onNotFound == 'raise':
                raise RuntimeError('ErrMessageNotFound', {
                    'msgid': key,
                    'args': dict
                })
            else:
                if onNotFound == 'pass':
                    return
                raise RuntimeError('GetMessage: WTF', onNotFound)
        msg = self.messages[key]
        text, title, suppress = msg.messageText, None, 0
        if dict == -1:
            pass
        elif dict is not None:
            try:
                if dict is not None and dict != -1:
                    dict = self.__prepdict(dict)
                if text is not None:
                    text = text % dict
            except KeyError as e:
                if onNotFound == 'raise':
                    raise
                sys.exc_clear()
                return self.GetMessage(
                    'ErrMessageKeyError', {
                        'msgid': key,
                        'text': msg.messageText,
                        'args': dict,
                        'err': strx(e)
                    })

        elif onDictMissing == 'error':
            if text and text.find('%(') != -1:
                if onNotFound == 'raise':
                    raise RuntimeError('ErrMessageDictMissing', {
                        'msgid': key,
                        'text': msg.messageText
                    })
                return self.GetMessage('ErrMessageDictMissing', {
                    'msgid': key,
                    'text': msg.messageText
                })
        if text is not None:
            ix = text.find('::')
            if ix != -1:
                title, text = text.split('::')
                suppress = 0
            else:
                ix = text.find(':s:')
                if ix != -1:
                    title, text = text.split(':s:')
                    suppress = 1
        else:
            text, title, suppress = (None, None, 0)
        return utillib.KeyVal(text=text,
                              title=title,
                              type=msg.dialogType,
                              audio=msg.urlAudio,
                              icon=msg.urlIcon,
                              suppress=suppress)
Example #60
0
 def test_sys_exc_clear(self):
     expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
     with check_py3k_warnings() as w:
         self.assertWarning(sys.exc_clear(), w, expected)