aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2022-12-08 09:59:53 +0100
committerErik Skultety <eskultet@redhat.com>2023-01-17 07:30:56 +0000
commiteaa8593891ade38392cdbfc87f788962c4d74cba (patch)
treeb985941cef033193578c1e73ce6269cd13b128e3
parent9cfc9beb3157d6c79bdb8e82074c805f77b775ef (diff)
downloadlibvirt-ci-eaa8593891ade38392cdbfc87f788962c4d74cba.zip
libvirt-ci-eaa8593891ade38392cdbfc87f788962c4d74cba.tar.gz
libvirt-ci-eaa8593891ade38392cdbfc87f788962c4d74cba.tar.bz2
packages: use DataDir to load mappings
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, as the file is always marked as internal. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r--lcitool/packages.py18
-rw-r--r--lcitool/util.py10
2 files changed, 16 insertions, 12 deletions
diff --git a/lcitool/packages.py b/lcitool/packages.py
index 6f3c313..efa7319 100644
--- a/lcitool/packages.py
+++ b/lcitool/packages.py
@@ -39,9 +39,6 @@ Exported functions:
import abc
import logging
-import yaml
-
-from pkg_resources import resource_filename
from lcitool import util, LcitoolError
@@ -203,7 +200,8 @@ class Packages:
"""
- def __init__(self):
+ def __init__(self, data_dir=util.DataDir()):
+ self._data_dir = data_dir
self._mappings = None
self._pypi_mappings = None
self._cpan_mappings = None
@@ -316,15 +314,11 @@ class Packages:
return self._get_cross_package(pkg_mapping, target)
def _load_mappings(self):
- mappings_path = resource_filename(__name__,
- "facts/mappings.yml")
-
try:
- with open(mappings_path, "r") as infile:
- mappings = yaml.safe_load(infile)
- self._mappings = mappings["mappings"]
- self._pypi_mappings = mappings["pypi_mappings"]
- self._cpan_mappings = mappings["cpan_mappings"]
+ mappings = self._data_dir.load_yaml("facts", "mappings")
+ self._mappings = mappings["mappings"]
+ self._pypi_mappings = mappings["pypi_mappings"]
+ self._cpan_mappings = mappings["cpan_mappings"]
except Exception as ex:
log.debug("Can't load mappings")
raise PackageError(f"Can't load mappings: {ex}")
diff --git a/lcitool/util.py b/lcitool/util.py
index 4fb65a1..56a6831 100644
--- a/lcitool/util.py
+++ b/lcitool/util.py
@@ -11,6 +11,7 @@ import os
import platform
import tempfile
import textwrap
+import yaml
from pathlib import Path
from pkg_resources import resource_filename
@@ -252,6 +253,15 @@ class DataDir:
if file.is_file() and (suffix is None or file.suffix == suffix):
yield file
+ def load_yaml(self, resource_path, name):
+ file = Path(resource_filename(__name__, resource_path), name + ".yml")
+ if not file.exists():
+ return {}
+
+ log.debug(f"Loading facts from '{file}'")
+ with open(file, "r") as infile:
+ return yaml.safe_load(infile)
+
def validate_cross_platform(cross_arch, osname):
if osname not in ["Debian", "Fedora"]: