Skip to content

Commit db7be6c

Browse files
committed
add optional download_path for pattern with diff files
1 parent 55d9e5a commit db7be6c

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

mergin/client_pull.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,18 @@ def merge(self):
135135
raise ClientError("Download of file {} failed. Please try it again.".format(self.dest_file))
136136

137137

138-
def get_download_items(file_path: str, file_size: int, file_version: str, directory: str, diff_only=False):
138+
def get_download_items(
139+
file_path: str,
140+
file_size: int,
141+
file_version: str,
142+
download_directory: str,
143+
download_path: Optional[str] = None,
144+
diff_only=False,
145+
):
139146
"""Returns an array of download queue items"""
140147

141-
file_dir = os.path.dirname(os.path.normpath(os.path.join(directory, file_path)))
142-
basename = os.path.basename(file_path) if diff_only else os.path.basename(file_path)
148+
file_dir = os.path.dirname(os.path.normpath(os.path.join(download_directory, file_path)))
149+
basename = os.path.basename(download_path) if download_path else os.path.basename(file_path)
143150
chunks = math.ceil(file_size / CHUNK_SIZE)
144151

145152
items = []
@@ -467,7 +474,7 @@ def get_diff_merge_files(delta_item: ProjectDeltaItem, target_dir: str) -> List[
467474

468475
for diff in delta_item.diffs:
469476
dest_file_path = prepare_file_destination(target_dir, diff.id)
470-
download_items = get_download_items(diff.id, diff.size, diff.version, target_dir, True)
477+
download_items = get_download_items(delta_item.path, diff.size, diff.version, target_dir, diff.id, True)
471478
result.append(FileToMerge(dest_file_path, download_items))
472479
return result
473480

@@ -558,7 +565,7 @@ def pull_project_async(mc, directory) -> Optional[PullJob]:
558565
# or we removed it within previous pull because we failed to apply patch the older version for some reason).
559566
# But it's not a problem - we will download the newest version and we're sorted.
560567
mp.log.info(f"missing base file for {item.path} -> going to download it (version {server_version})")
561-
items = get_download_items(item.path, item.size, server_version, tmp_dir.name, diff_only=False)
568+
items = get_download_items(item.path, item.size, server_version, tmp_dir.name)
562569
dest_file_path = mp.fpath(item.path, tmp_dir.name)
563570
merge_files.append(FileToMerge(dest_file_path, items))
564571
basefiles_to_patch.append((item.path, [diff.id for diff in item.diffs]))
@@ -665,8 +672,11 @@ def pull_project_finalize(job: PullJob):
665672
if not job.project_info and job.v2_pull:
666673
project_info_response = job.mc.project_info(job.project_path, version=job.version)
667674
job.project_info = asdict(project_info_response)
668-
else:
669-
raise ClientError("Missing project info for pull finalization")
675+
676+
if not job.project_info:
677+
job.mp.log.error("No project info available to finalize pull")
678+
job.mp.log.info("--- pull aborted")
679+
raise ClientError("No project info available to finalize pull")
670680

671681
# merge downloaded chunks
672682
try:
@@ -781,7 +791,14 @@ def download_diffs_async(mc, project_directory, file_path, versions):
781791
total_size = 0
782792
for file in fetch_files:
783793
diff = file.get("diff")
784-
items = get_download_items(diff["path"], diff["size"], file["version"], mp.cache_dir, diff_only=True)
794+
items = get_download_items(
795+
file.get("path"),
796+
diff["size"],
797+
file["version"],
798+
mp.cache_dir,
799+
download_path=diff.get("path"),
800+
diff_only=True,
801+
)
785802
dest_file_path = mp.fpath_cache(diff["path"], version=file["version"])
786803
if os.path.exists(dest_file_path):
787804
continue

mergin/test/test_client_pull.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,24 @@ def test_get_diff_merge_files():
2929
size=300,
3030
checksum="789",
3131
diffs=[
32-
ProjectDeltaItemDiff(id="diff1", size=10, version="v3"),
33-
ProjectDeltaItemDiff(id="diff2", size=20, version="v3"),
32+
ProjectDeltaItemDiff(id="diff2", size=20, version="v2"),
3433
],
3534
)
3635

3736
merge_files = get_diff_merge_files(item, tmp_dir)
3837

39-
assert len(merge_files) == 2
38+
assert len(merge_files) == 1
4039

41-
# Check diff1
42-
f1 = merge_files[0]
43-
assert f1.dest_file == os.path.join(tmp_dir, "diff1")
44-
assert len(f1.downloaded_items) == 1
45-
assert f1.downloaded_items[0].file_path == "diff1"
46-
47-
# Check diff2
48-
f2 = merge_files[1]
40+
# Check diff
41+
f2 = merge_files[0]
4942
assert f2.dest_file == os.path.join(tmp_dir, "diff2")
5043
assert len(f2.downloaded_items) == 1
51-
assert f2.downloaded_items[0].file_path == "diff2"
44+
assert f2.downloaded_items[0].file_path == "data.gpkg"
45+
assert f2.downloaded_items[0].size == 20
46+
assert f2.downloaded_items[0].version == "v2"
5247

5348

49+
@pytest.fixture
5450
def test_get_download_items():
5551
with tempfile.TemporaryDirectory() as tmp_dir:
5652
# Case 1: Small file (one chunk)
@@ -86,6 +82,9 @@ def test_get_download_items():
8682
assert len(items) == 0
8783

8884
# Case 4: Diff only
89-
items = get_download_items("diff_file", 50, "v1", tmp_dir, diff_only=True)
85+
items = get_download_items("base.gpkg", 50, "v1", tmp_dir, download_path="diff_file", diff_only=True)
9086
assert len(items) == 1
9187
assert items[0].diff_only is True
88+
assert items[0].file_path == "base.gpkg"
89+
assert items[0].size == 50
90+
assert items[0].download_file_path == os.path.join(tmp_dir, "diff_file.0")

0 commit comments

Comments
 (0)