Skip to content

Commit 7e3f598

Browse files
committed
Merge branch 'master' into download-project-temp-dir
2 parents c68e9de + 3cf8ade commit 7e3f598

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

mergin/client_pull.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def __init__(
368368
self.version = version
369369
self.files_to_merge = files_to_merge # list of FileToMerge instances
370370
self.download_queue_items = download_queue_items
371-
self.temp_dir = temp_dir # full path to temporary directory where we store downloaded files
371+
self.temp_dir = temp_dir # TemporaryDirectory instance where we store downloaded files
372372
self.mp = mp # MerginProject instance
373373
self.is_cancelled = False
374374
self.project_info = project_info # parsed JSON with project info returned from the server
@@ -430,8 +430,7 @@ def pull_project_async(mc, directory):
430430
# then we just download the whole file
431431
_pulling_file_with_diffs = lambda f: "diffs" in f and len(f["diffs"]) != 0
432432

433-
temp_dir = mp.fpath_meta(f"fetch_{local_version}-{server_version}")
434-
os.makedirs(temp_dir, exist_ok=True)
433+
temp_dir = tempfile.TemporaryDirectory(prefix="mm-pull-", ignore_cleanup_errors=True, delete=True)
435434
pull_changes = mp.get_pull_changes(server_info["files"])
436435
mp.log.debug("pull changes:\n" + pprint.pformat(pull_changes))
437436
fetch_files = []
@@ -458,10 +457,10 @@ def pull_project_async(mc, directory):
458457

459458
for file in fetch_files:
460459
diff_only = _pulling_file_with_diffs(file)
461-
items = _download_items(file, temp_dir, diff_only)
460+
items = _download_items(file, temp_dir.name, diff_only)
462461

463462
# figure out destination path for the file
464-
file_dir = os.path.dirname(os.path.normpath(os.path.join(temp_dir, file["path"])))
463+
file_dir = os.path.dirname(os.path.normpath(os.path.join(temp_dir.name, file["path"])))
465464
basename = os.path.basename(file["diff"]["path"]) if diff_only else os.path.basename(file["path"])
466465
dest_file_path = os.path.join(file_dir, basename)
467466
os.makedirs(file_dir, exist_ok=True)
@@ -482,8 +481,8 @@ def pull_project_async(mc, directory):
482481
file_path = file["path"]
483482
mp.log.info(f"missing base file for {file_path} -> going to download it (version {server_version})")
484483
file["version"] = server_version
485-
items = _download_items(file, temp_dir, diff_only=False)
486-
dest_file_path = mp.fpath(file["path"], temp_dir)
484+
items = _download_items(file, temp_dir.name, diff_only=False)
485+
dest_file_path = mp.fpath(file["path"], temp_dir.name)
487486
# dest_file_path = os.path.join(os.path.dirname(os.path.normpath(os.path.join(temp_dir, file['path']))), os.path.basename(file['path']))
488487
files_to_merge.append(FileToMerge(dest_file_path, items))
489488
continue
@@ -621,10 +620,10 @@ def pull_project_finalize(job: PullJob):
621620
# download their full versions so we have them up-to-date for applying changes
622621
for file_path, file_diffs in job.basefiles_to_patch:
623622
basefile = job.mp.fpath_meta(file_path)
624-
server_file = job.mp.fpath(file_path, job.temp_dir)
623+
server_file = job.mp.fpath(file_path, job.temp_dir.name)
625624

626625
shutil.copy(basefile, server_file)
627-
diffs = [job.mp.fpath(f, job.temp_dir) for f in file_diffs]
626+
diffs = [job.mp.fpath(f, job.temp_dir.name) for f in file_diffs]
628627
patch_error = job.mp.apply_diffs(server_file, diffs)
629628
if patch_error:
630629
# that's weird that we are unable to apply diffs to the basefile!
@@ -640,7 +639,7 @@ def pull_project_finalize(job: PullJob):
640639
raise ClientError("Cannot patch basefile {}! Please try syncing again.".format(basefile))
641640

642641
try:
643-
conflicts = job.mp.apply_pull_changes(job.pull_changes, job.temp_dir, job.project_info, job.mc)
642+
conflicts = job.mp.apply_pull_changes(job.pull_changes, job.temp_dir.name, job.project_info, job.mc)
644643
except Exception as e:
645644
job.mp.log.error("Failed to apply pull changes: " + str(e))
646645
job.mp.log.info("--- pull aborted")
@@ -653,7 +652,7 @@ def pull_project_finalize(job: PullJob):
653652
else:
654653
job.mp.log.info("--- pull finished -- at version " + job.mp.version())
655654

656-
shutil.rmtree(job.temp_dir)
655+
job.temp_dir.cleanup() # delete our temporary dir and all its content
657656
return conflicts
658657

659658

0 commit comments

Comments
 (0)