Skip to content

Commit

Permalink
os.replace -> shutil.move
Browse files Browse the repository at this point in the history
  • Loading branch information
Marishka17 committed Jan 9, 2025
1 parent 716f13c commit 7abd5ab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
8 changes: 4 additions & 4 deletions cvat/apps/dataset_manager/tests/test_rest_api_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ def patched_log_exception(logger=None, exc_info=True):
# only after checking whether a file exists inside an acquired lock
patch("cvat.apps.dataset_manager.views.osp_exists") as mock_osp_exists,
patch(
"cvat.apps.dataset_manager.views.os.replace", side_effect=original_replace
"cvat.apps.dataset_manager.views.shutil.move", side_effect=original_replace
) as mock_os_replace,
patch("cvat.apps.dataset_manager.views.log_exception", new=patched_log_exception),
patch("cvat.apps.dataset_manager.views.task.export_task") as mock_export_fn,
Expand Down Expand Up @@ -1861,7 +1861,7 @@ def test_export_can_reuse_older_file_if_still_relevant(self):
patch(
"cvat.apps.dataset_manager.views.osp_exists", side_effect=original_exists
) as mock_osp_exists,
patch("cvat.apps.dataset_manager.views.os.replace") as mock_os_replace,
patch("cvat.apps.dataset_manager.views.shutil.move") as mock_os_replace,
):
second_export_path = export(dst_format=format_name, task_id=task_id)

Expand Down Expand Up @@ -1908,7 +1908,7 @@ def _export_1(
"cvat.apps.dataset_manager.views.get_export_cache_lock",
new=self.patched_get_export_cache_lock,
),
patch("cvat.apps.dataset_manager.views.os.replace") as mock_os_replace,
patch("cvat.apps.dataset_manager.views.shutil.move") as mock_os_replace,
patch("cvat.apps.dataset_manager.views.task.export_task") as mock_export_fn,
patch("cvat.apps.dataset_manager.views.django_rq.get_scheduler"),
):
Expand Down Expand Up @@ -1948,7 +1948,7 @@ def _export_2(
"cvat.apps.dataset_manager.views.get_export_cache_lock",
new=self.patched_get_export_cache_lock,
),
patch("cvat.apps.dataset_manager.views.os.replace") as mock_os_replace,
patch("cvat.apps.dataset_manager.views.shutil.move") as mock_os_replace,
patch("cvat.apps.dataset_manager.views.task.export_task") as mock_export_fn,
patch("cvat.apps.dataset_manager.views.django_rq.get_scheduler"),
):
Expand Down
7 changes: 6 additions & 1 deletion cvat/apps/dataset_manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import os
import os.path as osp
import shutil
from datetime import timedelta
from os.path import exists as osp_exists

Expand Down Expand Up @@ -169,16 +170,20 @@ def export(

with TmpDirManager.get_tmp_directory_for_export(instance_type=instance_type) as temp_dir:
temp_file = osp.join(temp_dir, 'result')
# create a subdirectory to store export-related files,
# which will be fully included in the resulting archive
temp_subdir = osp.join(temp_dir, 'subdir')
os.makedirs(temp_subdir, exist_ok=True)

export_fn(db_instance.id, temp_file, format_name=dst_format,
server_url=server_url, save_images=save_images, temp_dir=temp_subdir)

with get_export_cache_lock(
output_path,
ttl=EXPORT_CACHE_LOCK_TTL,
acquire_timeout=EXPORT_CACHE_LOCK_ACQUISITION_TIMEOUT,
):
os.replace(temp_file, output_path)
shutil.move(temp_file, output_path)

logger.info(
f"The {db_instance.__class__.__name__.lower()} '{db_instance.id}' is exported "
Expand Down
2 changes: 1 addition & 1 deletion cvat/apps/engine/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ def create_backup(
acquire_timeout=EXPORT_CACHE_LOCK_ACQUISITION_TIMEOUT,
ttl=EXPORT_CACHE_LOCK_TTL,
):
os.replace(temp_file, output_path)
shutil.move(temp_file, output_path)

logger.info(
f"The {db_instance.__class__.__name__.lower()} '{db_instance.id}' is backed up at {output_path!r} "
Expand Down

0 comments on commit 7abd5ab

Please sign in to comment.