Skip to content

Commit

Permalink
feat: support toml, json and yaml
Browse files Browse the repository at this point in the history
yaml has been deprecated, json has beed added to support automation
  • Loading branch information
Jean-Louis Fuchs committed May 21, 2024
1 parent 308674e commit b63fbda
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 15 deletions.
13 changes: 11 additions & 2 deletions pyaptly/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import click

from .util import PyaptlyCliError

lg = logging.getLogger(__name__)


Expand All @@ -19,12 +21,18 @@ def entry_point():
if len_argv > 0 and argv[0].endswith("pyaptly"):
if len_argv > 2 and argv[1] == "legacy" and argv[2] != "--":
argv = argv[:2] + ["--"] + argv[2:]
if "--debug" in argv or "-d" in argv:
debug = True
else:
debug = False

try:
cli.main(argv[1:])
except CalledProcessError:
except (CalledProcessError, PyaptlyCliError):
pass # already logged
except Exception as e:
if debug:
raise
from . import util

path = util.write_traceback()
Expand Down Expand Up @@ -160,6 +168,7 @@ def publish(**kwargs):


@cli.command(help="convert yaml- to toml-comfig")
@click.option("--debug/--no-debug", "-d/-nd", default=False, type=bool)
@click.argument(
"yaml_path",
type=click.Path(
Expand Down Expand Up @@ -187,7 +196,7 @@ def publish(**kwargs):
default=False,
help="Add default values to fields if missing",
)
def yaml_to_toml(yaml_path: Path, toml_path: Path, add_defaults: bool):
def yaml_to_toml(yaml_path: Path, toml_path: Path, add_defaults: bool, debug: bool):
"""Convert pyaptly config files from yaml to toml."""
from . import config_file

Expand Down
31 changes: 20 additions & 11 deletions pyaptly/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@
import argparse
import logging
import sys
from pathlib import Path

import tomli

from . import (
command,
custom_logger,
mirror,
publish,
repo,
snapshot,
)
from . import command, custom_logger, mirror, publish, repo, snapshot, util

_logging_setup = False

Expand Down Expand Up @@ -45,8 +37,25 @@ def prepare(args):
"""Set pretend mode, read config and load state."""
command.Command.pretend_mode = args.pretend

path = Path(args.config)
with open(args.config, "rb") as f:
cfg = tomli.load(f)
if path.suffix == ".toml":
import tomli

cfg = tomli.load(f)
elif path.suffix == ".json":
import json

cfg = json.load(f)
elif path.suffix in (".yaml", ".yml"):
import yaml

cfg = yaml.safe_load(f)
lg.warn(
"NOTE: yaml has beed deprecated and will be remove on the next major release."
)
else:
util.exit_with_error(f"unknown config file extension: {path.suffix}")
return cfg


Expand Down
Empty file added pyaptly/tests/mirror.wuff
Empty file.
78 changes: 78 additions & 0 deletions pyaptly/tests/publish-publish.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"mirror": {
"fakerepo01": {
"max-tries": 2,
"archive": "http://localhost:3123/fakerepo01",
"gpg-keys": [
"2841988729C7F3FF"
],
"components": "main",
"distribution": "main"
},
"fakerepo02": {
"archive": "http://localhost:3123/fakerepo02",
"gpg-keys": [
"2841988729C7F3FF"
],
"components": "main",
"distribution": "main"
}
},
"snapshot": {
"fakerepo01-%T": {
"mirror": "fakerepo01",
"timestamp": {
"time": "00:00"
}
},
"fakerepo02-%T": {
"mirror": "fakerepo02",
"timestamp": {
"time": "00:00",
"repeat-weekly": "sat"
}
}
},
"publish": {
"fakerepo01": [
{
"gpg-key": "6D79A810B9B7ABAE",
"skip-contents": true,
"automatic-update": true,
"components": "main",
"distribution": "main",
"snapshots": [
{
"name": "fakerepo01-%T",
"timestamp": "current",
"archive-on-update": "archived-fakerepo01-%T"
}
]
}
],
"fakerepo02": [
{
"gpg-key": "6D79A810B9B7ABAE",
"automatic-update": true,
"components": "main",
"distribution": "main",
"snapshots": [
{
"name": "fakerepo02-%T",
"timestamp": "current",
"archive-on-update": "archived-fakerepo02-%T"
}
]
}
],
"fakerepo01-stable": [
{
"publish": "fakerepo01 main",
"gpg-key": "6D79A810B9B7ABAE",
"automatic-update": true,
"components": "main",
"distribution": "main"
}
]
}
}
50 changes: 50 additions & 0 deletions pyaptly/tests/publish-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
mirror:
fakerepo01:
archive: http://localhost:3123/fakerepo01
components: main
distribution: main
gpg-keys:
- 2841988729C7F3FF
max-tries: 2
fakerepo02:
archive: http://localhost:3123/fakerepo02
components: main
distribution: main
gpg-keys:
- 2841988729C7F3FF
publish:
fakerepo01:
- automatic-update: true
components: main
distribution: main
gpg-key: 6D79A810B9B7ABAE
skip-contents: true
snapshots:
- archive-on-update: archived-fakerepo01-%T
name: fakerepo01-%T
timestamp: current
fakerepo01-stable:
- automatic-update: true
components: main
distribution: main
gpg-key: 6D79A810B9B7ABAE
publish: fakerepo01 main
fakerepo02:
- automatic-update: true
components: main
distribution: main
gpg-key: 6D79A810B9B7ABAE
snapshots:
- archive-on-update: archived-fakerepo02-%T
name: fakerepo02-%T
timestamp: current
snapshot:
fakerepo01-%T:
mirror: fakerepo01
timestamp:
time: 00:00
fakerepo02-%T:
mirror: fakerepo02
timestamp:
repeat-weekly: sat
time: 00:00
10 changes: 9 additions & 1 deletion pyaptly/tests/test_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from .. import main, state_reader
from .. import main, state_reader, util


@pytest.mark.parametrize("config", ["debug.toml"], indirect=True)
Expand Down Expand Up @@ -46,6 +46,14 @@ def test_mirror_create(environment, config, caplog):
assert state.mirrors() == {"fakerepo03"}


@pytest.mark.parametrize("config", ["mirror.wuff"], indirect=True)
def test_mirror_config_fail(config):
"""Test if checking for unknown config file type works."""
args = ["-c", config, "mirror", "update", "asdfasdf"]
with pytest.raises(util.PyaptlyCliError):
main.main(args)


@pytest.mark.parametrize("config", ["mirror-basic.toml"], indirect=True)
def test_mirror_update(mirror_update):
"""Test if updating mirrors works."""
Expand Down
6 changes: 5 additions & 1 deletion pyaptly/tests/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ def test_publish_create_rotating(config, publish_create_rotating):
pass


@pytest.mark.parametrize("config", ["publish-publish.toml"], indirect=True)
@pytest.mark.parametrize(
"config",
["publish-publish.toml", "publish-publish.json", "publish-publish.yaml"],
indirect=True,
)
def test_publish_create_republish(config, publish_create_republish):
"""Test if creating republishes works."""
pass
Expand Down
9 changes: 9 additions & 0 deletions pyaptly/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
lg = logging.getLogger(__name__)


class PyaptlyCliError(Exception):
pass


def exit_with_error(error):
lg.error(error)
raise PyaptlyCliError()


def write_traceback(): # pragma: no cover
with NamedTemporaryFile("w", delete=False) as tmp:
tmp.write(traceback.format_exc())
Expand Down

0 comments on commit b63fbda

Please sign in to comment.