From 2f622ad69843f723f9fac0dc7ab5028fa62fb246 Mon Sep 17 00:00:00 2001 From: Quinten Stokkink Date: Thu, 17 Oct 2024 12:19:15 +0200 Subject: [PATCH] Purged instances of 'Tribler Experimental' --- .../core/content_discovery/community.py | 7 ++++++- .../restapi/create_torrent_endpoint.py | 7 ++++++- src/tribler/core/restapi/events_endpoint.py | 7 ++++++- src/tribler/core/restapi/rest_manager.py | 7 ++++++- .../core/content_discovery/test_community.py | 18 ++++++++++++++++-- .../core/restapi/test_events_endpoint.py | 2 +- 6 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/tribler/core/content_discovery/community.py b/src/tribler/core/content_discovery/community.py index c098ce38f4..79529f71fc 100644 --- a/src/tribler/core/content_discovery/community.py +++ b/src/tribler/core/content_discovery/community.py @@ -6,6 +6,7 @@ import time import uuid from binascii import hexlify, unhexlify +from importlib.metadata import version, PackageNotFoundError from itertools import count from typing import TYPE_CHECKING, Any, Callable, Sequence @@ -254,7 +255,11 @@ async def on_version_request(self, peer: Peer, _: VersionRequest) -> None: """ Callback for when our Tribler version and Operating System is requested. """ - version_response = VersionResponse("Tribler Experimental", sys.platform) + try: + v = version("tribler") + except PackageNotFoundError: + v = "git" + version_response = VersionResponse(f"Tribler {v}", sys.platform) self.ez_send(peer, version_response) @lazy_wrapper(VersionResponse) diff --git a/src/tribler/core/libtorrent/restapi/create_torrent_endpoint.py b/src/tribler/core/libtorrent/restapi/create_torrent_endpoint.py index 84501d03b5..882f2a45b4 100644 --- a/src/tribler/core/libtorrent/restapi/create_torrent_endpoint.py +++ b/src/tribler/core/libtorrent/restapi/create_torrent_endpoint.py @@ -1,6 +1,7 @@ import asyncio import base64 import json +from importlib.metadata import version, PackageNotFoundError from pathlib import Path import libtorrent as lt @@ -114,7 +115,11 @@ async def create_torrent(self, request: Request) -> RESTResponse: if parameters.get("export_dir"): export_dir = Path(parameters["export_dir"]) - params["created by"] = "Tribler version: Tribler Experimental" + try: + v = version("tribler") + except PackageNotFoundError: + v = "git" + params["created by"] = f"Tribler version: {v}" params["nodes"] = False params["httpseeds"] = False params["encoding"] = False diff --git a/src/tribler/core/restapi/events_endpoint.py b/src/tribler/core/restapi/events_endpoint.py index bee86db43f..e08661e1d2 100644 --- a/src/tribler/core/restapi/events_endpoint.py +++ b/src/tribler/core/restapi/events_endpoint.py @@ -4,6 +4,7 @@ import time from asyncio import CancelledError, Event, Future, Queue from contextlib import suppress +from importlib.metadata import version, PackageNotFoundError from traceback import format_exception from typing import TYPE_CHECKING, TypedDict @@ -103,9 +104,13 @@ def initial_message(self) -> MessageDict: """ Create the initial message to announce to the GUI. """ + try: + v = version("tribler") + except PackageNotFoundError: + v = "git" return { "topic": Notification.events_start.value.name, - "kwargs": {"public_key": self.public_key or "", "version": "Tribler Experimental"} + "kwargs": {"public_key": self.public_key or "", "version": v} } def error_message(self, reported_error: Exception) -> MessageDict: diff --git a/src/tribler/core/restapi/rest_manager.py b/src/tribler/core/restapi/rest_manager.py index ab7a9815eb..d962ba8df7 100644 --- a/src/tribler/core/restapi/rest_manager.py +++ b/src/tribler/core/restapi/rest_manager.py @@ -5,6 +5,7 @@ import traceback from asyncio.base_events import Server from functools import wraps +from importlib.metadata import version, PackageNotFoundError from pathlib import Path from typing import TYPE_CHECKING, Awaitable, Callable, Generic, TypeVar, cast @@ -210,13 +211,17 @@ async def start(self) -> None: Starts the HTTP API with the listen port as specified in the session configuration. """ self._logger.info("Starting RESTManager...") + try: + v = version("tribler") + except PackageNotFoundError: + v = "git" # Not using setup_aiohttp_apispec here, as we need access to the APISpec to set the security scheme aiohttp_apispec = AiohttpApiSpec( url="/docs/swagger.json", app=self.root_endpoint.app, title="Tribler REST API documentation", - version="Tribler Experimental", + version=f"Tribler {v}", swagger_path="/docs" ) if self.config.get("api/key"): diff --git a/src/tribler/test_unit/core/content_discovery/test_community.py b/src/tribler/test_unit/core/content_discovery/test_community.py index 3d27ac257a..92598f5d0d 100644 --- a/src/tribler/test_unit/core/content_discovery/test_community.py +++ b/src/tribler/test_unit/core/content_discovery/test_community.py @@ -5,8 +5,9 @@ from binascii import hexlify from typing import TYPE_CHECKING, cast from unittest import skipIf -from unittest.mock import AsyncMock, Mock +from unittest.mock import AsyncMock, Mock, patch +import tribler from ipv8.messaging.payload import IntroductionRequestPayload, NewIntroductionRequestPayload from ipv8.test.base import TestBase from ipv8.test.mocking.endpoint import MockEndpointListener @@ -205,7 +206,20 @@ async def test_request_for_version(self) -> None: message, = received self.assertEqual(sys.platform, message.platform) - self.assertEqual("Tribler Experimental", message.version) + self.assertEqual("Tribler git", message.version) + + async def test_request_for_version_build(self) -> None: + """ + Test if a build version request is responded to. + """ + with patch.dict(tribler.core.content_discovery.community.__dict__, {"version": lambda _: "1.2.3"}), \ + self.assertReceivedBy(0, [VersionResponse]) as received: + self.overlay(0).ez_send(self.peer(1), VersionRequest()) + await self.deliver_messages() + message, = received + + self.assertEqual(sys.platform, message.platform) + self.assertEqual("Tribler 1.2.3", message.version) def test_search_for_tags_no_db(self) -> None: """ diff --git a/src/tribler/test_unit/core/restapi/test_events_endpoint.py b/src/tribler/test_unit/core/restapi/test_events_endpoint.py index d30d740d8e..6a9ca9502e 100644 --- a/src/tribler/test_unit/core/restapi/test_events_endpoint.py +++ b/src/tribler/test_unit/core/restapi/test_events_endpoint.py @@ -115,7 +115,7 @@ async def test_establish_connection(self) -> None: self.assertEqual(200, response.status) self.assertEqual((b'event: events_start\n' - b'data: {"public_key": "", "version": "Tribler Experimental"}' + b'data: {"public_key": "", "version": "git"}' b'\n\n'), request.payload_writer.captured[0]) async def test_establish_connection_with_error(self) -> None: