From d0b642082658395bc537a72483316b56d6292bd5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20Lubas?= <lubasowo0@hotmail.com>
Date: Sat, 16 Dec 2023 16:01:24 +0100
Subject: [PATCH] Replace removed imp module with importlib

(cherry picked from commit 27b0d4a2d9cd21fa9f11a0770d94c578db6324d1)
---
 tools/toolbox.py | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/tools/toolbox.py b/tools/toolbox.py
index 6f80c68..0401989 100644
--- a/tools/toolbox.py
+++ b/tools/toolbox.py
@@ -21,7 +21,8 @@
 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 # DEALINGS IN THE SOFTWARE.
 
-import imp
+import importlib.util
+import importlib.machinery
 import os
 import subprocess
 import sys
@@ -45,8 +46,11 @@ def import_non_standard_path(name, path):
     # If any of the following calls raises an exception,
     # there's a problem we can't handle -- let the caller handle it.
 
-    with open(path, 'rb') as fp:
-        module = imp.load_module(name, fp, os.path.basename(path), ('.py', 'rb', imp.PY_SOURCE))
+    loader = importlib.machinery.SourceFileLoader(name, path)
+    spec = importlib.util.spec_from_file_location(name, path, loader=loader)
+    module = importlib.util.module_from_spec(spec)
+    sys.modules[name] = module
+    loader.exec_module(module)
 
     return module
 
@@ -66,7 +70,8 @@ def start_ratbagd(verbosity=0):
     elif verbosity == 0:
         args.append('--quiet')
 
-    ratbagd_process = subprocess.Popen(args, shell=False, stdout=sys.stdout, stderr=sys.stderr)
+    ratbagd_process = subprocess.Popen(
+        args, shell=False, stdout=sys.stdout, stderr=sys.stderr)
 
     dbus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
 
@@ -107,7 +112,7 @@ def sync_dbus():
         main_context.iteration(False)
 
 
-ratbagctl = import_non_standard_path(RATBAGCTL_NAME, RATBAGCTL_PATH)
+import_non_standard_path(RATBAGCTL_NAME, RATBAGCTL_PATH)
 
 from ratbagctl import open_ratbagd, get_parser, RatbagError, RatbagErrorCapability  # NOQA
 
-- 
2.44.0

