Beispiel #1
0
    def publish_file(self, namespace_id, data, archive_path):
        form = MultiPartForm()
        for key in data:
            form.add_field(key, data[key])

        form.add_file('file',
                      os.path.basename(archive_path),
                      fileHandle=codecs.open(archive_path, "rb"))

        url = '%s/namespaces/%s/collection/' % (self.baseurl, namespace_id)
        _, netloc, url, _, _, _ = urlparse(url)
        try:
            form_buffer = form.get_binary().getvalue()
            http = http_client.HTTPConnection(netloc)
            http.connect()
            http.putrequest("POST", url)
            http.putheader('Content-type', form.get_content_type())
            http.putheader('Content-length', str(len(form_buffer)))
            http.endheaders()
            http.send(form_buffer)
        except socket.error as exc:
            exceptions.GalaxyPublishError(
                "Error transferring file to Galaxy server: %s" % str(exc))
        r = http.getresponse()
        if r.status == 201:
            return r.read()
        else:
            exceptions.GalaxyPublishError(
                "Error transferring file to Galaxy server: %s - %s" %
                (r.status, r.reason))
Beispiel #2
0
    def publish_file(self, data, archive_path, publish_api_key):
        form = MultiPartForm()
        for key in data:
            form.add_field(key, data[key])

        form.add_file('file', os.path.basename(archive_path),
                      fileHandle=codecs.open(archive_path, "rb"),
                      mimetype='application/octet-stream')

        url = '%s/collections/' % self.baseurl
        log.debug('url: %s', url)

        _, netloc, url, _, _, _ = urlparse(url)

        try:
            form_buffer = form.get_binary().getvalue()
            http = http_client.HTTPConnection(netloc)
            http.connect()
            http.putrequest("POST", url)
            http.putheader('Content-type', form.get_content_type())
            http.putheader('Content-length', str(len(form_buffer)))

            if publish_api_key:
                http.putheader('Authorization', 'Token %s' % publish_api_key)

            http.endheaders()
            http.send(form_buffer)
        except socket.error as exc:
            log.exception(exc)
            raise exceptions.GalaxyPublishError(
                'Network error while transferring file "%s" to Galaxy server (%s): %s' %
                (archive_path, self.galaxy.server['url'], str(exc)),
                archive_path=archive_path
            )

        r = http.getresponse()

        log.debug('code: %s', r.getcode())
        log.debug('info: %s', r.info())
        log.debug('reason: %s', r.reason)

        response_body = r.read()
        log.debug('response_body: %s', response_body)

        # 202 'Accepted'
        if r.status == 202:
            return response_body
        else:
            raise exceptions.GalaxyPublishError(
                'Error transferring file "%s" to Galaxy server (%s): %s - %s' %
                (archive_path, self.galaxy.server['url'], r.status, r.reason),
                archive_path=archive_path
            )
Beispiel #3
0
    def publish_file(self, data, archive_path, publish_api_key):
        form = MultiPartForm()

        for key in data:
            form.add_field(key, data[key])

        form.add_file('file',
                      os.path.basename(archive_path),
                      fileHandle=codecs.open(archive_path, "rb"),
                      mimetype='application/octet-stream')

        # TODO: figure out how to track API versions finer grained? Ideally
        #       simple enough to not end up with adhoc HATEAOS imp
        #       Maybe just hardcode api ver in calls?
        collection_url_ver = 'v2'
        url = '%s/%s/collections/' % (self.base_api_url, collection_url_ver)

        request_headers = {}

        # TODO: create or use a request.Auth impl
        if publish_api_key:
            request_headers['Authorization'] = 'Token %s' % publish_api_key

        form_buffer = form.get_binary().getvalue()

        request_headers['Content-type'] = form.get_content_type()
        request_headers['Content-length'] = str(len(form_buffer))

        try:

            # TODO: pass in a file-like object and use stream=True
            resp = self.session.post(url,
                                     data=form_buffer,
                                     headers=request_headers,
                                     verify=self._validate_certs)

        except socket.error as exc:
            log.exception(exc)
            raise exceptions.GalaxyPublishError(
                'Network error while transferring file "%s" to Galaxy server (%s): %s'
                % (archive_path, self.galaxy_context.server['url'], str(exc)),
                archive_path=archive_path)

        # 202 'Accepted'
        if resp.status_code == 202:
            # FIXME: return the data instead of the text, ie return resp.json()
            return resp.text
        else:
            raise exceptions.GalaxyPublishError(
                'Error transferring file "%s" to Galaxy server (%s): %s - %s' %
                (archive_path, self.galaxy_context.server['url'],
                 resp.status_code, resp.reason),
                archive_path=archive_path)
Beispiel #4
0
def _publish(galaxy_context,
             archive_path,
             publish_api_key=None,
             display_callback=None):

    results = {'errors': [], 'success': True}

    archive = tarfile.open(archive_path, 'r')
    top_dir = os.path.commonprefix(archive.getnames())
    manifest_path = os.path.join(
        top_dir, collection_artifact_manifest.COLLECTION_MANIFEST_FILENAME)
    try:
        manifest_file = archive.extractfile(manifest_path)
    except tarfile.TarError as exc:
        raise exceptions.GalaxyPublishError(str(exc),
                                            archive_path=archive_path)

    try:
        manifest = collection_artifact_manifest.load(manifest_file)
    except Exception as exc:
        raise exceptions.GalaxyPublishError(str(exc),
                                            archive_path=archive_path)

    # display_callback('Creating publish task for %s from artifact archive %s' %
    #                 (manifest.collection_info.label, archive_path))
    # display_callback(json.dumps(attr.asdict(manifest.collection_info)))

    api = GalaxyAPI(galaxy_context)

    collection_name = manifest.collection_info.name

    data = {
        'sha256': _get_file_checksum(archive_path),
        'name': collection_name,
        'version': manifest.collection_info.version
    }

    log.debug("Publishing file %s with data: %s" %
              (archive_path, json.dumps(data)))

    b_response_body = api.publish_file(data, archive_path, publish_api_key)
    response_body = to_text(b_response_body, errors='surrogate_or_strict')

    # TODO: try/except on json error but let bubble up for now

    response_data = json.loads(response_body)
    results['response_data'] = response_data

    return results
Beispiel #5
0
    def publish_file(self, form, publish_api_key):
        # TODO: at somepoint, get the publish url from api
        collection_url_ver = 'v2'
        url = '%s/%s/collections/' % (self.base_api_url, collection_url_ver)

        request_headers = {}

        # TODO: create or use a request.Auth impl
        if publish_api_key:
            request_headers['Authorization'] = 'Token %s' % publish_api_key

        # FIXME: too tightly coupled
        artifact_path = form.files[0][1]

        try:
            # TODO: pass in a file-like object and use stream=True

            data = self.post_multipart_form(url,
                                            form=form,
                                            headers=request_headers)

            return data
        except exceptions.GalaxyRestAPIError as exc:
            log.exception(exc)

            error_msg = exc.message

            raise exceptions.GalaxyPublishError(error_msg,
                                                archive_path=artifact_path,
                                                url=url)
Beispiel #6
0
def _publish(galaxy_context, archive_path, display_callback=None):

    results = {'errors': [], 'success': True}

    archive = tarfile.open(archive_path, 'r')
    top_dir = os.path.commonprefix(archive.getnames())
    manifest_path = os.path.join(
        top_dir, collection_artifact_manifest.COLLECTION_MANIFEST_FILENAME)
    try:
        manifest_file = archive.extractfile(manifest_path)
    except tarfile.TarError as exc:
        raise exceptions.GalaxyPublishError(str(exc),
                                            archive_path=archive_path)

    try:
        manifest = collection_artifact_manifest.load(manifest_file)
    except Exception as exc:
        raise exceptions.GalaxyPublishError(str(exc),
                                            archive_path=archive_path)

    display_callback(json.dumps(attr.asdict(manifest.collection_info)))

    api = GalaxyAPI(galaxy_context)

    collection_name = manifest.collection_info.name
    namespace_name = manifest.collection_info.namespace

    log.debug("Attempting to fetch Galaxy namespace %s" % namespace_name)
    namespace = api.fetch_namespace(namespace_name)

    namespace_id = namespace.get('id')
    data = {
        'sha256': _get_file_checksum(archive_path),
        'name': collection_name,
        'version': manifest.collection_info.version
    }
    log.debug("Publishing file %s with data: %s" %
              (archive_path, json.dumps(data)))
    api.publish_file(namespace_id, data, archive_path)
    return results