[BUGFIX] Clear working directory before starting any downloads (#382)
* [BUGFIX] Clear working directory before starting any downloads * update fixture * fix prebuilt tests * check if dir exists before del in fixture
This commit is contained in:
parent
c8c66b5660
commit
c47b504cb4
3 changed files with 84 additions and 81 deletions
|
|
@ -95,9 +95,10 @@ class SubscriptionDownload(BaseSubscription, ABC):
|
||||||
entry=entry,
|
entry=entry,
|
||||||
)
|
)
|
||||||
|
|
||||||
def _delete_working_directory(self, is_error: bool) -> None:
|
def _delete_working_directory(self, is_error: bool = False) -> None:
|
||||||
_ = is_error
|
_ = is_error
|
||||||
shutil.rmtree(self.working_directory)
|
if os.path.isdir(self.working_directory):
|
||||||
|
shutil.rmtree(self.working_directory)
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def _prepare_working_directory(self):
|
def _prepare_working_directory(self):
|
||||||
|
|
@ -105,6 +106,7 @@ class SubscriptionDownload(BaseSubscription, ABC):
|
||||||
Context manager to create all directories to the working directory. Deletes the entire
|
Context manager to create all directories to the working directory. Deletes the entire
|
||||||
working directory when cleaning up.
|
working directory when cleaning up.
|
||||||
"""
|
"""
|
||||||
|
self._delete_working_directory()
|
||||||
os.makedirs(self.working_directory, exist_ok=True)
|
os.makedirs(self.working_directory, exist_ok=True)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -113,7 +115,7 @@ class SubscriptionDownload(BaseSubscription, ABC):
|
||||||
self._delete_working_directory(is_error=True)
|
self._delete_working_directory(is_error=True)
|
||||||
raise exc
|
raise exc
|
||||||
else:
|
else:
|
||||||
self._delete_working_directory(is_error=False)
|
self._delete_working_directory()
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def _maintain_archive_file(self):
|
def _maintain_archive_file(self):
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
@ -27,10 +28,11 @@ def working_directory() -> str:
|
||||||
"""
|
"""
|
||||||
with tempfile.TemporaryDirectory() as temp_dir:
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
|
|
||||||
def _assert_working_directory_empty(self, is_error: bool):
|
def _assert_working_directory_empty(self, is_error: bool = False):
|
||||||
files = [str(file_path) for file_path in _get_files_in_directory(temp_dir)]
|
files = [str(file_path) for file_path in _get_files_in_directory(temp_dir)]
|
||||||
num_files = len(files)
|
num_files = len(files)
|
||||||
shutil.rmtree(temp_dir)
|
if os.path.isdir(temp_dir):
|
||||||
|
shutil.rmtree(temp_dir)
|
||||||
|
|
||||||
if not is_error:
|
if not is_error:
|
||||||
if num_files > 0:
|
if num_files > 0:
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import tempfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
from typing import List
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
@ -126,85 +127,83 @@ def mock_download_collection_entries(
|
||||||
def _(**kwargs):
|
def _(**kwargs):
|
||||||
return mock_entry_dict_factory(**kwargs)
|
return mock_entry_dict_factory(**kwargs)
|
||||||
|
|
||||||
collection_1_entry_dicts = [
|
def _write_entries_to_working_dir(*args, **kwargs) -> List[Dict]:
|
||||||
_(
|
if (len(args[0].collection.urls.list) == 1) or (
|
||||||
uid="21-1",
|
"season.2" in kwargs["url"] and len(args[0].download_options.urls.list) > 1
|
||||||
upload_date="20210808",
|
):
|
||||||
playlist_index=1,
|
return [
|
||||||
playlist_count=4,
|
_(
|
||||||
is_youtube_channel=is_youtube_channel,
|
uid="21-1",
|
||||||
), # 1
|
upload_date="20210808",
|
||||||
_(
|
playlist_index=1,
|
||||||
uid="20-1",
|
playlist_count=4,
|
||||||
upload_date="20200808",
|
is_youtube_channel=is_youtube_channel,
|
||||||
playlist_index=2,
|
), # 1
|
||||||
playlist_count=4,
|
_(
|
||||||
is_youtube_channel=is_youtube_channel,
|
uid="20-1",
|
||||||
), # 2 98
|
upload_date="20200808",
|
||||||
_(
|
playlist_index=2,
|
||||||
uid="20-2",
|
playlist_count=4,
|
||||||
upload_date="20200808",
|
is_youtube_channel=is_youtube_channel,
|
||||||
playlist_index=3,
|
), # 2 98
|
||||||
playlist_count=4,
|
_(
|
||||||
is_youtube_channel=is_youtube_channel,
|
uid="20-2",
|
||||||
), # 1 99
|
upload_date="20200808",
|
||||||
_(
|
playlist_index=3,
|
||||||
uid="20-3",
|
playlist_count=4,
|
||||||
upload_date="20200807",
|
is_youtube_channel=is_youtube_channel,
|
||||||
playlist_index=4,
|
), # 1 99
|
||||||
playlist_count=4,
|
_(
|
||||||
is_youtube_channel=is_youtube_channel,
|
uid="20-3",
|
||||||
),
|
upload_date="20200807",
|
||||||
]
|
playlist_index=4,
|
||||||
collection_2_entry_dicts = [
|
playlist_count=4,
|
||||||
# 20-3 should resolve to collection 1 (which is season 2)
|
is_youtube_channel=is_youtube_channel,
|
||||||
_(
|
),
|
||||||
uid="20-3",
|
]
|
||||||
upload_date="20200807",
|
return [
|
||||||
playlist_index=1,
|
# 20-3 should resolve to collection 1 (which is season 2)
|
||||||
playlist_count=5,
|
_(
|
||||||
is_youtube_channel=is_youtube_channel,
|
uid="20-3",
|
||||||
),
|
upload_date="20200807",
|
||||||
_(
|
playlist_index=1,
|
||||||
uid="20-4",
|
playlist_count=5,
|
||||||
upload_date="20200806",
|
is_youtube_channel=is_youtube_channel,
|
||||||
playlist_index=2,
|
),
|
||||||
playlist_count=5,
|
_(
|
||||||
is_youtube_channel=is_youtube_channel,
|
uid="20-4",
|
||||||
),
|
upload_date="20200806",
|
||||||
_(
|
playlist_index=2,
|
||||||
uid="20-5",
|
playlist_count=5,
|
||||||
upload_date="20200706",
|
is_youtube_channel=is_youtube_channel,
|
||||||
playlist_index=3,
|
),
|
||||||
playlist_count=5,
|
_(
|
||||||
is_youtube_channel=is_youtube_channel,
|
uid="20-5",
|
||||||
),
|
upload_date="20200706",
|
||||||
_(
|
playlist_index=3,
|
||||||
uid="20-6",
|
playlist_count=5,
|
||||||
upload_date="20200706",
|
is_youtube_channel=is_youtube_channel,
|
||||||
playlist_index=4,
|
),
|
||||||
playlist_count=5,
|
_(
|
||||||
is_youtube_channel=is_youtube_channel,
|
uid="20-6",
|
||||||
),
|
upload_date="20200706",
|
||||||
_(
|
playlist_index=4,
|
||||||
uid="20-7",
|
playlist_count=5,
|
||||||
upload_date="20200606",
|
is_youtube_channel=is_youtube_channel,
|
||||||
playlist_index=5,
|
),
|
||||||
playlist_count=5,
|
_(
|
||||||
is_youtube_channel=is_youtube_channel,
|
uid="20-7",
|
||||||
),
|
upload_date="20200606",
|
||||||
]
|
playlist_index=5,
|
||||||
|
playlist_count=5,
|
||||||
|
is_youtube_channel=is_youtube_channel,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
with patch.object(
|
with patch.object(
|
||||||
Downloader, "extract_info_via_info_json"
|
Downloader, "extract_info_via_info_json", new=_write_entries_to_working_dir
|
||||||
) as mock_download_metadata, patch.object(
|
), patch.object(Downloader, "_extract_entry_info_with_retry", new=lambda _, entry: entry):
|
||||||
Downloader, "_extract_entry_info_with_retry", new=lambda _, entry: entry
|
|
||||||
):
|
|
||||||
# Stub out metadata. TODO: update this if we do metadata plugins
|
# Stub out metadata. TODO: update this if we do metadata plugins
|
||||||
mock_download_metadata.side_effect = [
|
|
||||||
collection_1_entry_dicts,
|
|
||||||
collection_2_entry_dicts,
|
|
||||||
]
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
return _mock_download_collection_entries_factory
|
return _mock_download_collection_entries_factory
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue