From a5c14082ed5d628152401b78c4fe0e5e1e9b9b46 Mon Sep 17 00:00:00 2001 From: drew2a Date: Sat, 16 Jul 2022 10:03:35 +0300 Subject: [PATCH] Return corrupt logic --- .../watch_folder/tests/test_watch_folder.py | 13 +++-- .../components/watch_folder/watch_folder.py | 54 ++++++++++++------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/tribler/core/components/watch_folder/tests/test_watch_folder.py b/src/tribler/core/components/watch_folder/tests/test_watch_folder.py index 78ce58bb87e..6a7c4f951ae 100644 --- a/src/tribler/core/components/watch_folder/tests/test_watch_folder.py +++ b/src/tribler/core/components/watch_folder/tests/test_watch_folder.py @@ -10,9 +10,8 @@ from tribler.core.components.watch_folder.settings import WatchFolderSettings from tribler.core.components.watch_folder.watch_folder import WatchFolder from tribler.core.tests.tools.common import TESTS_DATA_DIR, TORRENT_UBUNTU_FILE - - # pylint: disable=redefined-outer-name, protected-access +from tribler.core.utilities.path_util import Path @pytest.fixture @@ -55,9 +54,13 @@ async def test_watchfolder_torrent_file_corrupt(watch_folder: WatchFolder): directory = watch_folder.settings.get_path_as_absolute('directory', watch_folder.state_dir) shutil.copyfile(TORRENT_UBUNTU_FILE, directory / "test.torrent") - shutil.copyfile(TESTS_DATA_DIR / 'test_rss.xml', directory / "test2.torrent") - with pytest.raises(NoCrashException): - await watch_folder.check_watch_folder_handle_exceptions() + corrupted_torrent = directory / "test2.torrent" + shutil.copyfile(TESTS_DATA_DIR / 'test_rss.xml', corrupted_torrent) + + await watch_folder.check_watch_folder_handle_exceptions() + + assert not corrupted_torrent.exists() + assert Path(f'{corrupted_torrent}.corrupt').exists() @patch.object(TorrentDef, 'get_metainfo', Mock(return_value=None)) diff --git a/src/tribler/core/components/watch_folder/watch_folder.py b/src/tribler/core/components/watch_folder/watch_folder.py index 1ddd0dc0b3d..01f94540163 100644 --- a/src/tribler/core/components/watch_folder/watch_folder.py +++ b/src/tribler/core/components/watch_folder/watch_folder.py @@ -51,31 +51,47 @@ def check_watch_folder(self): return directory = self.settings.get_path_as_absolute('directory', self.state_dir) + self._logger.debug(f'Watch dir: {directory}') if not directory.is_dir(): self._logger.debug(f'Cancelled. Is not directory: {directory}.') return - # Make sure that we pass a str to os.walk - watch_dir = str(directory) - self._logger.debug(f'Watch dir: {watch_dir}') - - for root, _, files in os.walk(watch_dir): - torrent_files = (file for file in files if file.endswith(".torrent")) - for name in torrent_files: + for root, _, files in os.walk(str(directory)): + for name in files: path = Path(root) / name - self._logger.info(f'Torrent file found: {path}') + self.process_torrent_file(path) + + self._logger.debug('Checking watch folder completed.') + + def process_torrent_file(self, path: Path): + if not path.name.endswith(".torrent"): + return + self._logger.info(f'Torrent file found: {path}') + exception = None + try: + self.start_download(path) + except Exception as e: + self._logger.error(f'{e.__class__.__name__}: {e}') + exception = e - tdef = TorrentDef.load(path) - if not tdef.get_metainfo(): - self._logger.warning(f'Missed metainfo: {path}') - continue + if exception: + self._logger.info(f'Corrupted: {path}') + try: + path.replace(f'{path}.corrupt') + except OSError as e: + self._logger.warning(f'{e.__class__.__name__}: {e}') - infohash = tdef.get_infohash() + def start_download(self, path: Path): - if not self.download_manager.download_exists(infohash): - self._logger.info("Starting download from torrent file %s", name) - download_config = DownloadConfig.from_defaults(self.download_manager.download_defaults) - download_config.state_dir = self.state_dir - self.download_manager.start_download(torrent_file=path, config=download_config) + tdef = TorrentDef.load(path) + if not tdef.get_metainfo(): + self._logger.warning(f'Missed metainfo: {path}') + return - self._logger.debug('Checking watch folder completed.') + infohash = tdef.get_infohash() + + if not self.download_manager.download_exists(infohash): + self._logger.info("Starting download from torrent file %s", path.name) + download_config = DownloadConfig.from_defaults(self.download_manager.download_defaults) + download_config.state_dir = self.state_dir + self.download_manager.start_download(torrent_file=path, config=download_config)