Skip to content

Commit d441cf2

Browse files
authored
Remove diff files (#176)
* add function to clean after push_project * if file size is zero remove and consider file not_updated * do not check for size, only remove existing file * add remove_diff_files() * call cleaning function * import * remove cleaning * revert changes * add test checking if diff files are cleaned --------- Co-authored-by: Jan Caha <[email protected]>
1 parent 9a1c607 commit d441cf2

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

mergin/client_push.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import pprint
1515
import tempfile
1616
import concurrent.futures
17+
import os
1718

1819
from .common import UPLOAD_CHUNK_SIZE, ClientError
1920
from .merginproject import MerginProject
@@ -286,6 +287,8 @@ def push_project_finalize(job):
286287

287288
job.tmp_dir.cleanup() # delete our temporary dir and all its content
288289

290+
remove_diff_files(job)
291+
289292
job.mp.log.info("--- push finished - new project version " + job.server_resp["version"])
290293

291294

@@ -316,3 +319,13 @@ def _do_upload(item, job):
316319

317320
item.upload_blocking(job.mc, job.mp)
318321
job.transferred_size += item.size
322+
323+
324+
def remove_diff_files(job) -> None:
325+
"""Looks for diff files in the job and removes them."""
326+
327+
for change in job.changes["updated"]:
328+
if "diff" in change.keys():
329+
diff_file = job.mp.fpath_meta(change["diff"]["path"])
330+
if os.path.exists(diff_file):
331+
os.remove(diff_file)

mergin/merginproject.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ def get_push_changes(self):
357357
"mtime": datetime.fromtimestamp(os.path.getmtime(diff_file), tzlocal()),
358358
}
359359
else:
360+
if os.path.exists(diff_file):
361+
os.remove(diff_file)
360362
not_updated.append(file)
361363
except (pygeodiff.GeoDiffLibError, pygeodiff.GeoDiffLibConflictError) as e:
362364
self.log.warning("failed to create changeset for " + path)

mergin/test/test_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99
import pytz
1010
import sqlite3
11+
import glob
1112

1213
from .. import InvalidProject
1314
from ..client import MerginClient, ClientError, MerginProject, LoginError, decode_token_data, TokenError, ServerType
@@ -1890,3 +1891,27 @@ def test_version_info(mc):
18901891
created = datetime.strptime(info["created"], "%Y-%m-%dT%H:%M:%SZ")
18911892
assert created.date() == date.today()
18921893
assert info["changes"]["updated"][0]["size"] == 98304
1894+
1895+
1896+
def test_clean_diff_files(mc):
1897+
test_project = "test_clean"
1898+
project = API_USER + "/" + test_project
1899+
project_dir = os.path.join(TMP_DIR, test_project) # primary project dir for updates
1900+
project_dir_2 = os.path.join(TMP_DIR, test_project + "_2") # concurrent project dir
1901+
1902+
cleanup(mc, project, [project_dir, project_dir_2])
1903+
# create remote project
1904+
shutil.copytree(TEST_DATA_DIR, project_dir)
1905+
mc.create_project_and_push(test_project, project_dir)
1906+
1907+
# test push changes with diffs:
1908+
mp = MerginProject(project_dir)
1909+
f_updated = "base.gpkg"
1910+
# step 1) base.gpkg updated to inserted_1_A (inserted A feature)
1911+
shutil.move(mp.fpath(f_updated), mp.fpath_meta(f_updated)) # make local copy for changeset calculation
1912+
shutil.copy(mp.fpath("inserted_1_A.gpkg"), mp.fpath(f_updated))
1913+
mc.push_project(project_dir)
1914+
1915+
diff_files = glob.glob("*-diff-*", root_dir=os.path.split(mp.fpath_meta("inserted_1_A.gpkg"))[0])
1916+
1917+
assert diff_files == []

0 commit comments

Comments
 (0)