Exemplo n.º 1
0
def transmutation(package_version: dict, config, pkgstore: PackageStore, dao: Dao):
    filename: str = package_version["filename"]
    channel: str = package_version["channel_name"]
    package_format: str = package_version["package_format"]
    package_name: str = package_version["package_name"]
    platform = package_version["platform"]
    version = package_version["version"]
    build_number = package_version["build_number"]
    build_string = package_version["build_string"]
    uploader_id = package_version["uploader_id"]
    info = json.loads(package_version["info"])

    if package_format == "tarbz2" or not filename.endswith(".tar.bz2"):
        return

    fh = pkgstore.serve_path(channel, Path(platform) / filename)

    with TemporaryDirectory() as tmpdirname:
        local_file_name = os.path.join(tmpdirname, filename)
        with open(local_file_name, "wb") as local_file:
            # chunk size 10MB
            shutil.copyfileobj(fh, local_file, 10 * 1024 * 1024)

        fn, out_fn, errors = _convert(local_file_name, ".conda", tmpdirname, force=True)

        if errors:
            logger.error(f"transmutation errors --> {errors}")
            return

        filename_conda = os.path.basename(filename).replace('.tar.bz2', '.conda')

        logger.info(f"Adding file to package store: {Path(platform) / filename_conda}")

        with open(out_fn, 'rb') as f:
            calculate_file_hashes_and_size(info, f)
            f.seek(0)
            pkgstore.add_package(f, channel, str(Path(platform) / filename_conda))

        version = dao.create_version(
            channel,
            package_name,
            "conda",
            platform,
            version,
            build_number,
            build_string,
            filename_conda,
            json.dumps(info),
            uploader_id,
            info["size"],
            upsert=True,
        )

        if os.path.exists(out_fn):
            os.remove(out_fn)
Exemplo n.º 2
0
def download_remote_file(repository: RemoteRepository, pkgstore: PackageStore,
                         channel: str, path: str):
    """Download a file from a remote repository to a package store"""

    # Check if a download is already underway for this file
    lock = pkgstore.get_download_lock(channel, path)
    if lock:
        # Wait for the download to complete
        lock.acquire()
        # Release the lock so that any other clients can also finish
        lock.release()
        return
    # Acquire a lock to prevent multiple concurrent downloads of the same file
    with pkgstore.create_download_lock(channel, path):
        logger.debug(f"Downloading {path} from {channel} to pkgstore")
        remote_file = repository.open(path)
        data_stream = remote_file.file
        pkgstore.add_package(data_stream, channel, path)
    pkgstore.delete_download_lock(channel, path)