2121import shutil
2222import importlib
2323
24+
2425def _is_module_from_splunk_core (lib_module : ModuleType ) -> bool :
25- """
26- Check if the imported module is from the Splunk-provided libraries.
27- """
26+ """Check if the imported module is from the Splunk-provided libraries."""
2827 core_site_packages_regex = _get_core_site_packages_regex ()
2928
3029 splunk_site_packages_paths = [
@@ -42,16 +41,13 @@ def _is_module_from_splunk_core(lib_module: ModuleType) -> bool:
4241def _is_core_site_package_path (
4342 core_site_packages_directory : str , module_name : str , module_path : str
4443) -> bool :
45- """
46- Check if the module path originates from a core site-packages directory.
47- """
44+ """Check if the module path originates from a core site-packages
45+ directory."""
4846 return os .path .join (core_site_packages_directory , module_name ) in module_path
4947
5048
5149def _get_core_site_packages_regex () -> re .Pattern :
52- """
53- Get the regex pattern for matching site-packages directories.
54- """
50+ """Get the regex pattern for matching site-packages directories."""
5551 sep = os .path .sep
5652 sep_escaped = re .escape (sep )
5753
@@ -78,16 +74,19 @@ def _get_core_site_packages_regex() -> re.Pattern:
7874
7975
8076def _cache_lib (lib_name : str ):
81- """
82- Import the Splunk-shipped library first, before adding TA paths to sys.path, to ensure it is cached.
83- This way, even if the TA path added to sys.path contains the specified library,
84- Python will always reference the already cached library from the Splunk Python path.
77+ """Import the Splunk-shipped library first, before adding TA paths to
78+ sys.path, to ensure it is cached.
79+
80+ This way, even if the TA path added to sys.path contains the
81+ specified library, Python will always reference the already cached
82+ library from the Splunk Python path.
8583 """
8684 lib_module = importlib .import_module (lib_name )
8785 assert _is_module_from_splunk_core (
8886 lib_module
8987 ), f"The module { lib_name } is not from Splunk core site-packages."
9088
89+
9190def _get_app_path (absolute_path : str , current_script_folder : str = "lib" ) -> str :
9291 """Returns app path."""
9392 marker = os .path .join (os .path .sep , "etc" , "apps" )
@@ -101,27 +100,32 @@ def _get_app_path(absolute_path: str, current_script_folder: str = "lib") -> str
101100 path = absolute_path [:end ]
102101 return path
103102
103+
104104def _remove_lib_folder (lib_name : str ):
105- """
106- List and attempt to remove any folders directly under the 'lib' directory that contain lib_name in their name.
107- Handles exceptions during removal, allowing the script to proceed even if errors occur.
105+ """List and attempt to remove any folders directly under the 'lib'
106+ directory that contain lib_name in their name.
107+
108+ Handles exceptions during removal, allowing the script to proceed
109+ even if errors occur.
108110 """
109111
110- try :
111- app_dir = _get_app_path (os .path .abspath (__file__ ))
112- lib_dir = os .path .join (app_dir , "lib" )
112+ app_dir = _get_app_path (os .path .abspath (__file__ ))
113+ if app_dir is None :
114+ print (f"WARNING: Unable to determine app directory path for { lib_name } " )
115+ return
113116
117+ lib_dir = os .path .join (app_dir , "lib" )
118+
119+ try :
114120 for entry in os .listdir (lib_dir ):
115121 entry_path = os .path .join (lib_dir , entry )
116122 if os .path .isdir (entry_path ) and lib_name in entry :
117123 try :
118124 shutil .rmtree (entry_path )
119- except Exception :
120- # Bypassing exceptions to ensure uninterrupted execution
121- pass
122- except Exception :
123- # Bypassing exceptions to ensure uninterrupted execution
124- pass
125+ except Exception as e :
126+ print (f"ERROR: Failed to remove library folder { entry_path } : { e } " )
127+ except Exception as e :
128+ print (f"ERROR: Error in _remove_lib_folder for { lib_name } : { e } " )
125129
126130
127131def handle_splunk_provided_lib (lib_name : str ):
0 commit comments