Ejemplo n.º 1
0
    def delete(self,
               cascade_delete=None,
               access_key=None,
               secret_key=None,
               verbose=None,
               debug=None,
               retries=None,
               headers=None):
        """Delete a file from the Archive. Note: Some files -- such as
        <itemname>_meta.xml -- cannot be deleted.

        :type cascade_delete: bool
        :param cascade_delete: (optional) Also deletes files derived from the file, and
                               files the file was derived from.

        :type access_key: str
        :param access_key: (optional) IA-S3 access_key to use when making the given
                           request.

        :type secret_key: str
        :param secret_key: (optional) IA-S3 secret_key to use when making the given
                           request.

        :type verbose: bool
        :param verbose: (optional) Print actions to stdout.

        :type debug: bool
        :param debug: (optional) Set to True to print headers to stdout and exit exit
                      without sending the delete request.

        """
        cascade_delete = '0' if not cascade_delete else '1'
        access_key = self.item.session.access_key if not access_key else access_key
        secret_key = self.item.session.secret_key if not secret_key else secret_key
        debug = False if not debug else debug
        verbose = False if not verbose else verbose
        max_retries = 2 if retries is None else retries
        headers = dict() if headers is None else headers

        if 'x-archive-cascade-delete' not in headers:
            headers['x-archive-cascade-delete'] = cascade_delete

        url = '{0}//s3.us.archive.org/{1}/{2}'.format(
            self.item.session.protocol, self.identifier,
            urllib.parse.quote(self.name))
        self.item.session.mount_http_adapter(max_retries=max_retries,
                                             status_forcelist=[503],
                                             host='s3.us.archive.org')
        request = iarequest.S3Request(method='DELETE',
                                      url=url,
                                      headers=headers,
                                      access_key=access_key,
                                      secret_key=secret_key)
        if debug:
            return request
        else:
            if verbose:
                msg = ' deleting: {0}'.format(self.name)
                if cascade_delete:
                    msg += ' and all derivative files.'
                print(msg, file=sys.stderr)
            prepared_request = self.item.session.prepare_request(request)

            try:
                resp = self.item.session.send(prepared_request)
                resp.raise_for_status()
            except (RetryError, HTTPError, ConnectTimeout, ConnectionError,
                    socket.error, ReadTimeout) as exc:
                error_msg = 'Error deleting {0}, {1}'.format(url, exc)
                log.error(error_msg)
                raise
            else:
                return resp
            finally:
                # The retry adapter is mounted to the session object.
                # Make sure to remove it after delete, so it isn't
                # mounted if and when the session object is used for an
                # upload. This is important because we use custom retry
                # handling for IA-S3 uploads.
                url_prefix = '{0}//s3.us.archive.org'.format(
                    self.item.session.protocol)
                del self.item.session.adapters[url_prefix]
Ejemplo n.º 2
0
    def delete(self,
               cascade_delete=None,
               access_key=None,
               secret_key=None,
               verbose=None,
               debug=None):
        """Delete a file from the Archive. Note: Some files -- such as
        <itemname>_meta.xml -- cannot be deleted.

        :type cascade_delete: bool
        :param cascade_delete: (optional) Also deletes files derived from the file, and
                               files the file was derived from.

        :type access_key: str
        :param access_key: (optional) IA-S3 access_key to use when making the given
                           request.

        :type secret_key: str
        :param secret_key: (optional) IA-S3 secret_key to use when making the given
                           request.

        :type verbose: bool
        :param verbose: (optional) Print actions to stdout.

        :type debug: bool
        :param debug: (optional) Set to True to print headers to stdout and exit exit
                      without sending the delete request.

        """
        cascade_delete = False if not cascade_delete else True
        access_key = self.item.session.access_key if not access_key else access_key
        secret_key = self.item.session.secret_key if not secret_key else secret_key
        debug = False if not debug else debug
        verbose = False if not verbose else verbose

        url = 'http://s3.us.archive.org/{0}/{1}'.format(
            self.identifier, self.name)
        request = iarequest.S3Request(
            method='DELETE',
            url=url,
            headers={'x-archive-cascade-delete': int(cascade_delete)},
            access_key=access_key,
            secret_key=secret_key)
        if debug:
            return request
        else:
            if verbose:
                msg = ' deleting: {0}'.format(self.name)
                if cascade_delete:
                    msg += ' and all derivative files.'
                print(msg, file=sys.stderr)
            prepared_request = request.prepare()

            try:
                resp = self.item.session.send(prepared_request)
                resp.raise_for_status()
            except HTTPError as e:
                error_msg = 'Error deleting {0}, {1}'.format(resp.url, e)
                log.error(error_msg)
                raise
            finally:
                return resp