Skip to content

Commit 24213e4

Browse files
committed
Temp dir pull backkup
1 parent 5bec292 commit 24213e4

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

mergin/client_pull.py

Lines changed: 11 additions & 12 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 # full path to temporary directory where we store downloaded files
354+
self.temp_dir = temp_dir # TemporaryDirectory instance 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,8 +413,7 @@ 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 = mp.fpath_meta(f"fetch_{local_version}-{server_version}")
417-
os.makedirs(temp_dir, exist_ok=True)
416+
temp_dir = tempfile.TemporaryDirectory(prefix="mm-pull-", ignore_cleanup_errors=True, delete=True)
418417
pull_changes = mp.get_pull_changes(server_info["files"])
419418
mp.log.debug("pull changes:\n" + pprint.pformat(pull_changes))
420419
fetch_files = []
@@ -441,10 +440,10 @@ def pull_project_async(mc, directory):
441440

442441
for file in fetch_files:
443442
diff_only = _pulling_file_with_diffs(file)
444-
items = _download_items(file, temp_dir, diff_only)
443+
items = _download_items(file, temp_dir.name, diff_only)
445444

446445
# figure out destination path for the file
447-
file_dir = os.path.dirname(os.path.normpath(os.path.join(temp_dir, file["path"])))
446+
file_dir = os.path.dirname(os.path.normpath(os.path.join(temp_dir.name, file["path"])))
448447
basename = os.path.basename(file["diff"]["path"]) if diff_only else os.path.basename(file["path"])
449448
dest_file_path = os.path.join(file_dir, basename)
450449
os.makedirs(file_dir, exist_ok=True)
@@ -465,8 +464,8 @@ def pull_project_async(mc, directory):
465464
file_path = file["path"]
466465
mp.log.info(f"missing base file for {file_path} -> going to download it (version {server_version})")
467466
file["version"] = server_version
468-
items = _download_items(file, temp_dir, diff_only=False)
469-
dest_file_path = mp.fpath(file["path"], temp_dir)
467+
items = _download_items(file, temp_dir.name, diff_only=False)
468+
dest_file_path = mp.fpath(file["path"], temp_dir.name)
470469
# dest_file_path = os.path.join(os.path.dirname(os.path.normpath(os.path.join(temp_dir, file['path']))), os.path.basename(file['path']))
471470
files_to_merge.append(FileToMerge(dest_file_path, items))
472471
continue
@@ -604,10 +603,10 @@ def pull_project_finalize(job: PullJob):
604603
# download their full versions so we have them up-to-date for applying changes
605604
for file_path, file_diffs in job.basefiles_to_patch:
606605
basefile = job.mp.fpath_meta(file_path)
607-
server_file = job.mp.fpath(file_path, job.temp_dir)
606+
server_file = job.mp.fpath(file_path, job.temp_dir.name)
608607

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

625624
try:
626-
conflicts = job.mp.apply_pull_changes(job.pull_changes, job.temp_dir, job.project_info, job.mc)
625+
conflicts = job.mp.apply_pull_changes(job.pull_changes, job.temp_dir.name, job.project_info, job.mc)
627626
except Exception as e:
628627
job.mp.log.error("Failed to apply pull changes: " + str(e))
629628
job.mp.log.info("--- pull aborted")
@@ -636,7 +635,7 @@ def pull_project_finalize(job: PullJob):
636635
else:
637636
job.mp.log.info("--- pull finished -- at version " + job.mp.version())
638637

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

642641

@@ -866,4 +865,4 @@ def download_files_finalize(job):
866865

867866
# Remove temporary download directory
868867
if job.directory is not None and os.path.exists(job.directory):
869-
shutil.rmtree(job.directory)
868+
shutil.rmtree(job.directory)

0 commit comments

Comments
 (0)