Skip to content

Commit

Permalink
fix: add toml-to-toml converter and fix test-tomls
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Louis Fuchs committed May 30, 2024
1 parent b878fce commit b061daf
Show file tree
Hide file tree
Showing 16 changed files with 282 additions and 179 deletions.
42 changes: 41 additions & 1 deletion pyaptly/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def publish(**kwargs):
publish.publish(cfg, args=fake_args)


@cli.command(help="convert yaml- to toml-comfig")
@cli.command()
@click.option("--debug/--no-debug", "-d/-nd", default=False, type=bool)
@click.argument(
"yaml_path",
Expand Down Expand Up @@ -205,3 +205,43 @@ def yaml_to_toml(yaml_path: Path, toml_path: Path, add_defaults: bool, debug: bo
toml_path,
add_defaults=add_defaults,
)


@cli.command()
@click.option("--debug/--no-debug", "-d/-nd", default=False, type=bool)
@click.argument(
"in_path",
type=click.Path(
file_okay=True,
dir_okay=False,
exists=True,
readable=True,
path_type=Path,
),
)
@click.argument(
"toml_path",
type=click.Path(
file_okay=True,
dir_okay=False,
exists=False,
writable=True,
path_type=Path,
),
)
@click.option(
"-a/-na",
"--add-defaults/--no-add-defaults",
type=bool,
default=False,
help="Add default values to fields if missing",
)
def toml_to_toml(in_path: Path, toml_path: Path, add_defaults: bool, debug: bool):
"""Convert pyaptly config files from toml to toml."""
from . import config_file

config_file.toml_to_toml(
in_path,
toml_path,
add_defaults=add_defaults,
)
14 changes: 14 additions & 0 deletions pyaptly/config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pathlib import Path

import tomli
import yaml

from pyaptly import tomli_w
Expand All @@ -22,6 +23,19 @@ def yaml_to_toml(yaml_path: Path, toml_path: Path, *, add_defaults: bool = False
tomli_w.dump(config, tf)


def toml_to_toml(in_path: Path, toml_path: Path, *, add_defaults: bool = False):
"""Convert pyaptly config files from toml to toml.
Setting `add_defaults=True` will set common default during conversion.
"""
with in_path.open("rb") as nf:
with toml_path.open("wb") as tf:
config = tomli.load(nf)
if add_defaults:
add_default_to_config(config)
tomli_w.dump(config, tf)


def add_default_to_config(config):
"""Set common default in config if the fields are missing."""
if "mirror" in config:
Expand Down
51 changes: 42 additions & 9 deletions pyaptly/tests/debug.toml
Original file line number Diff line number Diff line change
@@ -1,31 +1,64 @@
[mirror.trusty]
components = [ "main", "multiverse", "restricted", "universe",]
architectures = [ "amd64", "i386",]
components = [
"main",
"multiverse",
"restricted",
"universe",
]
architectures = [
"amd64",
"i386",
]
distribution = "trusty"
sources = true
udeb = true
archive = "http://ch.archive.ubuntu.com/ubuntu/"
gpg-keys = [ "40976EAF437D05B5", "3B4FE6ACC0B21F32" ]
gpg-keys = [
"40976EAF437D05B5",
"3B4FE6ACC0B21F32",
]
keyserver = "keyserver.ubuntu.com"

[mirror.trusty-updates]
components = [ "main", "multiverse", "restricted", "universe",]
architectures = [ "amd64", "i386",]
components = [
"main",
"multiverse",
"restricted",
"universe",
]
architectures = [
"amd64",
"i386",
]
distribution = "trusty-updates"
sources = true
udeb = true
archive = "http://ch.archive.ubuntu.com/ubuntu/"
gpg-keys = [ "40976EAF437D05B5", "3B4FE6ACC0B21F32" ]
gpg-keys = [
"40976EAF437D05B5",
"3B4FE6ACC0B21F32",
]
keyserver = "keyserver.ubuntu.com"

[mirror.trusty-backports]
components = [ "main", "multiverse", "restricted", "universe",]
architectures = [ "amd64", "i386",]
components = [
"main",
"multiverse",
"restricted",
"universe",
]
architectures = [
"amd64",
"i386",
]
distribution = "trusty-backports"
sources = true
udeb = true
archive = "http://ch.archive.ubuntu.com/ubuntu/"
gpg-keys = [ "40976EAF437D05B5", "3B4FE6ACC0B21F32" ]
gpg-keys = [
"40976EAF437D05B5",
"3B4FE6ACC0B21F32",
]
keyserver = "keyserver.ubuntu.com"

[snapshot.trusty-latest]
Expand Down
8 changes: 6 additions & 2 deletions pyaptly/tests/mirror-basic.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
[mirror.fakerepo01]
max-tries = 2
archive = "http://localhost:3123/fakerepo01"
gpg-keys = [ "2841988729C7F3FF",]
gpg-keys = [
"2841988729C7F3FF",
]
components = "main"
distribution = "main"

[mirror.fakerepo02]
archive = "http://localhost:3123/fakerepo02"
gpg-keys = [ "2841988729C7F3FF",]
gpg-keys = [
"2841988729C7F3FF",
]
components = "main"
distribution = "main"
8 changes: 6 additions & 2 deletions pyaptly/tests/mirror-extra.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[mirror.fakerepo03]
archive = "http://localhost:3123/fakerepo03"
gpg-keys = [ "EC54D33E5B5EBE98",]
gpg-urls = [ "http://localhost:3123/keys/test02.key",]
gpg-keys = [
"EC54D33E5B5EBE98",
]
gpg-urls = [
"http://localhost:3123/keys/test02.key",
]
components = "main"
distribution = "main"
75 changes: 49 additions & 26 deletions pyaptly/tests/publish-current.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
[[publish."fakerepo01/current"]]
distribution = "stable"
architectures = [
"amd64",
]
components = [
"main",
]
snapshots = [
"fakerepo01-current",
]
automatic-update = true

[[publish."fakerepo02/current"]]
distribution = "stable"
architectures = [
"amd64",
]
components = [
"main",
]
snapshots = [
"fakerepo02-current",
]
automatic-update = true

[[publish."fake/current"]]
distribution = "stable"
architectures = [
"amd64",
]
components = [
"main",
]
snapshots = [
"fake-current",
]
automatic-update = true

[mirror.fakerepo01]
max-tries = 2
archive = "http://localhost:3123/fakerepo01"
gpg-keys = [ "2841988729C7F3FF",]
gpg-keys = [
"2841988729C7F3FF",
]
components = "main"
distribution = "main"

[mirror.fakerepo02]
archive = "http://localhost:3123/fakerepo02"
gpg-keys = [ "2841988729C7F3FF",]
gpg-keys = [
"2841988729C7F3FF",
]
components = "main"
distribution = "main"

Expand All @@ -18,27 +61,7 @@ mirror = "fakerepo01"
mirror = "fakerepo02"

[snapshot.fake-current]
merge = [ "fakerepo01-current", "fakerepo02-current",]

[publish]
[[publish."fakerepo01/current"]]
distribution = "stable"
architectures = [ "amd64",]
components = [ "main",]
snapshots = [ "fakerepo01-current",]
automatic-update = true

[[publish."fakerepo02/current"]]
distribution = "stable"
architectures = [ "amd64",]
components = [ "main",]
snapshots = [ "fakerepo02-current",]
automatic-update = true

[[publish."fake/current"]]
distribution = "stable"
architectures = [ "amd64",]
components = [ "main",]
snapshots = [ "fake-current",]
automatic-update = true

merge = [
"fakerepo01-current",
"fakerepo02-current",
]
21 changes: 9 additions & 12 deletions pyaptly/tests/publish-previous.toml
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
[mirror.fakerepo01]
max-tries = 2
archive = "http://localhost:3123/fakerepo01"
gpg-keys = [ "2841988729C7F3FF",]
gpg-keys = [
"2841988729C7F3FF",
]
components = "main"
distribution = "main"

[mirror.fakerepo02]
archive = "http://localhost:3123/fakerepo02"
gpg-keys = [ "2841988729C7F3FF",]
gpg-keys = [
"2841988729C7F3FF",
]
components = "main"
distribution = "main"

[snapshot."fakerepo01-%T"]
mirror = "fakerepo01"
timestamp = { time = "00:00" }

[snapshot."fakerepo02-%T"]
mirror = "fakerepo02"

[snapshot."fakerepo01-%T".timestamp]
time = "00:00"

[snapshot."fakerepo02-%T".timestamp]
time = "00:00"
repeat-weekly = "sat"
timestamp = { time = "00:00", repeat-weekly = "sat" }

[snapshot."superfake-%T"]
merge = [
{ name = "fakerepo01-%T", timestamp = "previous" },
{ name = "fakerepo02-%T", timestamp = 0 },
]

[snapshot."superfake-%T".timestamp]
time = "00:00"
timestamp = { time = "00:00" }
Loading

0 comments on commit b061daf

Please sign in to comment.