From 4305f86673e09ae638127d3815b81fd03c7169d4 Mon Sep 17 00:00:00 2001 From: Eivind Jahren Date: Tue, 6 Aug 2024 07:01:04 +0200 Subject: [PATCH 1/2] Remove legacy install --- python/resdata/__init__.py | 193 +++++++++++-------------------------- 1 file changed, 55 insertions(+), 138 deletions(-) diff --git a/python/resdata/__init__.py b/python/resdata/__init__.py index fc1395a60..5222260f9 100644 --- a/python/resdata/__init__.py +++ b/python/resdata/__init__.py @@ -32,7 +32,6 @@ import ctypes as ct import os.path -import sys import warnings warnings.filterwarnings( @@ -43,145 +42,63 @@ from cwrap import Prototype -RD_LEGACY_INSTALLED = not os.path.isdir( - os.path.join(os.path.dirname(__file__), ".libs") -) -if not RD_LEGACY_INSTALLED: - from .version import version as __version__ - - def get_include(): - return os.path.join(os.path.dirname(__file__), ".include") - - def dlopen_resdata(): - import ctypes - import platform - - path = os.path.join(os.path.dirname(__file__), ".libs") - if platform.system() == "Linux": - path = os.path.join(path, "libresdata.so") - elif platform.system() == "Darwin": - path = os.path.join(path, "libresdata.dylib") - elif platform.system() == "Windows": - path = os.path.join(os.path.dirname(__file__), ".bin", "libresdata.dll") - else: - raise NotImplementedError("Invalid platform") - - return ctypes.CDLL(path, ctypes.RTLD_GLOBAL) - - # Need to keep the function as a global variable so that we don't give C a - # dangling pointer - _abort_handler = None - - @ct.CFUNCTYPE(None, ct.c_char_p, ct.c_int, ct.c_char_p, ct.c_char_p, ct.c_char_p) - def _c_abort_handler(filename, lineno, function, message, backtrace): - global _abort_handler - if not _abort_handler: - return - _abort_handler( - filename.decode(), - lineno, - function.decode(), - message.decode(), - backtrace.decode(), +from .version import version as __version__ + + +def _dlopen_resdata(): + import ctypes + import platform + + path = os.path.join(os.path.dirname(__file__), ".libs") + if platform.system() == "Linux": + path = os.path.join(path, "libresdata.so") + elif platform.system() == "Darwin": + path = os.path.join(path, "libresdata.dylib") + elif platform.system() == "Windows": + path = os.path.join(os.path.dirname(__file__), ".bin", "libresdata.dll") + else: + raise NotImplementedError("Invalid platform") + + return ctypes.CDLL(path, ctypes.RTLD_GLOBAL) + + +# Need to keep the function as a global variable so that we don't give C a +# dangling pointer +_abort_handler = None + + +@ct.CFUNCTYPE(None, ct.c_char_p, ct.c_int, ct.c_char_p, ct.c_char_p, ct.c_char_p) +def _c_abort_handler(filename, lineno, function, message, backtrace): + global _abort_handler + if not _abort_handler: + return + _abort_handler( + filename.decode(), + lineno, + function.decode(), + message.decode(), + backtrace.decode(), + ) + + +def set_abort_handler(function): + """ + Set callback function for util_abort, which is called prior to std::abort() + """ + global _abort_handler + _abort_handler = function + + ResdataPrototype.lib.util_set_abort_handler(_c_abort_handler) + + +class ResdataPrototype(Prototype): + lib = _dlopen_resdata() + + def __init__(self, prototype, bind=True): + super(ResdataPrototype, self).__init__( + ResdataPrototype.lib, prototype, bind=bind ) - def set_abort_handler(function): - """ - Set callback function for util_abort, which is called prior to std::abort() - """ - global _abort_handler - _abort_handler = function - - ResdataPrototype.lib.util_set_abort_handler(_c_abort_handler) - - class ResdataPrototype(Prototype): - lib = dlopen_resdata() - - def __init__(self, prototype, bind=True): - super(ResdataPrototype, self).__init__( - ResdataPrototype.lib, prototype, bind=bind - ) - -else: - # - # If installed via CMake directly (legacy) - # - from cwrap import load as cwrapload - - try: - import ert_site_init - except ImportError: - pass - - required_version_hex = 0x02070000 - - resdata_path = None - resdata_so_version = "" - __version__ = "0.0.0" - - # 1. Try to load the __resdata_info module; this module has been - # configured by cmake during the build configuration process. The - # module should contain the variable lib_path pointing to the - # directory with shared object files. - try: - from .__resdata_info import ResdataInfo - - resdata_path = ResdataInfo.lib_path - resdata_so_version = ResdataInfo.so_version - __version__ = ResdataInfo.__version__ - except ImportError: - pass - except AttributeError: - pass - - # 2. Using the environment variable RESDATA_LIBRARY_PATH it is possible to - # override the default algorithms. If the RESDATA_LIBRARY_PATH is set - # to a non existing directory a warning will go to stderr and the - # setting will be ignored. - env_lib_path = os.getenv("RESDATA_LIBRARY_PATH") - if env_lib_path: - if os.path.isdir(env_lib_path): - ert_lib_path = os.getenv("RESDATA_LIBRARY_PATH") - else: - sys.stderr.write( - "Warning: Environment variable RESDATA_LIBRARY_PATH points to nonexisting directory:%s - ignored" - % env_lib_path - ) - - # Check that the final ert_lib_path setting corresponds to an existing - # directory. - if resdata_path: - if not os.path.isabs(resdata_path): - resdata_path = os.path.abspath( - os.path.join(os.path.dirname(__file__), resdata_path) - ) - - if not os.path.isdir(resdata_path): - resdata_path = None - - if sys.hexversion < required_version_hex: - raise Exception("ERT Python requires Python 2.7.") - - def load(name): - try: - return cwrapload(name, path=resdata_path, so_version=resdata_so_version) - except ImportError: - # For pip installs, setup.py puts the shared lib in this directory - own_dir = os.path.dirname(os.path.abspath(__file__)) - return cwrapload(name, path=own_dir, so_version=resdata_so_version) - - class ResdataPrototype(Prototype): - lib = load("resdata") - - def __init__(self, prototype, bind=True): - super(ResdataPrototype, self).__init__( - ResdataPrototype.lib, prototype, bind=bind - ) - - -# -# Common -# from .rd_type import ResDataType, ResdataTypeEnum from .rd_util import ( From 72dfe355402375d90d9ef4365992aa90f76d0647 Mon Sep 17 00:00:00 2001 From: Eivind Jahren Date: Tue, 6 Aug 2024 07:13:07 +0200 Subject: [PATCH 2/2] Remove CThreadpool --- python/resdata/util/util/cthread_pool.py | 74 ------------------------ 1 file changed, 74 deletions(-) delete mode 100644 python/resdata/util/util/cthread_pool.py diff --git a/python/resdata/util/util/cthread_pool.py b/python/resdata/util/util/cthread_pool.py deleted file mode 100644 index 50e1b288b..000000000 --- a/python/resdata/util/util/cthread_pool.py +++ /dev/null @@ -1,74 +0,0 @@ -import ctypes - -from cwrap import BaseCClass -from resdata import ResdataPrototype - -import weakref - - -class CThreadPool(BaseCClass): - TYPE_NAME = "rd_thread_pool" - - _alloc = ResdataPrototype("void* thread_pool_alloc(int, bool)", bind=False) - _free = ResdataPrototype("void thread_pool_free(rd_thread_pool)") - _add_job = ResdataPrototype( - "void thread_pool_add_job(rd_thread_pool, void*, void*)" - ) - _join = ResdataPrototype("void thread_pool_join(rd_thread_pool)") - - def __init__(self, pool_size, start=True): - c_ptr = self._alloc(pool_size, start) - super(CThreadPool, self).__init__(c_ptr) - self.arg_list = [] - - def addTaskFunction(self, name, lib, c_function_name): - function = CThreadPool.lookupCFunction(lib, c_function_name) - self_ref = weakref.ref(self) # avoid circular dependencies - - def wrappedFunction(arg): - return self_ref().addTask(function, arg) - - setattr(self, name, wrappedFunction) - - def addTask(self, cfunc, arg): - """ - The function should come from CThreadPool.lookupCFunction(). - """ - if isinstance(arg, BaseCClass): - arg_ptr = BaseCClass.from_param(arg) - else: - arg_ptr = arg - - self.arg_list.append(arg) - self._add_job(cfunc, arg_ptr) - - def join(self): - self._join() - - def free(self): - self.join() - self._free() - - @staticmethod - def lookupCFunction(lib, name): - if isinstance(lib, ctypes.CDLL): - func = getattr(lib, name) - return func - else: - raise TypeError("The lib argument must be of type ctypes.CDLL") - - -class CThreadPoolContextManager(object): - def __init__(self, tp): - self.__tp = tp - - def __enter__(self): - return self.__tp - - def __exit__(self, exc_type, exc_val, exc_tb): - self.__tp.join() - return False - - -def startCThreadPool(size): - return CThreadPoolContextManager(CThreadPool(size, start=True))