Skip to content

Commit 52bd00c

Browse files
committed
move temp_dir to TemporaryDirectory instance
1 parent 56d8b8b commit 52bd00c

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

mergin/client_pull.py

Lines changed: 10 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,9 +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_mergin = os.path.join(tempfile.gettempdir(), f"mergin-{mp.project_id()}")
417-
temp_dir = os.path.join(temp_dir_mergin, f"fetch_{local_version}-{server_version}")
418-
os.makedirs(temp_dir, exist_ok=True)
416+
temp_dir = tempfile.TemporaryDirectory(prefix="mm-pull-", ignore_cleanup_errors=True, delete=True)
419417
pull_changes = mp.get_pull_changes(server_info["files"])
420418
mp.log.debug("pull changes:\n" + pprint.pformat(pull_changes))
421419
fetch_files = []
@@ -442,10 +440,10 @@ def pull_project_async(mc, directory):
442440

443441
for file in fetch_files:
444442
diff_only = _pulling_file_with_diffs(file)
445-
items = _download_items(file, temp_dir, diff_only)
443+
items = _download_items(file, temp_dir.name, diff_only)
446444

447445
# figure out destination path for the file
448-
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"])))
449447
basename = os.path.basename(file["diff"]["path"]) if diff_only else os.path.basename(file["path"])
450448
dest_file_path = os.path.join(file_dir, basename)
451449
os.makedirs(file_dir, exist_ok=True)
@@ -466,8 +464,8 @@ def pull_project_async(mc, directory):
466464
file_path = file["path"]
467465
mp.log.info(f"missing base file for {file_path} -> going to download it (version {server_version})")
468466
file["version"] = server_version
469-
items = _download_items(file, temp_dir, diff_only=False)
470-
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)
471469
# dest_file_path = os.path.join(os.path.dirname(os.path.normpath(os.path.join(temp_dir, file['path']))), os.path.basename(file['path']))
472470
files_to_merge.append(FileToMerge(dest_file_path, items))
473471
continue
@@ -605,10 +603,10 @@ def pull_project_finalize(job: PullJob):
605603
# download their full versions so we have them up-to-date for applying changes
606604
for file_path, file_diffs in job.basefiles_to_patch:
607605
basefile = job.mp.fpath_meta(file_path)
608-
server_file = job.mp.fpath(file_path, job.temp_dir)
606+
server_file = job.mp.fpath(file_path, job.temp_dir.name)
609607

610608
shutil.copy(basefile, server_file)
611-
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]
612610
patch_error = job.mp.apply_diffs(server_file, diffs)
613611
if patch_error:
614612
# that's weird that we are unable to apply diffs to the basefile!
@@ -624,7 +622,7 @@ def pull_project_finalize(job: PullJob):
624622
raise ClientError("Cannot patch basefile {}! Please try syncing again.".format(basefile))
625623

626624
try:
627-
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)
628626
except Exception as e:
629627
job.mp.log.error("Failed to apply pull changes: " + str(e))
630628
job.mp.log.info("--- pull aborted")
@@ -637,7 +635,7 @@ def pull_project_finalize(job: PullJob):
637635
else:
638636
job.mp.log.info("--- pull finished -- at version " + job.mp.version())
639637

640-
shutil.rmtree(job.temp_dir)
638+
job.temp_dir.cleanup() # delete our temporary dir and all its content
641639
return conflicts
642640

643641

0 commit comments

Comments
 (0)