mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
Replace 2 uses of deprecated `imp` module
Also: - Move doctests for `in_directory()` function to unit tests, add test for `local_path_module` parameter to exercise the posixpath/ntpath import code.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user