Esempio n. 1
0
async def _read_pkgbase_json_file(path: Path) -> models.OutputPackageBase:
    """Read a JSON file that represents a pkgbase and return it as models.OutputPackageBase

    Parameters
    ----------
    path: Path
        A Path to to a JSON file

    Raises
    ------
    errors.RepoManagementFileError
        If the JSON file can not be decoded
    errors.RepoManagementValidationError
        If the JSON file can not be validated using models.OutputPackageBase

    Returns
    -------
    models.OutputPackageBase
        A pydantic model representing a pkgbase
    """

    async with aiofiles.open(path, "r") as input_file:
        try:
            return models.OutputPackageBase(
                **orjson.loads(await input_file.read()))
        except orjson.JSONDecodeError as e:
            raise errors.RepoManagementFileError(
                f"The JSON file '{path}' could not be decoded!\n{e}")
        except ValidationError as e:
            raise errors.RepoManagementValidationError(
                f"The JSON file '{path}' could not be validated!\n{e}")
Esempio n. 2
0
def create_json_files(path: py.path.local) -> None:
    for name in ["foo", "bar", "baz"]:
        model = models.OutputPackageBase(
            base=name,
            packager="someone",
            version="1.0.0-1",
            packages=[
                models.OutputPackage(
                    arch="foo",
                    builddate=1,
                    csize=0,
                    desc="description",
                    filename="foo.pkg.tar.zst",
                    files=["foo", "bar", "baz"],
                    isize=1,
                    license=["foo"],
                    md5sum="foo",
                    name=name,
                    pgpsig="foo",
                    sha256sum="foo",
                    url="foo",
                )
            ],
        )

        output_file = tempfile.NamedTemporaryFile(mode="wb", suffix=".json", dir=path, delete=False)
        output_file.write(
            orjson.dumps(model.dict(), option=orjson.OPT_INDENT_2 | orjson.OPT_APPEND_NEWLINE | orjson.OPT_SORT_KEYS)
        )
Esempio n. 3
0
async def db_file_as_models(
    db_path: Path, compression: str = "gz"
) -> AsyncIterator[Tuple[str, models.OutputPackageBase]]:
    """Read a repository database and yield the name of each pkgbase and the respective data (represented as an instance
    of models.OutputPackageBase) in a Tuple.

    Parameters
    ----------
    db_path: Path
        A Path representing a repository database file
    compression: str
        The compression used for the repository database file (support depends on tarfile). Defaults to "gz" (gzip
        compression)

    Returns
    -------
    Iterator[Tuple[str, models.OutputPackageBase]]:
        A Tuple holding the name of a pkgbase and its accompanying data in an instance of models.OutputPackageBase
    """

    packages: Dict[str, models.OutputPackageBase] = {}
    package_descs: Dict[str, models.PackageDesc] = {}
    package_files: Dict[str, models.Files] = {}
    async for member in files._db_file_member_as_model(  # pragma: no cover
        db_file=await files._read_db_file(db_path=db_path, compression=compression)
    ):
        if member.member_type == defaults.RepoDbMemberType.DESC:
            package_descs.update({member.name: await convert._desc_data_to_model(member.data)})
        if member.member_type == defaults.RepoDbMemberType.FILES:
            package_files.update({member.name: await convert._files_data_to_model(member.data)})

    for (name, package_desc) in package_descs.items():
        if packages.get(package_desc.base):
            packages[package_desc.base].packages += [package_desc.get_output_package(files=package_files.get(name))]
        else:
            packages.update(
                {
                    package_desc.base: models.OutputPackageBase(
                        base=package_desc.base,
                        makedepends=package_desc.makedepends,
                        packager=package_desc.packager,
                        version=package_desc.version,
                        packages=[package_desc.get_output_package(files=package_files.get(name))],
                    )
                }
            )

    for (name, package) in packages.items():
        yield (name, package)
                        version="foo",
                    ),
                    models.Files(files=["foo", "bar"]),
                ),
            ],
            models.OutputPackageBase(
                base="foo",
                packager="foo",
                version="foo",
                packages=[
                    models.OutputPackage(
                        arch="foo",
                        builddate=1,
                        csize=1,
                        desc="foo",
                        filename="foo",
                        files=["foo", "bar"],
                        isize=1,
                        license=["foo"],
                        md5sum="foo",
                        name="foo",
                        pgpsig="foo",
                        sha256sum="foo",
                        url="foo",
                    ),
                ],
            ),
        ),
    ],
)
@mark.asyncio
async def test_output_package_base_get_packages_as_models(