Пример #1
0
    def _upload_artifact(self, local_artifact, path_prefix, repo_id, hostname_for_download=None, use_direct_put=False):

        filename = os.path.basename(local_artifact.local_path)
        logger.info('-> Uploading %s', filename)
        logger.debug('local artifact: %s', local_artifact)

        # rgavf stands for repo-group-local_artifact-version-filename
        gavf = '{group}/{name}/{ver}/{filename}'.format(group=local_artifact.group.replace('.', '/'),
                                                        name=local_artifact.artifact, ver=local_artifact.version,
                                                        filename=filename)
        rgavf = '{repo_id}/{gavf}'.format(repo_id=repo_id, gavf=gavf)

        with open(local_artifact.local_path, 'rb') as f:
            if not use_direct_put:
                data = {
                    'g':local_artifact.group,
                    'a':local_artifact.artifact,
                    'v':local_artifact.version,
                    'r':repo_id,
                    'e': local_artifact.extension,
                    'p': local_artifact.extension,
                    'c': local_artifact.classifier,
                    'hasPom': 'false'
                }


                data_list = list(data.items())
                data_list.append( ('file', (filename, f, 'text/plain') ))
                m_for_logging = MultipartEncoder(fields=data_list)
                logger.debug('payload: %s', m_for_logging.to_string())

                f.seek(0)
                m = MultipartEncoder(fields=data_list)
                headers = {'Content-Type': m.content_type}

                self._send('service/local/artifact/maven/content', method='POST', data=m, headers=headers)

                result = RemoteArtifact(group=local_artifact.group, artifact=local_artifact.artifact,
                                      version=local_artifact.version, classifier=local_artifact.classifier,
                                      extension=local_artifact.extension, repo_id=repo_id)
                self.resolve_artifact(result)
                return result

            else:
                headers = {'Content-Type': 'application/x-rpm'}
                remote_path = '{path_prefix}/{rgavf}'.format(path_prefix=path_prefix, rgavf=rgavf)
                self._send(remote_path, method='PUT', headers=headers, data=f)

                # if not specified, use repository url
                hostname_for_download = hostname_for_download or self._repository_url
                url = '{hostname}/content/repositories/{rgavf}'.format(hostname=hostname_for_download, rgavf=rgavf)

                # get classifier and extension from nexus
                path = 'service/local/repositories/{repo_id}/content/{gavf}?describe=maven2'.format(repo_id=repo_id, gavf=gavf)
                maven_metadata = self._send_json(path)['data']

                return RemoteArtifact(group=maven_metadata['groupId'], artifact=maven_metadata['artifactId'],
                                      version=maven_metadata['version'], classifier=maven_metadata.get('classifier', ''),
                                      extension=maven_metadata.get('extension', ''), url=url, repo_id=repo_id)
Пример #2
0
    def release_staging_repo(self,
                             repo_id,
                             description='No description',
                             auto_drop_after_release=True,
                             keep_metadata=False):
        """
        Releases all contents of a staging repository to a release repository which this staging repository targets.

        :param repo_id: id of staging repository
        :param description:
        :param auto_drop_after_release: set this to True if you want to delete the staging repository after releasing
        :param keep_metadata: Keeps custom maven metadata of artifacts after release. Works only there is list of
         artifacts created by upload_artifacts_to_new_staging with upload_filelist=False. It is because current Nexus 2.x
         can't do keep the metadata after release, so we manually read the metadata, release and then set them again.
        :return:
        """
        if keep_metadata:
            # download list of artifacts
            resp = self._send(
                'content/repositories/{repo_id}/{filelist_path}'.format(
                    repo_id=repo_id,
                    filelist_path=self._get_filelist_path(repo_id)))

            artifacts = [
                RemoteArtifact.from_repo_id_and_coordinates(repo_id,
                                                            coordinates=coords)
                for coords in resp.text.split('\n')
            ]

            # download metadata for all files
            for artifact in artifacts:
                artifact.metadata = self.get_artifact_metadata(artifact)

            release_repo_id = self._get_target_repository(repo_id)

        data = {
            'data': {
                'stagedRepositoryIds': [repo_id],
                'description': description,
                'autoDropAfterRelease': auto_drop_after_release
            }
        }
        result = self._send_json('service/local/staging/bulk/promote',
                                 data,
                                 method='POST')

        if keep_metadata:
            for artifact in artifacts:
                artifact.repo_id = release_repo_id
                self.set_artifact_metadata(artifact, artifact.metadata)

        return result
Пример #3
0
    def release_staging_repo(self, repo_id, description='No description', auto_drop_after_release=True,
                             keep_metadata=False):
        """
        Releases all contents of a staging repository to a release repository which this staging repository targets.

        :param repo_id: id of staging repository
        :param description:
        :param auto_drop_after_release: set this to True if you want to delete the staging repository after releasing
        :param keep_metadata: Keeps custom maven metadata of artifacts after release. Works only there is list of
         artifacts created by upload_artifacts_to_new_staging with upload_filelist=False. It is because current Nexus 2.x
         can't do keep the metadata after release, so we manually read the metadata, release and then set them again.
        :return:
        """
        if keep_metadata:
            # download list of artifacts
            resp = self._send('content/repositories/{repo_id}/{filelist_path}'.format(repo_id=repo_id,
                                                                                      filelist_path=self._get_filelist_path(repo_id)))

            artifacts = [RemoteArtifact.from_repo_id_and_coordinates(repo_id, coordinates=coords)
                        for coords in resp.text.split('\n')]

            # download metadata for all files
            for artifact in artifacts:
                artifact.metadata = self.get_artifact_metadata(artifact)

            release_repo_id = self._get_target_repository(repo_id)

        data = {'data': {'stagedRepositoryIds': [repo_id], 'description': description,
                         'autoDropAfterRelease': auto_drop_after_release}}
        result = self._send_json('service/local/staging/bulk/promote', data, method='POST')

        if keep_metadata:
            for artifact in artifacts:
                artifact.repo_id = release_repo_id
                self.set_artifact_metadata(artifact, artifact.metadata)

        return result