[Core] Make create_torrent return a deferred

This allows to create a torrent file on the remote server
and get its content in one call.
This commit is contained in:
Radu Carpa 2023-08-02 13:43:02 +02:00 committed by Calum Lind
parent b63699c6de
commit 4088e13905
No known key found for this signature in database
GPG Key ID: 90597A687B836BA3
2 changed files with 42 additions and 18 deletions

View File

@ -12,7 +12,6 @@ import logging
import os import os
import shutil import shutil
import tempfile import tempfile
import threading
from base64 import b64decode, b64encode from base64 import b64decode, b64encode
from typing import Any, Dict, List, Optional, Tuple, Union from typing import Any, Dict, List, Optional, Tuple, Union
from urllib.request import URLError, urlopen from urllib.request import URLError, urlopen
@ -1001,21 +1000,19 @@ class Core(component.Component):
add_to_session=False, add_to_session=False,
): ):
log.debug('creating torrent..') log.debug('creating torrent..')
threading.Thread( return threads.deferToThread(
target=self._create_torrent_thread, self._create_torrent_thread,
args=( path,
path, tracker,
tracker, piece_length,
piece_length, comment=comment,
comment, target=target,
target, webseeds=webseeds,
webseeds, private=private,
private, created_by=created_by,
created_by, trackers=trackers,
trackers, add_to_session=add_to_session,
add_to_session, )
),
).start()
def _create_torrent_thread( def _create_torrent_thread(
self, self,
@ -1055,12 +1052,13 @@ class Core(component.Component):
with open(target, 'wb') as _file: with open(target, 'wb') as _file:
_file.write(filecontent) _file.write(filecontent)
filedump = b64encode(filecontent)
log.debug('torrent created!') log.debug('torrent created!')
if add_to_session: if add_to_session:
options = {} options = {}
options['download_location'] = os.path.split(path)[0] options['download_location'] = os.path.split(path)[0]
filedump = b64encode(filecontent)
self.add_torrent_file(filename, filedump, options) self.add_torrent_file(filename, filedump, options)
return filename, filedump
@export @export
def upload_plugin(self, filename: str, filedump: Union[str, bytes]) -> None: def upload_plugin(self, filename: str, filedump: Union[str, bytes]) -> None:

View File

@ -3,7 +3,7 @@
# the additional special exception to link portions of this program with the OpenSSL library. # the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details. # See LICENSE for more details.
# #
import base64
import os import os
from base64 import b64encode from base64 import b64encode
from hashlib import sha1 as sha from hashlib import sha1 as sha
@ -483,3 +483,29 @@ class TestCore(BaseTestCase):
assert self.core._create_peer_id('2.0.1rc1') == '-DE201r-' assert self.core._create_peer_id('2.0.1rc1') == '-DE201r-'
assert self.core._create_peer_id('2.11.0b2') == '-DE2B0b-' assert self.core._create_peer_id('2.11.0b2') == '-DE2B0b-'
assert self.core._create_peer_id('2.4.12b2.dev3') == '-DE24CD-' assert self.core._create_peer_id('2.4.12b2.dev3') == '-DE24CD-'
@pytest.mark.parametrize(
'path',
[
common.get_test_data_file('deluge.png'),
os.path.dirname(common.get_test_data_file('deluge.png')),
],
)
@pytest.mark.parametrize('piece_length', [2**14, 2**16])
@pytest_twisted.inlineCallbacks
def test_create_torrent(self, path, tmp_path, piece_length):
target = tmp_path / 'test.torrent'
filename, filedump = yield self.core.create_torrent(
path=path,
tracker=None,
piece_length=piece_length,
target=target,
add_to_session=False,
)
filecontent = base64.b64decode(filedump)
with open(target, 'rb') as f:
assert f.read() == filecontent
lt.torrent_info(filecontent)