Ejemplo n.º 1
0
    def upload_file_object(self, infd, completion_routine=None, subpath=None):
        status = location.Status()
        try:
            if completion_routine is None:
                completion_routine = lambda x: x

            full_path = self.full_path
            if subpath:
                full_path = os.path.join(full_path, subpath.lstrip(os.path.sep))

            dirname = os.path.dirname(full_path)
            try:
                os.makedirs(dirname)
            except (OSError, IOError):
                pass

            with open(full_path, "wb") as outfd:
                while 1:
                    data = infd.read(self.BUFFSIZE)
                    if not data:
                        break

                    outfd.write(data)

            self._session.logging.warn("Uploaded %s", full_path)

        except Exception as e:
            self._session.logging.warn("Unable to write %s: %s", full_path, e)
            status = location.Status(500, unicode(e))

        # Done - report status.
        completion_routine(status)
Ejemplo n.º 2
0
    def upload_local_file(self, local_filename, completion_routine=None,
                          delete=True):
        status = location.Status()
        try:
            if completion_routine is None:
                completion_routine = lambda x: x

            # Only copy the files if they are not the same.
            if local_filename != self.full_path:
                self._ensure_dir_exists()

                with open(local_filename, "rb") as infd:
                    with open(self.full_path, "wb") as outfd:
                        while 1:
                            data = infd.read(self.BUFFSIZE)
                            if not data:
                                break

                            outfd.write(data)

                # Remove the local copy if the caller does not care about it any
                # more.
                if delete:
                    self._session.logging.debug("Removing local file %s",
                                            local_filename)
                    os.unlink(local_filename)

        except Exception as e:
            status = location.Status(500, unicode(e))

        # Done - report status.
        completion_routine(status)
Ejemplo n.º 3
0
    def _report_error(self, completion_routine, response=None, message=None):
        if response:
            # Only include the text in case of error.
            if not response.ok:
                status = location.Status(response.status_code, response.text)
            else:
                status = location.Status(response.status_code)

        else:
            status = location.Status(500, message)

        if response is None or not response.ok:
            if completion_routine:
                return completion_routine(status)

            raise IOError(response.text)
        else:
            if completion_routine:
                completion_routine(status)

        return response.ok