diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2022-12-08 09:59:53 +0100 |
---|---|---|
committer | Erik Skultety <eskultet@redhat.com> | 2023-01-17 07:30:56 +0000 |
commit | 40589eed1c56f040d0f07fc354c242a0e0d83185 (patch) | |
tree | ca078e03f1b7e1a95b44a7f1f4a2454f24deaab6 | |
parent | eaa8593891ade38392cdbfc87f788962c4d74cba (diff) | |
download | libvirt-ci-40589eed1c56f040d0f07fc354c242a0e0d83185.zip libvirt-ci-40589eed1c56f040d0f07fc354c242a0e0d83185.tar.gz libvirt-ci-40589eed1c56f040d0f07fc354c242a0e0d83185.tar.bz2 |
targets: use DataDir to load target facts
This simplifies the code, removing the usage of pkg_resources.
There is no command line interface change yet; the data directory
is *not* used to allow local mappings.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r-- | lcitool/targets.py | 27 |
1 files changed, 9 insertions, 18 deletions
diff --git a/lcitool/targets.py b/lcitool/targets.py index e796bb1..1bbd253 100644 --- a/lcitool/targets.py +++ b/lcitool/targets.py @@ -5,10 +5,6 @@ # SPDX-License-Identifier: GPL-2.0-or-later import logging -import yaml - -from pathlib import Path -from pkg_resources import resource_filename from lcitool import util, LcitoolError @@ -35,16 +31,11 @@ class Targets(): def targets(self): return list(self.target_facts.keys()) - def __init__(self): + def __init__(self, data_dir=util.DataDir()): + self._data_dir = data_dir self._target_facts = None @staticmethod - def _read_facts_from_file(yaml_path): - log.debug(f"Loading facts from '{yaml_path}'") - with open(yaml_path, "r") as infile: - return yaml.safe_load(infile) - - @staticmethod def _validate_target_facts(target_facts, target): fname = target + ".yml" @@ -60,19 +51,19 @@ class Targets(): def _load_target_facts(self): facts = {} - targets_path = Path(resource_filename(__name__, "facts/targets/")) - targets_all_path = Path(targets_path, "all.yml") + all_targets = {item.stem + for item in self._data_dir.list_files("facts/targets", ".yml", + internal=True)} # first load the shared facts from targets/all.yml - shared_facts = self._read_facts_from_file(targets_all_path) + shared_facts = self._data_dir.load_yaml("facts/targets", "all") # then load the rest of the facts - for entry in targets_path.iterdir(): - if not entry.is_file() or entry.suffix != ".yml" or entry.name == "all.yml": + for target in all_targets: + if target == "all": continue - target = entry.stem - facts[target] = self._read_facts_from_file(entry) + facts[target] = self._data_dir.load_yaml("facts/targets", target) self._validate_target_facts(facts[target], target) facts[target]["target"] = target |