From 2787d2382a4500936a94ba9a5e3821c4fad943b4 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 22 Nov 2023 09:32:23 +0100 Subject: pakjoy: Separate _patch and patch_webengine --- qutebrowser/browser/webengine/webenginesettings.py | 2 +- qutebrowser/misc/pakjoy.py | 35 ++++++++-------------- tests/unit/misc/test_pakjoy.py | 12 ++++---- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/qutebrowser/browser/webengine/webenginesettings.py b/qutebrowser/browser/webengine/webenginesettings.py index e1aa7c52e..20657685e 100644 --- a/qutebrowser/browser/webengine/webenginesettings.py +++ b/qutebrowser/browser/webengine/webenginesettings.py @@ -556,7 +556,7 @@ def init(): log.init.debug("Initializing profiles...") # Apply potential resource patches before initializing profiles. - pakjoy.patch() + pakjoy.patch_webengine() _init_default_profile() init_private_profile() diff --git a/qutebrowser/misc/pakjoy.py b/qutebrowser/misc/pakjoy.py index 478281186..016fc2b8e 100644 --- a/qutebrowser/misc/pakjoy.py +++ b/qutebrowser/misc/pakjoy.py @@ -172,19 +172,8 @@ def copy_webengine_resources() -> pathlib.Path: return work_dir -def patch(file_to_patch: pathlib.Path = None) -> 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: - try: - file_to_patch = copy_webengine_resources() / "qtwebengine_resources.pak" - except OSError: - log.misc.exception("Failed to copy webengine resources, not applying quirk") - return - +def _patch(file_to_patch: pathlib.Path) -> None: + """Apply any patches to the given pak file.""" if not file_to_patch.exists(): log.misc.error( "Resource pak doesn't exist at expected location! " @@ -203,14 +192,16 @@ def patch(file_to_patch: pathlib.Path = None) -> None: log.misc.exception("Failed to apply quirk to resources pak.") -if __name__ == "__main__": - output_test_file = pathlib.Path("/tmp/test.pak") - #shutil.copy("/opt/google/chrome/resources.pak", output_test_file) - shutil.copy("/usr/share/qt6/resources/qtwebengine_resources.pak", output_test_file) - patch(output_test_file) +def patch_webengine() -> None: + """Apply any patches to webengine resource pak files.""" + versions = version.qtwebengine_versions(avoid_init=True) + if versions.webengine != utils.VersionNumber(6, 6): + return - with open(output_test_file, "rb") as fd: - reparsed = PakParser(fd) + try: + webengine_resources_path = copy_webengine_resources() + except OSError: + log.misc.exception("Failed to copy webengine resources, not applying quirk") + return - print(reparsed.manifest_entry) - print(reparsed.manifest) + _patch(webengine_resources_path / "qtwebengine_resources.pak") diff --git a/tests/unit/misc/test_pakjoy.py b/tests/unit/misc/test_pakjoy.py index e2c828cb9..26c386759 100644 --- a/tests/unit/misc/test_pakjoy.py +++ b/tests/unit/misc/test_pakjoy.py @@ -65,7 +65,7 @@ def affected_version(monkeypatch): def test_version_gate(unaffected_version, mocker): fake_open = mocker.patch("qutebrowser.misc.pakjoy.open") - pakjoy.patch() + pakjoy.patch_webengine() assert not fake_open.called @@ -95,7 +95,7 @@ class TestWithRealResourcesFile: # Go through the full patching processes with the real resources file from # the current installation. Make sure our replacement string is in it # afterwards. - pakjoy.patch() + pakjoy.patch_webengine() patched_resources = pathlib.Path(os.environ["QTWEBENGINE_RESOURCES_PATH"]) @@ -136,12 +136,12 @@ class TestWithRealResourcesFile: monkeypatch.setattr(pakjoy.shutil, osfunc, lambda *_args: raiseme(PermissionError(osfunc))) with caplog.at_level(logging.ERROR, "misc"): - pakjoy.patch() + pakjoy.patch_webengine() assert caplog.messages == ["Failed to copy webengine resources, not applying quirk"] def test_expected_file_not_found(self, tmp_cache, monkeypatch, caplog): with caplog.at_level(logging.ERROR, "misc"): - pakjoy.patch(pathlib.Path(tmp_cache) / "doesntexist") + pakjoy._patch(pathlib.Path(tmp_cache) / "doesntexist") assert caplog.messages[-1].startswith( "Resource pak doesn't exist at expected location! " "Not applying quirks. Expected location: " @@ -264,13 +264,13 @@ class TestWithConstructedResourcesFile: affected_version): buffer = pak_factory(entries=[json_manifest_factory(url=b"example.com")]) - # Write bytes to file so we can test pakjoy.patch() + # Write bytes to file so we can test pakjoy._patch() tmpfile = pathlib.Path(tmp_cache) / "bad.pak" with open(tmpfile, "wb") as fd: fd.write(buffer.read()) with caplog.at_level(logging.ERROR, "misc"): - pakjoy.patch(tmpfile) + pakjoy._patch(tmpfile) assert caplog.messages == [ "Failed to apply quirk to resources pak." -- cgit v1.2.3-54-g00ecf