From b161ec1feade62411e882853a45677e56289eb2e Mon Sep 17 00:00:00 2001 From: toofar Date: Fri, 3 Nov 2023 13:17:32 +1300 Subject: Fix lint, move resource dir copying to method Fixes lint: shadows variables, docstrings, unused attribute Move resource dir copying to its own method: * will probably help with unit tests * allows for cleaner overriding of what file to patch (instead of a big nested block) Adds a sneaky `assert` statement in there because the pak file should definitely exist at that location. Although it not existing shouldn't really be fatal... Maintains the thing in `__main__`, although I'm not sure if we need to keep that? --- qutebrowser/misc/pakjoy.py | 67 ++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/qutebrowser/misc/pakjoy.py b/qutebrowser/misc/pakjoy.py index c2647c478..ba0c216c4 100644 --- a/qutebrowser/misc/pakjoy.py +++ b/qutebrowser/misc/pakjoy.py @@ -1,4 +1,3 @@ - # SPDX-FileCopyrightText: Florian Bruhin (The-Compiler) # # SPDX-License-Identifier: GPL-3.0-or-later @@ -50,7 +49,7 @@ class Pak5Header: encoding: int # uint32 resource_count: int # uint16 - alias_count: int # uint16 + _alias_count: int # uint16 _FORMAT: ClassVar[str] = ' None: """Parse the .pak file from the given file object.""" - version = binparsing.unpack(" int: + """Return byte offset of TARGET_URL into the pak file.""" try: return self.manifest_entry.file_offset + self.manifest.index(TARGET_URL) except ValueError: @@ -143,45 +145,46 @@ class PakParser: raise binparsing.ParseError("Couldn't find hangouts manifest") -def patch(): - versions = version.qtwebengine_versions(avoid_init=True) - if versions.webengine != utils.VersionNumber(6, 6): - return - - resources_path = qtutils.library_path(qtutils.LibraryPath.data) / "resources" +def copy_webengine_resources(): + """Copy qtwebengine resources to local dir for patching.""" + resources_dir = qtutils.library_path(qtutils.LibraryPath.data) / "resources" work_dir = pathlib.Path(standarddir.cache()) / "webengine_resources_pak_quirk" - patched_file = work_dir / "qtwebengine_resources.pak" - print(f"{work_dir=} {work_dir.exists()=}") - print(f"{resources_path=}") if work_dir.exists(): + # TODO: make backup? shutil.rmtree(work_dir) - shutil.copytree(resources_path, work_dir) - - with open(patched_file, "r+b") as f: - parser = PakParser(f) - offset = parser.find_patch_offset() - f.seek(offset) - f.write(REPLACEMENT_URL) + shutil.copytree(resources_dir, work_dir) os.environ["QTWEBENGINE_RESOURCES_PATH"] = str(work_dir) + return work_dir -if __name__ == "__main__": - import shutil - shutil.copy("/usr/share/qt6/resources/qtwebengine_resources.pak", "/tmp/test.pak") - with open("/tmp/test.pak", "r+b") as f: +def patch(file_to_patch: pathlib.Path = None): + """Apply any patches to webengine resource pak files.""" + versions = version.qtwebengine_versions(avoid_init=True) + if versions.webengine != utils.VersionNumber(6, 6): + return + + if not file_to_patch: + file_to_patch = copy_webengine_resources() / "qtwebengine_resources.pak" + assert file_to_patch.exists() + + with open(file_to_patch, "r+b") as f: parser = PakParser(f) - print(parser.manifest_entry) - print(parser.manifest) offset = parser.find_patch_offset() f.seek(offset) f.write(REPLACEMENT_URL) - with open("/tmp/test.pak", "rb") as f: - parser = PakParser(f) - print(parser.manifest_entry) - print(parser.manifest) +if __name__ == "__main__": + output_test_file = pathlib.Path("/tmp/test.pak") + shutil.copy("/usr/share/qt6/resources/qtwebengine_resources.pak", output_test_file) + patch(output_test_file) + + with open(output_test_file, "rb") as fd: + reparsed = PakParser(fd) + + print(reparsed.manifest_entry) + print(reparsed.manifest) -- cgit v1.2.3-54-g00ecf