Handle several variants of Python 'imp', 'importlib' modules

Message ID 20220105153252.744786-1-thomas@codesourcery.com
State New
Headers
Series Handle several variants of Python 'imp', 'importlib' modules |

Commit Message

Thomas Schwinge Jan. 5, 2022, 3:32 p.m. UTC
  Fix-up for recent commit f0582fdbf1267f0f34bf3c3b6698b60026410146
"Replace use of deprecated Python 'imp' module with 'importlib'", and
commit cc1f38ffedb8d456d43fb52c369409037c5ca4a
"Replace Python 'import importlib' with 'import importlib.machinery'",
because compatibility...

	* tests/mockfedabipkgdiff.in: Handle several variants of Python
	'imp', 'importlib' modules.

Signed-off-by: Thomas Schwinge <thomas@codesourcery.com>
Tested-by: Mark Wielaard <mark@klomp.org> (CentOS 7)
---
 tests/mockfedabipkgdiff.in | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
  

Comments

Dodji Seketeli Jan. 6, 2022, 2:44 p.m. UTC | #1
Thomas Schwinge <thomas@codesourcery.com> a écrit:

> Fix-up for recent commit f0582fdbf1267f0f34bf3c3b6698b60026410146
> "Replace use of deprecated Python 'imp' module with 'importlib'", and
> commit cc1f38ffedb8d456d43fb52c369409037c5ca4a
> "Replace Python 'import importlib' with 'import importlib.machinery'",
> because compatibility...
>
> 	* tests/mockfedabipkgdiff.in: Handle several variants of Python
> 	'imp', 'importlib' modules.
>
> Signed-off-by: Thomas Schwinge <thomas@codesourcery.com>
> Tested-by: Mark Wielaard <mark@klomp.org> (CentOS 7)

Applied to master, thanks!

[...]

Cheers,
  

Patch

diff --git tests/mockfedabipkgdiff.in tests/mockfedabipkgdiff.in
index 29a28ad9..1e562666 100644
--- tests/mockfedabipkgdiff.in
+++ tests/mockfedabipkgdiff.in
@@ -39,7 +39,6 @@  variables.
 
 import os
 import tempfile
-import importlib.machinery
 import six
 
 try:
@@ -69,8 +68,34 @@  def get_download_dir():
     return DOWNLOAD_CACHE_DIR
 
 
+def load_source(name, path):
+    # Different version of Python want this be done differently.
+    try:
+        import importlib.machinery
+        loader = importlib.machinery.SourceFileLoader(name, path)
+        import importlib.util
+        spec = importlib.util.spec_from_loader(name, loader)
+        module = importlib.util.module_from_spec(spec)
+        spec.loader.exec_module(module)
+        import sys.modules
+        sys.modules[name] = module
+        return module
+    except:
+        pass
+    try:
+        import importlib.machinery
+        loader = importlib.machinery.SourceFileLoader(name, path)
+        module = loader.load_module()
+        return module
+    except:
+        pass
+    import imp
+    module = imp.load_source(name, path)
+    return module
+
+
 # Import the fedabipkgdiff program file from the source directory.
-fedabipkgdiff_mod = importlib.machinery.SourceFileLoader('fedabipkgdiff', FEDABIPKGDIFF).load_module()
+fedabipkgdiff_mod = load_source('fedabipkgdiff', FEDABIPKGDIFF)
 
 
 # -----------------  Koji resource storage begins ------------------