Skip to content

Commit 5bec292

Browse files
authored
Merge pull request #260 from MerginMaps/revert-253-pull-to-temp-dir
Revert "Store pull files in temp directory"
2 parents 3cf8ade + 8568b15 commit 5bec292

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

mergin/client_pull.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def __init__(
351351
self.version = version
352352
self.files_to_merge = files_to_merge # list of FileToMerge instances
353353
self.download_queue_items = download_queue_items
354-
self.temp_dir = temp_dir # TemporaryDirectory instance where we store downloaded files
354+
self.temp_dir = temp_dir # full path to temporary directory where we store downloaded files
355355
self.mp = mp # MerginProject instance
356356
self.is_cancelled = False
357357
self.project_info = project_info # parsed JSON with project info returned from the server
@@ -413,7 +413,8 @@ def pull_project_async(mc, directory):
413413
# then we just download the whole file
414414
_pulling_file_with_diffs = lambda f: "diffs" in f and len(f["diffs"]) != 0
415415

416-
temp_dir = tempfile.TemporaryDirectory(prefix="mm-pull-", ignore_cleanup_errors=True, delete=True)
416+
temp_dir = mp.fpath_meta(f"fetch_{local_version}-{server_version}")
417+
os.makedirs(temp_dir, exist_ok=True)
417418
pull_changes = mp.get_pull_changes(server_info["files"])
418419
mp.log.debug("pull changes:\n" + pprint.pformat(pull_changes))
419420
fetch_files = []
@@ -440,10 +441,10 @@ def pull_project_async(mc, directory):
440441

441442
for file in fetch_files:
442443
diff_only = _pulling_file_with_diffs(file)
443-
items = _download_items(file, temp_dir.name, diff_only)
444+
items = _download_items(file, temp_dir, diff_only)
444445

445446
# figure out destination path for the file
446-
file_dir = os.path.dirname(os.path.normpath(os.path.join(temp_dir.name, file["path"])))
447+
file_dir = os.path.dirname(os.path.normpath(os.path.join(temp_dir, file["path"])))
447448
basename = os.path.basename(file["diff"]["path"]) if diff_only else os.path.basename(file["path"])
448449
dest_file_path = os.path.join(file_dir, basename)
449450
os.makedirs(file_dir, exist_ok=True)
@@ -464,8 +465,8 @@ def pull_project_async(mc, directory):
464465
file_path = file["path"]
465466
mp.log.info(f"missing base file for {file_path} -> going to download it (version {server_version})")
466467
file["version"] = server_version
467-
items = _download_items(file, temp_dir.name, diff_only=False)
468-
dest_file_path = mp.fpath(file["path"], temp_dir.name)
468+
items = _download_items(file, temp_dir, diff_only=False)
469+
dest_file_path = mp.fpath(file["path"], temp_dir)
469470
# dest_file_path = os.path.join(os.path.dirname(os.path.normpath(os.path.join(temp_dir, file['path']))), os.path.basename(file['path']))
470471
files_to_merge.append(FileToMerge(dest_file_path, items))
471472
continue
@@ -603,10 +604,10 @@ def pull_project_finalize(job: PullJob):
603604
# download their full versions so we have them up-to-date for applying changes
604605
for file_path, file_diffs in job.basefiles_to_patch:
605606
basefile = job.mp.fpath_meta(file_path)
606-
server_file = job.mp.fpath(file_path, job.temp_dir.name)
607+
server_file = job.mp.fpath(file_path, job.temp_dir)
607608

608609
shutil.copy(basefile, server_file)
609-
diffs = [job.mp.fpath(f, job.temp_dir.name) for f in file_diffs]
610+
diffs = [job.mp.fpath(f, job.temp_dir) for f in file_diffs]
610611
patch_error = job.mp.apply_diffs(server_file, diffs)
611612
if patch_error:
612613
# that's weird that we are unable to apply diffs to the basefile!
@@ -622,7 +623,7 @@ def pull_project_finalize(job: PullJob):
622623
raise ClientError("Cannot patch basefile {}! Please try syncing again.".format(basefile))
623624

624625
try:
625-
conflicts = job.mp.apply_pull_changes(job.pull_changes, job.temp_dir.name, job.project_info, job.mc)
626+
conflicts = job.mp.apply_pull_changes(job.pull_changes, job.temp_dir, job.project_info, job.mc)
626627
except Exception as e:
627628
job.mp.log.error("Failed to apply pull changes: " + str(e))
628629
job.mp.log.info("--- pull aborted")
@@ -635,7 +636,7 @@ def pull_project_finalize(job: PullJob):
635636
else:
636637
job.mp.log.info("--- pull finished -- at version " + job.mp.version())
637638

638-
job.temp_dir.cleanup() # delete our temporary dir and all its content
639+
shutil.rmtree(job.temp_dir)
639640
return conflicts
640641

641642

0 commit comments

Comments
 (0)