diff --git a/pyaptly/cli.py b/pyaptly/cli.py index ed48e6e..d61ea9e 100644 --- a/pyaptly/cli.py +++ b/pyaptly/cli.py @@ -7,6 +7,8 @@ import click +from .util import PyaptlyCliError + lg = logging.getLogger(__name__) @@ -26,7 +28,7 @@ def entry_point(): try: cli.main(argv[1:]) - except CalledProcessError: + except (CalledProcessError, PyaptlyCliError): pass # already logged except Exception as e: if debug: diff --git a/pyaptly/main.py b/pyaptly/main.py index 8da90a0..ce3ada6 100755 --- a/pyaptly/main.py +++ b/pyaptly/main.py @@ -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 @@ -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 diff --git a/pyaptly/tests/mirror.wuff b/pyaptly/tests/mirror.wuff new file mode 100644 index 0000000..e69de29 diff --git a/pyaptly/tests/publish-publish.json b/pyaptly/tests/publish-publish.json new file mode 100644 index 0000000..1d1b348 --- /dev/null +++ b/pyaptly/tests/publish-publish.json @@ -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" + } + ] + } +} \ No newline at end of file diff --git a/pyaptly/tests/publish-publish.yaml b/pyaptly/tests/publish-publish.yaml new file mode 100644 index 0000000..2b7c818 --- /dev/null +++ b/pyaptly/tests/publish-publish.yaml @@ -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 diff --git a/pyaptly/tests/test_mirror.py b/pyaptly/tests/test_mirror.py index 12a0571..3be1629 100644 --- a/pyaptly/tests/test_mirror.py +++ b/pyaptly/tests/test_mirror.py @@ -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) @@ -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.""" diff --git a/pyaptly/tests/test_publish.py b/pyaptly/tests/test_publish.py index ad54926..d48c5c5 100644 --- a/pyaptly/tests/test_publish.py +++ b/pyaptly/tests/test_publish.py @@ -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 diff --git a/pyaptly/util.py b/pyaptly/util.py index 344ee4c..05c0945 100644 --- a/pyaptly/util.py +++ b/pyaptly/util.py @@ -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())