Ejemplo n.º 1
0
def create_version_from_metadata(
    channel_name: str,
    user_id: bytes,
    package_file_name: str,
    package_data: dict,
    dao: Dao,
):
    package_name = package_data["name"]
    package = dao.get_package(channel_name, package_name)
    if not package:
        package_info = rest_models.Package(
            name=package_name,
            summary=package_data.get("summary", ""),
            description=package_data.get("description", ""),
        )
        dao.create_package(channel_name, package_info, user_id, "owner")

    pkg_format = "tarbz2" if package_file_name.endswith(
        ".tar.bz2") else ".conda"
    version = dao.create_version(
        channel_name,
        package_name,
        pkg_format,
        get_subdir_compat(package_data),
        package_data["version"],
        int(package_data["build_number"]),
        package_data["build"],
        package_file_name,
        json.dumps(package_data),
        user_id,
        package_data["size"],
    )

    return version
Ejemplo n.º 2
0
def package_version(db, user, channel_name, package_name, public_package,
                    dao: Dao, config: Config):

    pkgstore = config.get_package_store()
    filename = Path("test-package-0.1-0.tar.bz2")
    with open(filename, 'rb') as fid:
        pkgstore.add_file(fid.read(), channel_name, 'linux-64' / filename)
    package_format = "tarbz2"
    package_info = "{}"
    version = dao.create_version(
        channel_name,
        package_name,
        package_format,
        "linux-64",
        "0.1",
        0,
        "",
        str(filename),
        package_info,
        user.id,
    )

    yield version

    db.delete(version)
    db.commit()
Ejemplo n.º 3
0
def package_version(db, user, channel_name, package_name, dao: Dao):
    channel_data = Channel(name=channel_name, private=False)
    package_data = Package(name=package_name)

    channel = dao.create_channel(channel_data, user.id, "owner")
    package = dao.create_package(channel_name, package_data, user.id, "owner")
    package_format = "tarbz2"
    package_info = "{}"
    version = dao.create_version(
        channel_name,
        package_name,
        package_format,
        "linux-64",
        "0.1",
        0,
        "",
        "",
        package_info,
        user.id,
    )

    yield version

    db.delete(version)
    db.delete(package)
    db.delete(channel)
    db.commit()
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
def test_package_version(db, dao: Dao, user, channel_name, package_name):
    channel_data = Channel(name=channel_name, private=False)
    package_data = Package(name=package_name)

    dao.create_channel(channel_data, user.id, "owner")
    package = dao.create_package(channel_name, package_data, user.id, "owner")
    package_format = "tarbz2"
    package_info = "{}"

    versions = [
        ("0.1.0", 0),
        ("1.0.0", 0),
        ("0.0.1", 0),
        ("0.0.2", 0),
        ("0.0.3", 0),
        ("1.0.0", 1),
        ("1.0.0", 2),
        ("0.1.0", 5),
        ("0.1.0", 2),
    ]
    for ver, build_str in versions:
        dao.create_version(
            channel_name,
            package_name,
            package_format,
            "linux-64",
            ver,
            build_str,
            "",
            f"{package_name}-{ver}-{build_str}.tar.bz2",
            package_info,
            user.id,
            size=0,
        )
    res = dao.get_package_versions(package)
    res_versions = [(VersionOrder(x[0].version), x[0].build_number)
                    for x in res]

    assert sorted(res_versions, reverse=True) == res_versions
Ejemplo n.º 6
0
def post_file(
        files: List[UploadFile] = File(...),
        package: db_models.Package = Depends(get_package_or_fail),
        dao: Dao = Depends(get_dao),
        auth: authorization.Rules = Depends(get_rules)):
    auth.assert_upload_file(package.channel.name, package.name)

    channel_dir = f'static/channels/{package.channel.name}'
    for file in files:
        with tarfile.open(fileobj=file.file._file, mode="r:bz2") as tar:
            info = json.load(tar.extractfile('info/index.json'))

        parts = file.filename.split('-')
        if parts[0] != package.name or info['name'] != package.name:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)

        dir = f'{channel_dir}/{info["subdir"]}/'
        os.makedirs(dir, exist_ok=True)

        file.file._file.seek(0)
        with open(f'{dir}/{file.filename}', 'wb') as my_file:
            shutil.copyfileobj(file.file, my_file)

        user_id = auth.assert_user()

        dao.create_version(
            package=package,
            platform=info['subdir'],
            version=info['version'],
            build_number=info['build_number'],
            build_string=info['build'],
            filename=file.filename,
            info=json.dumps(info),
            uploader_id=user_id)

    subprocess.run(['conda', 'index', channel_dir])
Ejemplo n.º 7
0
def package_version(
    db,
    user,
    public_channel,
    channel_name,
    package_name,
    public_package,
    dao: Dao,
    config: Config,
):

    pkgstore = config.get_package_store()
    filename = Path(
        __file__).parent / "data" / "xtensor-io-0.10.3-hb585cf6_0.tar.bz2"
    with open(filename, "rb") as fid:
        pkgstore.add_file(fid.read(), channel_name, "linux-64" / filename)
    package_format = "tarbz2"
    package_info = "{}"
    version = dao.create_version(
        channel_name,
        package_name,
        package_format,
        "linux-64",
        "0.1",
        0,
        "",
        str(filename),
        package_info,
        user.id,
        size=11,
    )

    dao.update_channel_size(channel_name)
    db.refresh(public_channel)

    yield version

    try:
        db.delete(version)
        db.commit()
    except Exception:
        pass