Ejemplo n.º 1
0
    def submit_results(self, transport_url, body, headers, timeout):
        transport = HTTPTransport(transport_url)
        start_time = time.time()
        submission_stat = os.fstat(body[0][1].fileno())
        submission_size = submission_stat.st_size

        try:
            response = transport.exchange(body, headers, timeout)
        except (IOError, OSError) as error:
            logging.debug(error)
            return (False, error)

        end_time = time.time()
        if not response:
            error = "Error contacting the server: %s." % transport_url
            logging.debug(error)
            return (False, error)
        elif response.status != 200:
            error = "Server returned unexpected status: %d. " % \
                    response.status
            logging.debug(error)
            return (False, error)
        else:
            #This is the only success block
            text = response.read().decode()
            status_url = json.loads(text)['url']
            if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
                logging.debug("Response headers:\n%s",
                              pprint.pformat(response.getheaders()))
                logging.debug("Response content:\n%s",
                              pprint.pformat(text))
            logging.info("Sent %d bytes and received %d bytes in %s.",
                         submission_size, len(text),
                         format_delta(end_time - start_time))
            return (True, status_url)
Ejemplo n.º 2
0
    def submit_results(self, transport_url, body, headers, timeout):
        transport = HTTPTransport(transport_url)
        start_time = time.time()
        submission_stat = os.fstat(body[0][1].fileno())
        submission_size = submission_stat.st_size

        try:
            response = transport.exchange(body, headers, timeout)
        except (IOError, OSError) as error:
            logging.debug(error)
            return (False, error)

        end_time = time.time()
        if not response:
            error = "Error contacting the server: %s." % transport_url
            logging.debug(error)
            return (False, error)
        elif response.status != 200:
            error = "Server returned unexpected status: %d. " % \
                    response.status
            logging.debug(error)
            return (False, error)
        else:
            #This is the only success block
            text = response.read().decode()
            status_url = json.loads(text)['url']
            if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
                logging.debug("Response headers:\n%s",
                              pprint.pformat(response.getheaders()))
                logging.debug("Response content:\n%s",
                              pprint.pformat(text))
            logging.info("Sent %d bytes and received %d bytes in %s.",
                         submission_size, len(text),
                         format_delta(end_time - start_time))
            return (True, status_url)
    def exchange(self):
        # Encode form data
        form = {}
        for field, value in self._form.items():
            form[field] = str(value).encode("UTF-8")

        # Compress and add payload to form
        payload = open(self._report, "r").read()
        compressed_payload = bz2.compress(payload)
        file = StringIO(compressed_payload)
        file.name = "%s.xml.bz2" % str(gethostname())
        file.size = len(compressed_payload)
        form["field.submission_data"] = file

        if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
            logging.debug("Uncompressed payload length: %d", len(payload))

        transport = HTTPTransport(self.transport_url)

        start_time = time.time()
        response = transport.exchange(form, self._headers,
            timeout=string_to_type(self.timeout))
        end_time = time.time()

        if not response:
            self._manager.reactor.fire("exchange-error", _("""\
Failed to contact server. Please try
again or upload the following file name:
%s

directly to the system database:
https://launchpad.net/+hwdb/+submit""") % posixpath.abspath(self._report))
            return
        elif response.status != 200:
            self._manager.reactor.fire("exchange-error", _("""\
Failed to upload to server,
please try again later."""))
            return

        if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
            logging.debug("Response headers:\n%s",
                pprint.pformat(response.getheaders()))

        header = response.getheader("x-launchpad-hwdb-submission")
        if not header:
            self._manager.reactor.fire("exchange-error",
                _("Information not posted to Launchpad."))
        elif "Error" in header:
            # HACK: this should return a useful error message
            self._manager.reactor.fire("exchange-error", header)
            logging.error(header)
        else:
            text = response.read()
            logging.info("Sent %d bytes and received %d bytes in %s.",
                file.size, len(text), format_delta(end_time - start_time))
Ejemplo n.º 4
0
    def launchpad_exchange(self, interface):
        # Maybe on the next exchange...
        if not self._form["field.system"] \
           or self._form["field.submission_data"]:
            return

        # Compress and add payload to form
        payload = open(self._launchpad_report, "rb").read()
        compressed_payload = bz2.compress(payload)
        file = BytesIO(compressed_payload)
        file.name = "%s.xml.bz2" % gethostname()
        file.size = len(compressed_payload)
        self._form["field.submission_data"] = file

        if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
            logging.debug("Uncompressed payload length: %d", len(payload))

        # Encode form data
        try:
            form = FORM.coerce(self._form)
        except ValueError as e:
            self._manager.reactor.fire(
                "exchange-error", _("""\
Failed to process form: %s""" % e))
            return

        transport = HTTPTransport(self.transport_url)
        start_time = time.time()
        try:
            response = transport.exchange(form,
                                          self._headers,
                                          timeout=self.timeout)
        except Exception as error:
            self._manager.reactor.fire("exchange-error", str(error))
            return
        end_time = time.time()

        if not response:
            self._manager.reactor.fire(
                "exchange-error",
                _("""\
Failed to contact server. Please try
again or upload the following file name:
%s

directly to the system database:
https://launchpad.net/+hwdb/+submit""") %
                posixpath.abspath(self._launchpad_report))
            return
        elif response.status != 200:
            self._manager.reactor.fire(
                "exchange-error",
                _("""\
Failed to upload to server,
please try again later."""))
            return

        if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
            logging.debug("Response headers:\n%s",
                          pprint.pformat(response.getheaders()))

        header = response.getheader("x-launchpad-hwdb-submission")
        if not header:
            self._manager.reactor.fire(
                "exchange-error", _("Information not posted to Launchpad."))
        elif "Error" in header:
            # HACK: this should return a useful error message
            self._manager.reactor.fire("exchange-error", header)
            logging.error(header)
        else:
            text = response.read()
            self._manager.reactor.fire("exchange-success", text)
            logging.info("Sent %d bytes and received %d bytes in %s.",
                         file.size, len(text),
                         format_delta(end_time - start_time))
Ejemplo n.º 5
0
    def launchpad_exchange(self):
        # Maybe on the next exchange...
        if not self._form["field.system"] \
           or self._form["field.submission_data"]:
            return

        # Compress and add payload to form
        payload = open(self._launchpad_report, "rb").read()
        compressed_payload = bz2.compress(payload)
        file = BytesIO(compressed_payload)
        file.name = "%s.xml.bz2" % gethostname()
        file.size = len(compressed_payload)
        self._form["field.submission_data"] = file

        if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
            logging.debug("Uncompressed payload length: %d", len(payload))

        # Encode form data
        try:
            form = FORM.coerce(self._form)
        except ValueError as e:
            self._manager.reactor.fire("exchange-error", _("""\
Failed to process form: %s""" % e))
            return

        transport = HTTPTransport(self.transport_url)
        start_time = time.time()
        try:
            response = transport.exchange(form, self._headers,
                timeout=self.timeout)
        except Exception as error:
            self._manager.reactor.fire("exchange-error", str(error))
            return
        end_time = time.time()

        if not response:
            self._manager.reactor.fire("exchange-error", _("""\
Failed to contact server. Please try
again or upload the following file name:
%s

directly to the system database:
https://launchpad.net/+hwdb/+submit""") % posixpath.abspath(self._launchpad_report))
            return
        elif response.status != 200:
            self._manager.reactor.fire("exchange-error", _("""\
Failed to upload to server,
please try again later."""))
            return

        if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
            logging.debug("Response headers:\n%s",
                pprint.pformat(response.getheaders()))

        header = response.getheader("x-launchpad-hwdb-submission")
        if not header:
            self._manager.reactor.fire("exchange-error",
                _("Information not posted to Launchpad."))
        elif "Error" in header:
            # HACK: this should return a useful error message
            self._manager.reactor.fire("exchange-error", header)
            logging.error(header)
        else:
            text = response.read()
            self._manager.reactor.fire("exchange-success", text)
            logging.info("Sent %d bytes and received %d bytes in %s.",
                file.size, len(text), format_delta(end_time - start_time))
Ejemplo n.º 6
0
directly to the system database:
https://launchpad.net/+hwdb/+submit""") % posixpath.abspath(self._launchpad_report))
            return
        elif response.status != 200:
            self._manager.reactor.fire("exchange-error", _("""\
Failed to upload to server,
please try again later."""))
            return

        if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
            logging.debug("Response headers:\n%s",
                pprint.pformat(response.getheaders()))

        header = response.getheader("x-launchpad-hwdb-submission")
        if not header:
            self._manager.reactor.fire("exchange-error",
                _("Information not posted to Launchpad."))
        elif "Error" in header:
            # HACK: this should return a useful error message
            self._manager.reactor.fire("exchange-error", header)
            logging.error(header)
        else:
            text = response.read()
            self._manager.reactor.fire("exchange-success", text)
            logging.info("Sent %d bytes and received %d bytes in %s.",
                file.size, len(text), format_delta(end_time - start_time))


factory = LaunchpadExchange