From b7ae3a15778b9cceb318453417a357b0806081cf Mon Sep 17 00:00:00 2001 From: Nodar Gogoberidze Date: Fri, 6 Nov 2026 11:40:26 -0500 Subject: [PATCH] [windows] Fix batchfile path replacement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Decode URLs using the source platform’s rules and normaliz separators before matching - Resolves #4977 --- .../measurement/_measurements.py | 34 ++-- tests/measurement/test_batch_path_mappings.py | 160 ++++++++++++++++++ 2 files changed, 182 insertions(+), 12 deletions(-) create mode 100644 tests/measurement/test_batch_path_mappings.py diff --git a/cellprofiler_core/measurement/_measurements.py b/cellprofiler_core/measurement/_measurements.py index 49fc22f3..6f7ae1e4 100644 --- a/cellprofiler_core/measurement/_measurements.py +++ b/cellprofiler_core/measurement/_measurements.py @@ -1,10 +1,11 @@ import json import logging +import nturl2path import os import re import sys +import urllib.parse import urllib.request -from urllib.request import url2pathname as os_url2pathname import warnings import h5py @@ -1458,18 +1459,27 @@ def alter_url_post_create_batch(self, url): if not self.has_feature(EXPERIMENT, M_PATH_MAPPINGS,): return url d = json.loads(self.get_experiment_measurement(M_PATH_MAPPINGS)) - full_name = os_url2pathname(url[5:]) - full_name_c = full_name if d[K_CASE_SENSITIVE] else full_name.lower() - if d[K_LOCAL_SEPARATOR] != os.path.sep: - full_name = full_name.replace(d[K_LOCAL_SEPARATOR], os.path.sep) + parsed_url = urllib.parse.urlsplit(url) + url_path = parsed_url.path + if parsed_url.netloc and parsed_url.netloc.lower() != "localhost": + url_path = "//" + parsed_url.netloc + url_path + if d[K_URL2PATHNAME_PACKAGE_NAME] == "nturl2path": + full_name = nturl2path.url2pathname(url_path) + else: + full_name = urllib.parse.unquote(url_path) + full_name = full_name.replace(d[K_LOCAL_SEPARATOR], "/") + mapped = False for local_directory, remote_directory in d[K_PATH_MAPPINGS]: - if d[K_CASE_SENSITIVE]: - if full_name_c.startswith(local_directory): - full_name = remote_directory + full_name[len(local_directory) :] - else: - if full_name_c.startswith(local_directory.lower()): - full_name = remote_directory + full_name[len(local_directory) :] - url = "file:" + urllib.request.pathname2url(full_name) + local_directory = local_directory.replace(d[K_LOCAL_SEPARATOR], "/") + full_name_c = full_name if d[K_CASE_SENSITIVE] else full_name.lower() + local_directory_c = ( + local_directory if d[K_CASE_SENSITIVE] else local_directory.lower() + ) + if full_name_c.startswith(local_directory_c): + full_name = remote_directory + full_name[len(local_directory) :] + mapped = True + if mapped: + return "file:" + urllib.request.pathname2url(full_name) return url ########################################################### diff --git a/tests/measurement/test_batch_path_mappings.py b/tests/measurement/test_batch_path_mappings.py new file mode 100644 index 00000000..05d9f7c6 --- /dev/null +++ b/tests/measurement/test_batch_path_mappings.py @@ -0,0 +1,160 @@ +import json +import nturl2path +import urllib.parse + +import pytest +from cellprofiler_core.constants.measurement import ( + K_CASE_SENSITIVE, + K_LOCAL_SEPARATOR, + K_PATH_MAPPINGS, + K_URL2PATHNAME_PACKAGE_NAME, + M_PATH_MAPPINGS, +) +from cellprofiler_core.measurement import Measurements, _measurements + + +@pytest.fixture +def measurements(): + result = Measurements() + yield result + result.close() + + +@pytest.fixture(params=["posix", "windows"]) +def target(request, monkeypatch): + if request.param == "windows": + encode = nturl2path.pathname2url + decode = nturl2path.url2pathname + root = r"Z:\cluster\images" + expected = r"Z:\cluster\images\subfolder\image é#%.tif" + else: + encode = urllib.parse.quote + decode = urllib.parse.unquote + root = "/home/ubuntu/bucket/projects/project/batch/images" + expected = root + "/subfolder/image é#%.tif" + monkeypatch.setattr(_measurements.urllib.request, "pathname2url", encode) + return root, expected, decode + + +@pytest.mark.parametrize( + "url,local_root,case_sensitive,separator,package", + [ + ( + "file:///D:/Batch_1_CellGrowth/images/subfolder/image%20%C3%A9%23%25.tif", + r"D:\Batch_1_CellGrowth\images", + False, + "\\", + "nturl2path", + ), + ( + "file:/D:/BATCH_1_CELLGROWTH/images/subfolder/image%20%C3%A9%23%25.tif", + "d:/Batch_1_CellGrowth/images", + False, + "\\", + "nturl2path", + ), + ( + "file://server/share/images/subfolder/image%20%C3%A9%23%25.tif", + r"\\server\share\images", + False, + "\\", + "nturl2path", + ), + ( + "file:///C:/Program%20Files/CellProfiler/images/subfolder/image%20%C3%A9%23%25.tif", + r"C:\Program Files\CellProfiler\images", + False, + "\\", + "nturl2path", + ), + ( + "file:///Users/local/images/subfolder/image%20%C3%A9%23%25.tif", + "/Users/local/images", + True, + "/", + "urllib.request", + ), + ( + "file:/Users/local/images/subfolder/image%20%C3%A9%23%25.tif", + "/Users/local/images", + True, + "/", + "urllib.request", + ), + ], + ids=[ + "windows-drive", + "windows-forward-slashes", + "windows-unc", + "windows-space-in-root", + "posix-uri", + "posix-short-uri", + ], +) +@pytest.mark.parametrize("trailing_separator", [False, True]) +def test_alter_url_post_create_batch( + measurements, + target, + url, + local_root, + case_sensitive, + separator, + package, + trailing_separator, +): + remote_root, expected, decode = target + if trailing_separator: + local_root += separator + remote_root += "/" + measurements.add_experiment_measurement( + M_PATH_MAPPINGS, + json.dumps( + { + K_CASE_SENSITIVE: case_sensitive, + K_LOCAL_SEPARATOR: separator, + K_URL2PATHNAME_PACKAGE_NAME: package, + K_PATH_MAPPINGS: [("unrelated", "unused"), (local_root, remote_root)], + } + ), + ) + result = measurements.alter_url_post_create_batch(url) + assert result.startswith("file:") + assert decode(result[5:]) == expected + + +@pytest.mark.parametrize( + "url", + ["https://example.org/image.tif", "s3://bucket/image.tif", "file:///D:/image.tif"], +) +def test_alter_url_without_mappings(measurements, url): + assert measurements.alter_url_post_create_batch(url) == url + + +def test_unmatched_url_is_unchanged(measurements): + measurements.write_path_mappings([(r"C:\images", "/cluster/images")]) + url = "file:///D:/images/image.tif" + assert measurements.alter_url_post_create_batch(url) == url + + +@pytest.mark.parametrize( + "url", ["https://example.org/image.tif", "s3://bucket/image.tif"] +) +def test_non_file_url_with_mappings(measurements, url): + measurements.write_path_mappings([("", "/cluster/images")]) + assert measurements.alter_url_post_create_batch(url) == url + + +def test_posix_mapping_is_case_sensitive(measurements): + measurements.add_experiment_measurement( + M_PATH_MAPPINGS, + json.dumps( + { + K_CASE_SENSITIVE: True, + K_LOCAL_SEPARATOR: "/", + K_URL2PATHNAME_PACKAGE_NAME: "urllib.request", + K_PATH_MAPPINGS: [("/Users/local/images", "/cluster/images")], + } + ), + ) + url = "file:///Users/LOCAL/images/image.tif" + assert measurements.alter_url_post_create_batch(url) == url