From e5e76bba6e2dcd2168095dffd7ea4e2c81458f29 Mon Sep 17 00:00:00 2001 From: Aakarsh2007 <183278592+Aakarsh2007@users.noreply.github.com> Date: Tue, 1 Dec 2026 23:45:59 +0530 Subject: [PATCH] fix: stream remote files when computing deployment metadata --- CHANGELOG.md | 3 ++ databusclient/api/deploy.py | 16 +++--- pyproject.toml | 2 +- tests/test_deploy_streaming_metadata.py | 70 +++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 tests/test_deploy_streaming_metadata.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 80baae66..26225df0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ All notable changes to this project will be documented in this file. ### Changed - File downloads now identify as `databus-python-client/ (https://github.com/dbpedia/databus-python-client)` instead of the Requests library default. +### Fixed +- Deployments that infer a remote file's checksum and size now stream it in chunks instead of buffering the full response. + ## [0.15] - 2025-12-31 ### Added diff --git a/databusclient/api/deploy.py b/databusclient/api/deploy.py index 2c8cd081..5d75521e 100644 --- a/databusclient/api/deploy.py +++ b/databusclient/api/deploy.py @@ -183,13 +183,15 @@ def _load_file_stats(url: str) -> Tuple[str, int]: information in the CLI or metadata file. """ - resp = requests.get(url, timeout=30) - if resp.status_code >= 400: - raise requests.exceptions.RequestException(response=resp) - - sha256sum = hashlib.sha256(bytes(resp.content)).hexdigest() - content_length = len(resp.content) - return sha256sum, content_length + checksum = hashlib.sha256() + content_length = 0 + with requests.get(url, timeout=30, stream=True) as response: + if response.status_code >= 400: + raise requests.exceptions.RequestException(response=response) + for chunk in response.iter_content(chunk_size=64 * 1024): + checksum.update(chunk) + content_length += len(chunk) + return checksum.hexdigest(), content_length def get_file_info(distribution_str: str) -> Tuple[Dict[str, str], str, str, str, int]: diff --git a/pyproject.toml b/pyproject.toml index d44c5483..c9d07c08 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "databusclient" -version = "1.3.2" +version = "1.3.3" description = "A simple client for submitting, downloading, and deleting data on the DBpedia Databus" authors = ["DBpedia Association"] license = "Apache-2.0 License" diff --git a/tests/test_deploy_streaming_metadata.py b/tests/test_deploy_streaming_metadata.py new file mode 100644 index 00000000..43249f1e --- /dev/null +++ b/tests/test_deploy_streaming_metadata.py @@ -0,0 +1,70 @@ +"""Offline coverage for metadata inferred from remote deployment files.""" + +from hashlib import sha256 +from unittest.mock import MagicMock, PropertyMock + +import pytest +import requests + +from databusclient.api.deploy import get_file_info + + +URL = "https://example.org/file.ttl.gz" + + +@pytest.fixture +def remote_file(monkeypatch): + response = MagicMock(spec=requests.Response) + type(response).content = PropertyMock( + side_effect=AssertionError("deployment metadata must not buffer the response") + ) + response.status_code = 200 + response.__enter__.return_value = response + get = MagicMock(return_value=response) + monkeypatch.setattr(requests, "get", get) + return get, response + + +@pytest.mark.parametrize("chunks", [[], [b"first chunk", b"second chunk"]]) +def test_inferred_file_metadata_uses_streamed_bytes(remote_file, chunks): + get, response = remote_file + response.iter_content.return_value = iter(chunks) + payload = b"".join(chunks) + + assert get_file_info(URL) == ( + {}, + "ttl", + "gz", + sha256(payload).hexdigest(), + len(payload), + ) + + get.assert_called_once_with(URL, stream=True, timeout=30) + response.__exit__.assert_called_once_with(None, None, None) + + +def test_failed_metadata_request_closes_response_without_reading_body(remote_file): + _, response = remote_file + response.status_code = 404 + + with pytest.raises(requests.exceptions.RequestException) as error: + get_file_info(URL) + + assert error.value.response is response + response.iter_content.assert_not_called() + response.__exit__.assert_called_once() + + +def test_interrupted_metadata_stream_closes_response(remote_file): + _, response = remote_file + + def interrupted_chunks(*args, **kwargs): + yield b"partial body" + raise requests.exceptions.ChunkedEncodingError("connection interrupted") + + response.iter_content.side_effect = interrupted_chunks + + with pytest.raises(requests.exceptions.ChunkedEncodingError, match="interrupted"): + get_file_info(URL) + + response.__exit__.assert_called_once()