Verified Commit d1fa043d authored by David Runge's avatar David Runge
Browse files

Add operation to create a db from JSON files

Add `create_db_from_json_files()` to create a repository database from
JSON files in a directory.
Change `db_file_as_models()` to rely on
`models.PackageDesc.get_output_packages()` instead of
`convert._transform_package_desc_to_output_package()` and add a missing
parameter (base) to the instantiation of `models.OutputPackageBase()`.

Add the two fixtures `dummy_json_files_in_dir()` and `empty_file()` to
create a few (valid) dummy input JSON files in a temporary directory and
an empty file (respectively).
Add a test for `create_db_from_json_files()`.
parent 3103d0f7
...@@ -36,21 +36,16 @@ def db_file_as_models(db_path: Path, compression: str = "gz") -> Iterator[Tuple[ ...@@ -36,21 +36,16 @@ def db_file_as_models(db_path: Path, compression: str = "gz") -> Iterator[Tuple[
for (name, package_desc) in package_descs.items(): for (name, package_desc) in package_descs.items():
if packages.get(package_desc.base): if packages.get(package_desc.base):
packages[package_desc.base].packages += [ packages[package_desc.base].packages += [package_desc.get_output_package(files=package_files.get(name))]
convert._transform_package_desc_to_output_package(desc=package_desc, files=package_files.get(name))
else: else:
packages.update( packages.update(
{ {
package_desc.base: models.OutputPackageBase( package_desc.base: models.OutputPackageBase(
makedepends=package_desc.makedepends, makedepends=package_desc.makedepends,
packager=package_desc.packager, packager=package_desc.packager,
version=package_desc.version, version=package_desc.version,
packages=[ packages=[package_desc.get_output_package(files=package_files.get(name))],
desc=package_desc, files=package_files.get(name)
) )
} }
) )
...@@ -78,3 +73,32 @@ def dump_db_to_json_files(input_path: Path, output_path: Path) -> None: ...@@ -78,3 +73,32 @@ def dump_db_to_json_files(input_path: Path, output_path: Path) -> None:
model.dict(), option=orjson.OPT_INDENT_2 | orjson.OPT_APPEND_NEWLINE | orjson.OPT_SORT_KEYS model.dict(), option=orjson.OPT_INDENT_2 | orjson.OPT_APPEND_NEWLINE | orjson.OPT_SORT_KEYS
) )
) )
def create_db_from_json_files(
input_path: Path, output_path: Path, db_type: defaults.RepoDbType = defaults.RepoDbType.DEFAULT
) -> None:
"""Create a repository database from a list of JSON files found in a directory
input_path: Path
A directory from which to read JSON files
output_path: Path
A file to which to write a repository database
db_type: defaults.RepoDbType
A member of the defaults.RepoDbType IntEnum to define what type of repository database to create:
Either defaults.RepoDbType.DEFAULT for the default .db database or defaults.RepoDbType.FILES for the .files
database (defaults to defaults.RepoDbType.DEFAULT)
repodbfile = convert.RepoDbFile()
database = files._write_db_file(path=output_path)
for path in files._json_files_in_directory(path=input_path):
model = files._read_pkgbase_json_file(path)
...@@ -8,7 +8,7 @@ from pytest import fixture ...@@ -8,7 +8,7 @@ from pytest import fixture
from repo_management import models, operations from repo_management import models, operations
from .fixtures import create_db_file from .fixtures import create_db_file, create_json_files
@fixture(scope="function") @fixture(scope="function")
...@@ -25,6 +25,19 @@ def create_dir_path() -> Iterator[Path]: ...@@ -25,6 +25,19 @@ def create_dir_path() -> Iterator[Path]:
shutil.rmtree(temp_dir) shutil.rmtree(temp_dir)
def dummy_json_files_in_dir() -> Iterator[Path]:
temp_dir = create_json_files()
yield temp_dir
def empty_file() -> Iterator[Path]:
[foo, file_name] = tempfile.mkstemp()
yield Path(file_name)
def test_db_file_as_models(create_gz_db_file: Path) -> None: def test_db_file_as_models(create_gz_db_file: Path) -> None:
for (name, model) in operations.db_file_as_models(db_path=create_gz_db_file): for (name, model) in operations.db_file_as_models(db_path=create_gz_db_file):
assert isinstance(name, str) assert isinstance(name, str)
...@@ -36,3 +49,7 @@ def test_dump_db_to_json_files( ...@@ -36,3 +49,7 @@ def test_dump_db_to_json_files(
create_dir_path: Path, create_dir_path: Path,
) -> None: ) -> None:
operations.dump_db_to_json_files(input_path=create_gz_db_file, output_path=create_dir_path) operations.dump_db_to_json_files(input_path=create_gz_db_file, output_path=create_dir_path)
def test_create_db_from_json_files(dummy_json_files_in_dir: Path, empty_file: Path) -> None:
operations.create_db_from_json_files(input_path=dummy_json_files_in_dir, output_path=empty_file)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment