Example #1
0
def synchronize(remote_pk, repository_pk):
    """
    Sync content from the remote repository.

    Create a new version of the repository that is synchronized with the remote.

    Args:
        remote_pk (str): The remote PK.
        repository_pk (str): The repository PK.

    Raises:
        ValueError: If the remote does not specify a url to sync.

    """
    remote = RpmRemote.objects.get(pk=remote_pk)
    repository = RpmRepository.objects.get(pk=repository_pk)

    if not remote.url:
        raise ValueError(
            _('A remote must have a url specified to synchronize.'))

    log.info(
        _('Synchronizing: repository={r} remote={p}').format(r=repository.name,
                                                             p=remote.name))

    deferred_download = (remote.policy != Remote.IMMEDIATE
                         )  # Interpret download policy

    treeinfo = get_treeinfo_data(remote)
    if treeinfo:
        treeinfo["repositories"] = {}
        for repodata in set(treeinfo["download"]["repodatas"]):
            if repodata == ".":
                treeinfo["repositories"].update({repodata: str(repository_pk)})
                continue
            name = f"{repodata}-{treeinfo['hash']}"
            sub_repo, created = RpmRepository.objects.get_or_create(
                name=name, sub_repo=True)
            if created:
                sub_repo.save()
            treeinfo["repositories"].update({repodata: str(sub_repo.pk)})
            path = f"{repodata}/"
            new_url = urljoin(remote.url, path)
            if repodata_exists(remote, new_url):
                stage = RpmFirstStage(remote,
                                      deferred_download,
                                      new_url=new_url)
                dv = RpmDeclarativeVersion(first_stage=stage,
                                           repository=sub_repo)
                dv.create()

    first_stage = RpmFirstStage(remote, deferred_download, treeinfo=treeinfo)
    dv = RpmDeclarativeVersion(first_stage=first_stage, repository=repository)
    dv.create()
Example #2
0
def synchronize(remote_pk, repository_pk, mirror, skip_types, optimize):
    """
    Sync content from the remote repository.

    Create a new version of the repository that is synchronized with the remote.

    Args:
        remote_pk (str): The remote PK.
        repository_pk (str): The repository PK.
        mirror (bool): Mirror mode.
        skip_types (list): List of content to skip.
        optimize(bool): Optimize mode.

    Raises:
        ValueError: If the remote does not specify a url to sync.

    """
    remote = RpmRemote.objects.get(pk=remote_pk)
    repository = RpmRepository.objects.get(pk=repository_pk)

    if not remote.url:
        raise ValueError(
            _("A remote must have a url specified to synchronize."))

    log.info(
        _("Synchronizing: repository={r} remote={p}").format(r=repository.name,
                                                             p=remote.name))

    deferred_download = remote.policy != Remote.IMMEDIATE  # Interpret download policy

    with WorkingDirectory():
        remote_url = fetch_remote_url(remote)
    if not remote_url:
        raise ValueError(_("A no valid remote URL was provided."))
    else:
        remote_url = remote_url.rstrip("/") + "/"

    if optimize and is_optimized_sync(repository, remote, remote_url):
        return

    with WorkingDirectory():
        treeinfo = get_treeinfo_data(remote, remote_url)
    if treeinfo:
        treeinfo["repositories"] = {}
        for repodata in set(treeinfo["download"]["repodatas"]):
            if repodata == DIST_TREE_MAIN_REPO_PATH:
                treeinfo["repositories"].update({repodata: None})
                continue
            name = f"{repodata}-{treeinfo['hash']}"
            sub_repo, created = RpmRepository.objects.get_or_create(
                name=name, sub_repo=True)
            if created:
                sub_repo.save()
            directory = treeinfo["repo_map"][repodata]
            treeinfo["repositories"].update({directory: str(sub_repo.pk)})
            path = f"{repodata}/"
            new_url = urljoin(remote_url, path)
            with WorkingDirectory():
                repodata_exists = get_repomd_file(remote, new_url)
            if repodata_exists:
                if optimize and is_optimized_sync(sub_repo, remote, new_url):
                    continue
                stage = RpmFirstStage(
                    remote,
                    sub_repo,
                    deferred_download,
                    skip_types=skip_types,
                    new_url=new_url,
                )
                dv = RpmDeclarativeVersion(first_stage=stage,
                                           repository=sub_repo)
                dv.create()
                sub_repo.last_sync_remote = remote
                sub_repo.last_sync_repo_version = sub_repo.latest_version(
                ).number
                sub_repo.save()

    first_stage = RpmFirstStage(
        remote,
        repository,
        deferred_download,
        skip_types=skip_types,
        treeinfo=treeinfo,
        new_url=remote_url,
    )
    dv = RpmDeclarativeVersion(first_stage=first_stage,
                               repository=repository,
                               mirror=mirror)
    dv.create()
    repository.last_sync_remote = remote
    repository.last_sync_repo_version = repository.latest_version().number
    repository.save()