replace imp.load_source with a new thing based on importlib

The builtin imp module has been deprecated for a long time
and is gone in Python 3.12.
3 files changed, 26 insertions(+), 12 deletions(-)

M rework/cli.py
M rework/helper.py
M rework/task.py
M rework/cli.py +2 -3
@@ 1,4 1,3 @@ 
-import imp
 from datetime import timedelta
 from time import sleep
 import tzlocal

          
@@ 19,6 18,7 @@ from rework.helper import (
     cleanup_tasks,
     cleanup_workers,
     find_dburi,
+    load_source,
     schedule_plan,
     utcnow,
     setuplogger

          
@@ 60,10 60,9 @@ def register_operations(dburi, module, d
     The registered operations will be relative to the current host.
     """
     # load the module
-    imp.load_source('operations', module)
+    load_source('operations', module)
     engine = create_engine(find_dburi(dburi))
     ok, ko = api.freeze_operations(engine, domain)
-
     print(f'registered {len(ok)} new operation ({len(ko)} already known)')
 
 

          
M rework/helper.py +15 -0
@@ 11,6 11,8 @@ import re
 import json
 import struct
 import tzlocal
+import importlib
+import sys
 
 from icron import croniter_range
 import pyzstd as zstd

          
@@ 37,6 39,19 @@ def setuplogger(level=logging.DEBUG):
     return logger
 
 
+def load_source(modname, filename):
+    loader = importlib.machinery.SourceFileLoader(
+        modname, filename
+    )
+    spec = importlib.util.spec_from_file_location(
+        modname, filename, loader=loader
+    )
+    module = importlib.util.module_from_spec(spec)
+    sys.modules[modname] = module
+    loader.exec_module(module)
+    return module
+
+
 def utcnow():
     return datetime.utcnow().replace(tzinfo=pytz.utc)
 

          
M rework/task.py +9 -9
@@ 1,5 1,4 @@ 
 import sys
-import imp
 import time
 from pickle import dumps, loads
 import traceback as tb

          
@@ 9,6 8,7 @@ import logging
 from sqlhelp import select, update
 
 from rework.helper import (
+    load_source,
     pack_io,
     PGLogHandler,
     PGLogWriter,

          
@@ 313,15 313,15 @@ class Task:
             ).values(
                 started=utcnow()
             ).do(cn)
+            name, path = self.engine.execute(
+                'select name, path '
+                'from rework.operation '
+                'where rework.operation.id = %(operation)s',
+                operation=self.operation
+            ).fetchone()
+        mod = load_source('module', path)
+        func = getattr(mod, name)
         try:
-            name, path = self.engine.execute("""
-                select name, path
-                from rework.operation
-                where rework.operation.id = %(operation)s
-            """, {'operation': self.operation}
-            ).fetchone()
-            mod = imp.load_source('module', path)
-            func = getattr(mod, name)
             func(self)
         except:
             with self.engine.begin() as cn: