Skip to content

Commit abd91e4

Browse files
authored
Merge pull request #329 from MerginMaps/fix/download-file-bare-output-path
Fix single file download to an output path without a directory
2 parents 73440ce + 653a3b0 commit abd91e4

4 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎mergin/client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ def get_file_diff(self, project_dir, file_path, output_diff, version_from, versi
12741274
diffs.append(mp.fpath_cache(file_history["history"][v]["diff"]["path"], v))
12751275

12761276
# concatenate diffs, if needed
1277-
output_dir = os.path.dirname(output_diff)
1277+
output_dir = os.path.abspath(os.path.dirname(output_diff))
12781278
if len(diffs) >= 1:
12791279
os.makedirs(output_dir, exist_ok=True)
12801280
if len(diffs) > 1:

‎mergin/client_pull.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def __init__(self, dest_file, downloaded_items: typing.List[DownloadQueueItem],
182182

183183
def from_chunks(self):
184184
"""Merges downloaded chunks into a single file at dest_file path"""
185-
file_dir = os.path.dirname(self.dest_file)
185+
file_dir = os.path.abspath(os.path.dirname(self.dest_file))
186186
os.makedirs(file_dir, exist_ok=True)
187187

188188
with open(self.dest_file, "wb") as final:
@@ -437,7 +437,7 @@ def apply(self, directory, mp: Union[MerginProject, "DownloadScratchContext"]):
437437
file_dir = os.path.dirname(os.path.normpath(os.path.join(directory, self.file_path)))
438438
dest_file_path = os.path.join(file_dir, basename)
439439
else:
440-
file_dir = os.path.dirname(os.path.normpath(self.destination_file))
440+
file_dir = os.path.dirname(os.path.abspath(self.destination_file))
441441
dest_file_path = self.destination_file
442442
os.makedirs(file_dir, exist_ok=True)
443443

‎mergin/report.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def create_report(mc, directory, since, to, out_file):
284284
raise ClientError("Reporting failed, please check log for details")
285285

286286
# export report to csv file
287-
out_dir = os.path.dirname(out_file)
287+
out_dir = os.path.abspath(os.path.dirname(out_file))
288288
os.makedirs(out_dir, exist_ok=True)
289289
with open(out_file, "w", newline="") as f_csv:
290290
writer = csv.DictWriter(f_csv, fieldnames=headers)

‎mergin/test/test_client_pull.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
from mergin.common import DeltaChangeType, CHUNK_SIZE
55
from mergin.models import ProjectDeltaChange, ProjectDeltaItemDiff
6-
from mergin.client_pull import get_download_diff_files, get_download_items
6+
from mergin.client_pull import DownloadQueueItem, UpdateTask, get_download_diff_files, get_download_items
77

88

99
def test_get_diff_download_files():
@@ -74,3 +74,20 @@ def test_get_download_items():
7474
assert items[0].file_path == "base.gpkg"
7575
assert items[0].size == 50
7676
assert items[0].download_file_path == os.path.join(tmp_dir, "diff_file.0")
77+
78+
79+
@pytest.mark.parametrize("output_file", ["out.txt", "./out.txt"])
80+
def test_update_task_apply_to_bare_filename(output_file, monkeypatch):
81+
"""Single file download must accept an output path without a directory part."""
82+
with tempfile.TemporaryDirectory() as tmp_dir:
83+
chunk_path = os.path.join(tmp_dir, "data.txt.0")
84+
with open(chunk_path, "wb") as f:
85+
f.write(b"content")
86+
item = DownloadQueueItem("data.txt", 7, "v1", False, 0, chunk_path)
87+
task = UpdateTask("data.txt", [item], destination_file=output_file)
88+
89+
monkeypatch.chdir(tmp_dir)
90+
task.apply(tmp_dir, None)
91+
92+
with open(os.path.join(tmp_dir, "out.txt"), "rb") as f:
93+
assert f.read() == b"content"

0 commit comments

Comments
 (0)