glasswall.libraries.library

 1import ctypes as ct
 2import os
 3
 4import glasswall
 5from glasswall import utils
 6
 7
 8class Library:
 9    """ A Glasswall library. """
10
11    def __init__(self, library_path: str):
12        self.library_path = library_path
13
14    def load_library(self, library_path: str):
15        if not os.path.isfile(library_path):
16            if os.path.isdir(library_path):
17                library_path = utils.get_library(self.__class__.__name__, library_path)
18            else:
19                raise FileNotFoundError(library_path)
20
21        self.library_path = library_path
22
23        # Preload dependencies to avoid "OSError: ...: cannot open shared object file: No such file or directory"
24        dependencies = [
25            os.path.join(os.path.dirname(self.library_path), dependency)
26            for dependency in glasswall.libraries.os_info[glasswall._OPERATING_SYSTEM][utils.as_snake_case(self.__class__.__name__)]["dependencies"]
27        ]
28        missing_dependencies = utils.load_dependencies(dependencies, ignore_errors=True)
29
30        with utils.CwdHandler(new_cwd=self.library_path):
31            try:
32                # Try to load library
33                return ct.cdll.LoadLibrary(self.library_path)
34            except OSError as e:
35                # If library fails to load and there are missing dependencies, list them
36                if missing_dependencies:
37                    raise FileNotFoundError(f"Unable to load {self.__class__.__name__}. Below dependencies are missing in directory: {os.path.dirname(self.library_path)}\n{', '.join(missing_dependencies)}") from e
38                raise
class Library:
11class Library:
12    """ A Glasswall library. """
13
14    def __init__(self, library_path: str):
15        self.library_path = library_path
16
17    def load_library(self, library_path: str):
18        if not os.path.isfile(library_path):
19            if os.path.isdir(library_path):
20                library_path = utils.get_library(self.__class__.__name__, library_path)
21            else:
22                raise FileNotFoundError(library_path)
23
24        self.library_path = library_path
25
26        # Preload dependencies to avoid "OSError: ...: cannot open shared object file: No such file or directory"
27        dependencies = [
28            os.path.join(os.path.dirname(self.library_path), dependency)
29            for dependency in glasswall.libraries.os_info[glasswall._OPERATING_SYSTEM][utils.as_snake_case(self.__class__.__name__)]["dependencies"]
30        ]
31        missing_dependencies = utils.load_dependencies(dependencies, ignore_errors=True)
32
33        with utils.CwdHandler(new_cwd=self.library_path):
34            try:
35                # Try to load library
36                return ct.cdll.LoadLibrary(self.library_path)
37            except OSError as e:
38                # If library fails to load and there are missing dependencies, list them
39                if missing_dependencies:
40                    raise FileNotFoundError(f"Unable to load {self.__class__.__name__}. Below dependencies are missing in directory: {os.path.dirname(self.library_path)}\n{', '.join(missing_dependencies)}") from e
41                raise

A Glasswall library.

Library(library_path: str)
14    def __init__(self, library_path: str):
15        self.library_path = library_path
library_path
def load_library(self, library_path: str):
17    def load_library(self, library_path: str):
18        if not os.path.isfile(library_path):
19            if os.path.isdir(library_path):
20                library_path = utils.get_library(self.__class__.__name__, library_path)
21            else:
22                raise FileNotFoundError(library_path)
23
24        self.library_path = library_path
25
26        # Preload dependencies to avoid "OSError: ...: cannot open shared object file: No such file or directory"
27        dependencies = [
28            os.path.join(os.path.dirname(self.library_path), dependency)
29            for dependency in glasswall.libraries.os_info[glasswall._OPERATING_SYSTEM][utils.as_snake_case(self.__class__.__name__)]["dependencies"]
30        ]
31        missing_dependencies = utils.load_dependencies(dependencies, ignore_errors=True)
32
33        with utils.CwdHandler(new_cwd=self.library_path):
34            try:
35                # Try to load library
36                return ct.cdll.LoadLibrary(self.library_path)
37            except OSError as e:
38                # If library fails to load and there are missing dependencies, list them
39                if missing_dependencies:
40                    raise FileNotFoundError(f"Unable to load {self.__class__.__name__}. Below dependencies are missing in directory: {os.path.dirname(self.library_path)}\n{', '.join(missing_dependencies)}") from e
41                raise