From 9fd541bee4069277888ef7ec24d49f8fca5948de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= Date: Tue, 11 Jul 2023 09:58:44 +0200 Subject: [PATCH] Stop using "imp" module Fixes broken build on Fedora Rawhide. The deprecated importlib API has been removed from Python 3.12, see https://github.com/python/cpython/issues/98040. Addressing: Traceback (most recent call last): File "/builddir/build/BUILD/scap-security-guide-0.1.69/build-scripts/build_templated_content.py", line 9, in import ssg.templates File "/builddir/build/BUILD/scap-security-guide-0.1.69/ssg/templates.py", line 5, in import imp ModuleNotFoundError: No module named 'imp' --- ssg/templates.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/ssg/templates.py b/ssg/templates.py index e5954db3339..1a938e3a67a 100644 --- a/ssg/templates.py +++ b/ssg/templates.py @@ -2,7 +2,6 @@ from __future__ import print_function import os -import imp import glob from collections import namedtuple @@ -38,6 +37,24 @@ TEMPLATE_YAML_FILE_NAME = "template.yml" +def load_module(module_name, module_path): + try: + # Python 2.7 + from imp import load_source + return load_source(module_name, module_path) + except ImportError: + # https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly + import importlib + spec = importlib.util.spec_from_file_location(module_name, module_path) + if not spec: + raise ValueError("Error loading '%s' module" % module_path) + module = importlib.util.module_from_spec(spec) + if not spec.loader: + raise ValueError("Error loading '%s' module" % module_path) + spec.loader.exec_module(module) + return module + + class Template: def __init__(self, templates_root_directory, name): self.langs = [] @@ -83,8 +100,8 @@ def preprocess(self, parameters, lang): def _preprocess_with_template_module(self, parameters, lang): if self.preprocessing_file_path is not None: unique_dummy_module_name = "template_" + self.name - preprocess_mod = imp.load_source(unique_dummy_module_name, - self.preprocessing_file_path) + preprocess_mod = load_module( + unique_dummy_module_name, self.preprocessing_file_path) if not hasattr(preprocess_mod, "preprocess"): msg = ( "The '{name}' template's preprocessing file {preprocessing_file} "