Exemple #1
0
def convert_config_to_tribler75(state_dir):
    """
    Convert the download config files from Tribler 7.4 to 7.5 format.
    """
    for filename in (state_dir / STATEDIR_CHECKPOINT_DIR).glob('*.conf'):
        try:
            config = DownloadConfig.load(filename)

            # Convert resume data
            resumedata = config.get_engineresumedata()
            if b'mapped_files' in resumedata:
                resumedata.pop(b'mapped_files')
                config.set_engineresumedata(resumedata)
                config.write(str(filename))

            # Convert metainfo
            metainfo = config.get_metainfo()
            if not config.config['download_defaults'].get(
                    'selected_files') or not metainfo:
                continue  # no conversion needed/possible, selected files will be reset to their default (i.e., all files)
            tdef = TorrentDef.load_from_dict(metainfo)
            config.set_selected_files([
                tdef.get_index_of_file_in_files(fn) for fn in
                config.config['download_defaults'].pop('selected_files')
            ])
            config.write(str(filename))
        except ConfigObjParseError:
            logger.error("Could not parse %s file so removing it", filename)
            os.remove(filename)
Exemple #2
0
    def load_checkpoint(self, filename):
        try:
            config = DownloadConfig.load(filename)
        except Exception:
            self._logger.exception("Could not open checkpoint file %s",
                                   filename)
            return

        metainfo = config.get_metainfo()
        if not metainfo:
            self._logger.error(
                "Could not resume checkpoint %s; metainfo not found", filename)
            return
        if not isinstance(metainfo, dict):
            self._logger.error(
                "Could not resume checkpoint %s; metainfo is not dict %s %s",
                filename, type(metainfo), repr(metainfo))
            return

        try:
            url = metainfo.get(b'url', None)
            url = url.decode('utf-8') if url else url
            tdef = (TorrentDefNoMetainfo(metainfo[b'infohash'],
                                         metainfo[b'name'], url) if b'infohash'
                    in metainfo else TorrentDef.load_from_dict(metainfo))
        except (KeyError, ValueError) as e:
            self._logger.exception(
                "Could not restore tdef from metainfo dict: %s %s ", e,
                metainfo)
            return

        if config.get_bootstrap_download():
            if hexlify(tdef.get_infohash(
            )) != self.tribler_session.config.get_bootstrap_infohash():
                self.remove_config(tdef.get_infohash())
                return

        config.state_dir = self.tribler_session.config.get_state_dir()
        if config.get_dest_dir() == '':  # removed torrent ignoring
            self._logger.info("Removing checkpoint %s destdir is %s", filename,
                              config.get_dest_dir())
            os.remove(filename)
            return

        try:
            if self.download_exists(tdef.get_infohash()):
                self._logger.info(
                    "Not resuming checkpoint because download has already been added"
                )
            else:
                self.start_download(tdef=tdef, config=config)
        except Exception:
            self._logger.exception(
                "Not resume checkpoint due to exception while adding download")
Exemple #3
0
async def test_save_resume(mock_handle, test_download, test_tdef):
    """
    testing call resume data alert
    """
    mock_handle.is_valid = lambda: True
    mock_handle.save_resume_data = lambda: test_download.register_task(
        'post_alert',
        test_download.process_alert,
        alert,
        'save_resume_data_alert',
        delay=0.1)

    alert = Mock(resume_data={b'info-hash': test_tdef.get_infohash()})
    await test_download.save_resume_data()
    basename = hexlify(test_tdef.get_infohash()) + '.conf'
    filename = test_download.dlmgr.get_checkpoint_dir() / basename
    dcfg = DownloadConfig.load(str(filename))
    assert test_tdef.get_infohash(), dcfg.get_engineresumedata().get(
        b'info-hash')
Exemple #4
0
 async def test_save_resume(self):
     """
     testing call resume data alert
     """
     tdef = self.create_tdef()
     alert = Mock(resume_data={b'info-hash': tdef.get_infohash()})
     dl = Download(self.session, tdef)
     dl.setup()
     dl.handle = MockObject()
     dl.handle.is_valid = lambda: True
     dl.handle.save_resume_data = lambda: dl.register_task(
         'post_alert',
         dl.process_alert,
         alert,
         'save_resume_data_alert',
         delay=0.1)
     await dl.save_resume_data()
     basename = hexlify(tdef.get_infohash()) + '.conf'
     filename = self.session.dlmgr.get_checkpoint_dir() / basename
     dcfg = DownloadConfig.load(filename)
     self.assertEqual(tdef.get_infohash(),
                      dcfg.get_engineresumedata().get(b'info-hash'))
     await dl.shutdown()
Exemple #5
0
def test_default_download_config_load(tmpdir):
    with open(tmpdir / "dlconfig.conf", 'wb') as conf_file:
        conf_file.write(b"[Tribler]\nabc=def")

    dcfg = DownloadConfig.load(tmpdir / "dlconfig.conf")
    assert dcfg.config['Tribler']['abc'] == 'def'
Exemple #6
0
    def test_default_download_config_load(self):
        with open(self.session_base_dir / "dlconfig.conf", 'wb') as conf_file:
            conf_file.write(b"[Tribler]\nabc=def")

        dcfg = DownloadConfig.load(self.session_base_dir / "dlconfig.conf")
        self.assertEqual(dcfg.config['Tribler']['abc'], 'def')
Exemple #7
0
 def test_download_load_corrupt(self):
     dlcfg = DownloadConfig()
     dlcfg.load(self.CONFIG_FILES_DIR / "corrupt_download_config.conf")
Exemple #8
0
 def test_download_save_load(self):
     dlcfg = DownloadConfig()
     file_path = self.session_base_dir / "downloadconfig.conf"
     dlcfg.write(file_path)
     dlcfg.load(file_path)