From 8a9bbd2eeb844c69e76b21dbe49a8014b229c363 Mon Sep 17 00:00:00 2001 From: Shengjing Zhu Date: Wed, 25 Dec 2024 08:29:45 +0800 Subject: [PATCH] urlchecker: Extract version from deb for rotating url --- src/checkers/urlchecker.py | 8 ++++ src/lib/utils.py | 10 +++++ tests/com.tencent.WeChat.yaml | 9 ++++ ...checker.py => test_urlchecker_appimage.py} | 0 tests/test_urlchecker_deb.py | 44 +++++++++++++++++++ 5 files changed, 71 insertions(+) create mode 100644 tests/com.tencent.WeChat.yaml rename tests/{test_urlchecker.py => test_urlchecker_appimage.py} (100%) create mode 100644 tests/test_urlchecker_deb.py diff --git a/src/checkers/urlchecker.py b/src/checkers/urlchecker.py index 767eb5af..6086f394 100644 --- a/src/checkers/urlchecker.py +++ b/src/checkers/urlchecker.py @@ -114,6 +114,14 @@ async def check(self, external_data: ExternalBase): version_string = await utils.extract_appimage_version( tmpfile, ) + if url.endswith(".deb"): + with tempfile.NamedTemporaryFile("w+b") as tmpfile: + new_version = await utils.get_extra_data_info_from_url( + url, session=self.session, dest_io=tmpfile + ) + version_string = utils.extract_deb_version( + tmpfile, + ) else: new_version = await utils.get_extra_data_info_from_url( url, session=self.session diff --git a/src/lib/utils.py b/src/lib/utils.py index 8ffbb606..f7a2fd56 100644 --- a/src/lib/utils.py +++ b/src/lib/utils.py @@ -18,6 +18,8 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import apt_inst +import apt_pkg import datetime as dt import zoneinfo import json @@ -449,6 +451,14 @@ async def extract_appimage_version(appimg_io: t.IO): return kf.get_string(GLib.KEY_FILE_DESKTOP_GROUP, "X-AppImage-Version") +def extract_deb_version(deb_io: t.IO): + assert deb_io.name + log.debug("file path %s", deb_io.name) + content = apt_inst.DebFile(deb_io.name).control.extractdata("control") + log.debug("deb content %s", content) + return apt_pkg.TagSection(content).get("Version") + + _GITHUB_URL_PATTERN = re.compile( r""" ^git@github.com: diff --git a/tests/com.tencent.WeChat.yaml b/tests/com.tencent.WeChat.yaml new file mode 100644 index 00000000..6f38900d --- /dev/null +++ b/tests/com.tencent.WeChat.yaml @@ -0,0 +1,9 @@ +app-id: com.tencent.WeChat +modules: + - name: wechat + sources: + - type: extra-data + filename: wechat.deb + url: https://dldir1v6.qq.com/weixin/Universal/Linux/WeChatLinux_x86_64.deb + sha256: "0000000000000000000000000000000000000000000000000000000000000000" + size: 0 diff --git a/tests/test_urlchecker.py b/tests/test_urlchecker_appimage.py similarity index 100% rename from tests/test_urlchecker.py rename to tests/test_urlchecker_appimage.py diff --git a/tests/test_urlchecker_deb.py b/tests/test_urlchecker_deb.py new file mode 100644 index 00000000..daa69c16 --- /dev/null +++ b/tests/test_urlchecker_deb.py @@ -0,0 +1,44 @@ +import unittest +import os + +from src.manifest import ManifestChecker +from src.lib.utils import init_logging +from src.lib.checksums import MultiDigest + +TEST_MANIFEST = os.path.join(os.path.dirname(__file__), "com.tencent.WeChat.yaml") + + +class TestURLChecker(unittest.IsolatedAsyncioTestCase): + def setUp(self): + init_logging() + + async def test_check(self): + checker = ManifestChecker(TEST_MANIFEST) + ext_data = await checker.check() + + data = self._find_by_filename(ext_data, "wechat.deb") + self.assertIsNotNone(data) + self.assertEqual(data.filename, "wechat.deb") + self.assertIsNotNone(data.new_version) + self.assertIsInstance(data.new_version.size, int) + self.assertGreater(data.new_version.size, 0) + self.assertIsNotNone(data.new_version.checksum) + self.assertIsInstance(data.new_version.checksum, MultiDigest) + self.assertNotEqual( + data.new_version.checksum, + MultiDigest( + sha256="0000000000000000000000000000000000000000000000000000000000000000" # noqa: E501 + ), + ) + self.assertIsNotNone(data.new_version.version) + + def _find_by_filename(self, ext_data, filename): + for data in ext_data: + if data.filename == filename: + return data + else: + return None + + +if __name__ == "__main__": + unittest.main()