Skip to content

Commit

Permalink
Merge pull request #114 from adfinis/jlf/feat-support-toml-json-and-yaml
Browse files Browse the repository at this point in the history
feat: support toml, json and yaml
  • Loading branch information
Jean-Louis Fuchs authored May 30, 2024
2 parents 8bba42a + b63fbda commit 581d556
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 14 deletions.
4 changes: 3 additions & 1 deletion 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 @@ -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:
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 581d556

Please sign in to comment.