From 393f9528e747236097574b8b570f0ba33dc27906 Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Thu, 5 Sep 2024 15:40:22 +0100 Subject: [PATCH] Drop use of deprecated imghdr standard library module which has been removed in the upcoming Python 3.13 . --- lib/galaxy/util/image_util.py | 5 ----- packages/app/setup.cfg | 2 +- packages/data/setup.cfg | 2 +- packages/test.sh | 2 +- packages/tool_shed/setup.cfg | 2 +- packages/tool_util/setup.cfg | 2 +- packages/util/setup.cfg | 2 ++ test/unit/util/test_checkers.py | 13 ++++++++++++- 8 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/galaxy/util/image_util.py b/lib/galaxy/util/image_util.py index 3b11a50d2fda..b8c26cf0ff54 100644 --- a/lib/galaxy/util/image_util.py +++ b/lib/galaxy/util/image_util.py @@ -1,6 +1,5 @@ """Provides utilities for working with image files.""" -import imghdr import logging from typing import ( List, @@ -22,11 +21,7 @@ def image_type(filename: str) -> Optional[str]: with Image.open(filename) as im: fmt = im.format except Exception: - # We continue to try with imghdr, so this is a rare case of an - # exception we expect to happen frequently, so we're not logging pass - if not fmt: - fmt = imghdr.what(filename) if fmt: return fmt.upper() else: diff --git a/packages/app/setup.cfg b/packages/app/setup.cfg index 1184ee741978..2da169b502a4 100644 --- a/packages/app/setup.cfg +++ b/packages/app/setup.cfg @@ -41,7 +41,7 @@ install_requires = galaxy-objectstore galaxy-tool-util[cwl,edam] galaxy-tours - galaxy-util + galaxy-util[image_util] galaxy-web-framework galaxy-web-stack Beaker diff --git a/packages/data/setup.cfg b/packages/data/setup.cfg index c48c3d1825bc..3e397570a3d6 100644 --- a/packages/data/setup.cfg +++ b/packages/data/setup.cfg @@ -36,7 +36,7 @@ install_requires = galaxy-objectstore galaxy-schema galaxy-tool-util - galaxy-util[template] + galaxy-util[image_util,template] alembic alembic-utils bdbag>=1.6.3 diff --git a/packages/test.sh b/packages/test.sh index d2ed377c352b..2120ac2b922a 100755 --- a/packages/test.sh +++ b/packages/test.sh @@ -49,7 +49,7 @@ while read -r package_dir || [ -n "$package_dir" ]; do # https://stackoverflow. # Install extras (if needed) if [ "$package_dir" = "util" ]; then - pip install '.[template,jstree,config_template]' + pip install '.[image_util,template,jstree,config_template]' elif [ "$package_dir" = "tool_util" ]; then pip install '.[cwl,mulled,edam,extended-assertions]' else diff --git a/packages/tool_shed/setup.cfg b/packages/tool_shed/setup.cfg index 2f167e97693a..2198ec26277f 100644 --- a/packages/tool_shed/setup.cfg +++ b/packages/tool_shed/setup.cfg @@ -36,7 +36,7 @@ install_requires = galaxy-auth galaxy-config galaxy-data - galaxy-util + galaxy-util[image_util] galaxy-web-framework galaxy-web-stack galaxy-web-apps diff --git a/packages/tool_util/setup.cfg b/packages/tool_util/setup.cfg index 6fd52ba67a7f..0c22986928f2 100644 --- a/packages/tool_util/setup.cfg +++ b/packages/tool_util/setup.cfg @@ -33,7 +33,7 @@ version = 24.2.dev0 [options] include_package_data = True install_requires = - galaxy-util>=22.1 + galaxy-util[image_util]>=22.1 conda-package-streaming lxml!=4.2.2 MarkupSafe diff --git a/packages/util/setup.cfg b/packages/util/setup.cfg index 064f331f621c..124251ee564b 100644 --- a/packages/util/setup.cfg +++ b/packages/util/setup.cfg @@ -48,6 +48,8 @@ packages = find: python_requires = >=3.7 [options.extras_require] +image_util = + pillow jstree = dictobj template = diff --git a/test/unit/util/test_checkers.py b/test/unit/util/test_checkers.py index 1e30d25656cd..b474defc965a 100644 --- a/test/unit/util/test_checkers.py +++ b/test/unit/util/test_checkers.py @@ -1,6 +1,10 @@ +import os.path import tempfile -from galaxy.util.checkers import check_html +from galaxy.util.checkers import ( + check_html, + check_image, +) def test_check_html(): @@ -17,3 +21,10 @@ def test_check_html(): tmpb.write(b"\x1f\x8b") tmpb.flush() assert not check_html(tmpb.name) + + +def test_check_image(): + for filename, expected in (("1.tiff", True), ("454Score.png", True), ("1.bam", False)): + path = f"test-data/{filename}" + assert os.path.exists(path) + assert check_image(path) is expected