diff --git a/lib/galaxy/util/__init__.py b/lib/galaxy/util/__init__.py index 429013e71c8..7a4a4b6da69 100644 --- a/lib/galaxy/util/__init__.py +++ b/lib/galaxy/util/__init__.py @@ -786,20 +786,6 @@ def in_directory(file, directory, local_path_module=os.path): local_path_module is used by Pulsar to check Windows paths while running on a POSIX-like system. - - >>> base_dir = tempfile.mkdtemp() - >>> safe_dir = os.path.join(base_dir, "user") - >>> os.mkdir(safe_dir) - >>> good_file = os.path.join(safe_dir, "1") - >>> with open(good_file, "w") as f: _ = f.write("hello") - >>> in_directory(good_file, safe_dir) - True - >>> in_directory("/other/file/is/here.txt", safe_dir) - False - >>> unsafe_link = os.path.join(safe_dir, "2") - >>> os.symlink("/other/file/bad.fasta", unsafe_link) - >>> in_directory(unsafe_link, safe_dir) - False """ if local_path_module != os.path: _safe_contains = importlib.import_module(f"galaxy.util.path.{local_path_module.__name__}").safe_contains diff --git a/lib/galaxy/util/path/__init__.py b/lib/galaxy/util/path/__init__.py index a220871c92b..8842807931b 100644 --- a/lib/galaxy/util/path/__init__.py +++ b/lib/galaxy/util/path/__init__.py @@ -2,9 +2,10 @@ """ import errno -import imp +import importlib import logging import shlex +import types from functools import partial from itertools import starmap from operator import getitem @@ -424,7 +425,7 @@ def __splitext_ignore(path, ignore=None): # cross-platform support -def _build_self(target, path_module): +def _build_self(target: types.ModuleType, path_module: types.ModuleType) -> None: """Populate a module with the same exported functions as this module, but using the given os.path module. :type target: module @@ -432,20 +433,8 @@ def _build_self(target, path_module): :type path_module: ``ntpath`` or ``posixpath`` module :param path_module: module implementing ``os.path`` API to use for path functions """ - __copy_self().__set_fxns_on(target, path_module) - - -def __copy_self(names=__name__, parent=None): - """Returns a copy of this module that can be modified without modifying `galaxy.util.path`` in ``sys.modules``.""" - if isinstance(names, str): - names = iter(names.split(".")) - try: - name = next(names) - except StopIteration: - return parent - path = parent and parent.__path__ - parent = imp.load_module(name, *imp.find_module(name, path)) - return __copy_self(names, parent) + self_copy = importlib.import_module(__name__) + self_copy.__set_fxns_on(target, path_module) def __set_fxns_on(target, path_module): diff --git a/lib/galaxy/webapps/galaxy/api/webhooks.py b/lib/galaxy/webapps/galaxy/api/webhooks.py index 033f65cc00f..79ea78a0660 100644 --- a/lib/galaxy/webapps/galaxy/api/webhooks.py +++ b/lib/galaxy/webapps/galaxy/api/webhooks.py @@ -1,7 +1,7 @@ """ API Controller providing Galaxy Webhooks """ -import imp +import importlib.util import logging from typing import Any @@ -37,7 +37,12 @@ class WebhooksController(BaseGalaxyAPIController): webhook = next(webhook for webhook in self.app.webhooks_registry.webhooks if webhook.id == webhook_id) if webhook and webhook.helper != "": - return imp.load_source(webhook.path, webhook.helper).main( # type: ignore[attr-defined] + spec = importlib.util.spec_from_file_location(webhook.path, webhook.helper) + assert spec + module = importlib.util.module_from_spec(spec) + assert spec.loader + spec.loader.exec_module(module) + return module.main( trans, webhook, params, diff --git a/test/unit/util/test_paths.py b/test/unit/util/test_paths.py new file mode 100644 index 00000000000..acd6334a85c --- /dev/null +++ b/test/unit/util/test_paths.py @@ -0,0 +1,28 @@ +import ntpath +import os +import posixpath +import tempfile + +from galaxy.util import in_directory + + +def test_in_directory(): + base_dir = tempfile.mkdtemp() + safe_dir = os.path.join(base_dir, "user") + os.mkdir(safe_dir) + good_file = os.path.join(safe_dir, "1") + with open(good_file, "w") as f: + f.write("hello") + assert in_directory(good_file, safe_dir) + + assert not in_directory("/other/file/is/here.txt", safe_dir) + + unsafe_link = os.path.join(safe_dir, "2") + os.symlink("/other/file/bad.fasta", unsafe_link) + assert not in_directory(unsafe_link, safe_dir) + + # Test local_path_module parameter + if os.path == posixpath: + assert in_directory(good_file, safe_dir, local_path_module=ntpath) + elif os.path == ntpath: + assert in_directory(good_file, safe_dir, local_path_module=posixpath)