From ff4e17190f36cb3bbf889adcffd1db780b526f04 Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Tue, 22 Mar 2016 16:10:28 -0700 Subject: [PATCH 1/3] Allow searching system-wide data path on Linux for userscripts --- qutebrowser/commands/userscripts.py | 12 ++++++++---- qutebrowser/utils/standarddir.py | 11 +++++++++++ tests/unit/utils/test_standarddir.py | 22 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/qutebrowser/commands/userscripts.py b/qutebrowser/commands/userscripts.py index 7a6c44f0e..27b21c2cb 100644 --- a/qutebrowser/commands/userscripts.py +++ b/qutebrowser/commands/userscripts.py @@ -344,13 +344,17 @@ def run(cmd, *args, win_id, env, verbose=False): user_agent = config.get('network', 'user-agent') if user_agent is not None: env['QUTE_USER_AGENT'] = user_agent - cmd = os.path.expanduser(cmd) + cmd_path = os.path.expanduser(cmd) # if cmd is not given as an absolute path, look it up # ~/.local/share/qutebrowser/userscripts (or $XDG_DATA_DIR) - if not os.path.isabs(cmd): - log.misc.debug("{} is no absolute path".format(cmd)) - cmd = os.path.join(standarddir.data(), "userscripts", cmd) + if not os.path.isabs(cmd_path): + log.misc.debug("{} is no absolute path".format(cmd_path)) + cmd_path = os.path.join(standarddir.data(), "userscripts", cmd) + if not os.path.exists(cmd_path): + cmd_path = os.path.join(standarddir.system_data(), + "userscripts", cmd) + log.misc.debug("Userscript to run: {}".format(cmd_path)) runner.run(cmd, *args, env=env, verbose=verbose) runner.finished.connect(commandrunner.deleteLater) diff --git a/qutebrowser/utils/standarddir.py b/qutebrowser/utils/standarddir.py index 351b94563..b1676b795 100644 --- a/qutebrowser/utils/standarddir.py +++ b/qutebrowser/utils/standarddir.py @@ -65,6 +65,17 @@ def data(): return path +def system_data(): + """Get a location for system-wide data. This path may be read-only.""" + if sys.platform.startswith('linux'): + path = "/usr/share/qutebrowser" + if not os.path.exists(path): + path = data() + else: + path = data() + return path + + def cache(): """Get a location for the cache.""" typ = QStandardPaths.CacheLocation diff --git a/tests/unit/utils/test_standarddir.py b/tests/unit/utils/test_standarddir.py index 7262cdd77..75f0eeb9e 100644 --- a/tests/unit/utils/test_standarddir.py +++ b/tests/unit/utils/test_standarddir.py @@ -313,3 +313,25 @@ class TestCreatingDir: func = getattr(standarddir, typ) func() + + +class TestSystemData: + + """Test system data path.""" + + def test_system_datadir_exist_linux(self, monkeypatch): + """Test that /usr/share/qutebrowser is used if path exists.""" + monkeypatch.setattr('sys.platform', "linux") + monkeypatch.setattr(os.path, 'exists', lambda path: True) + assert standarddir.system_data() == "/usr/share/qutebrowser" + + @pytest.mark.linux + def test_system_datadir_not_exist_linux(self, monkeypatch): + """Test that system-wide path isn't used on linux if path not exist.""" + monkeypatch.setattr(os.path, 'exists', lambda path: False) + assert standarddir.system_data() == standarddir.data() + + def test_system_datadir_unsupportedos(self, monkeypatch): + """Test that system-wide path is not used on non-Linux OS.""" + monkeypatch.setattr('sys.platform', "potato") + assert standarddir.system_data() == standarddir.data() From 439d8156017e5ee3c9cd1991d6ea9d230ac3130f Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Thu, 24 Mar 2016 16:26:19 -0700 Subject: [PATCH 2/3] Now runner calls cmd_path instead of cmd --- qutebrowser/commands/userscripts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/commands/userscripts.py b/qutebrowser/commands/userscripts.py index 27b21c2cb..f95818eb1 100644 --- a/qutebrowser/commands/userscripts.py +++ b/qutebrowser/commands/userscripts.py @@ -356,6 +356,6 @@ def run(cmd, *args, win_id, env, verbose=False): "userscripts", cmd) log.misc.debug("Userscript to run: {}".format(cmd_path)) - runner.run(cmd, *args, env=env, verbose=verbose) + runner.run(cmd_path, *args, env=env, verbose=verbose) runner.finished.connect(commandrunner.deleteLater) runner.finished.connect(runner.deleteLater) From f2f0f429fbf0b284fddf84b394bf5b3c175130ba Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 25 Mar 2016 11:09:03 +0100 Subject: [PATCH 3/3] Update docs --- CHANGELOG.asciidoc | 1 + README.asciidoc | 1 + doc/help/commands.asciidoc | 7 +++++-- qutebrowser/browser/commands.py | 9 ++++++--- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index eb3f2bbdf..b42ec2279 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -43,6 +43,7 @@ Changed `:download-cancel --all`. It does the same as `:download-clear`. - Improved detection of URLs/search terms when pasting multiple lines. - Don't remove `qutebrowser-editor-*` temporary file if editor subprocess crashed +- Userscripts are also searched in `/usr/share/qutebrowser/userscripts`. Fixed ~~~~~ diff --git a/README.asciidoc b/README.asciidoc index 8d534b252..181bd11aa 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -199,6 +199,7 @@ Contributors, sorted by the number of commits in descending order: * Fritz V155 Reichwald * Franz Fellner * Corentin Jule +* Clayton Craft * zwarag * xd1le * haxwithaxe diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 0d6fa8169..79e0549dd 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -625,8 +625,11 @@ Note the {url} variable which gets replaced by the current URL might be useful h * +'cmdline'+: The commandline to execute. ==== optional arguments -* +*-u*+, +*--userscript*+: Run the command as a userscript. Either store the userscript in `~/.local/share/qutebrowser/userscripts` - (or `$XDG_DATA_DIR`), or use an absolute path. +* +*-u*+, +*--userscript*+: Run the command as a userscript. You can use an absolute path, or store the userscript in one of those + locations: + - `~/.local/share/qutebrowser/userscripts` + (or `$XDG_DATA_DIR`) + - `/usr/share/qutebrowser/userscripts` * +*-v*+, +*--verbose*+: Show notifications when the command started/exited. * +*-d*+, +*--detach*+: Whether the command should be detached from qutebrowser. diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index c34c766a2..82a52cc3f 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -918,9 +918,12 @@ class CommandDispatcher: useful here. Args: - userscript: Run the command as a userscript. Either store the - userscript in `~/.local/share/qutebrowser/userscripts` - (or `$XDG_DATA_DIR`), or use an absolute path. + userscript: Run the command as a userscript. You can use an + absolute path, or store the userscript in one of those + locations: + - `~/.local/share/qutebrowser/userscripts` + (or `$XDG_DATA_DIR`) + - `/usr/share/qutebrowser/userscripts` verbose: Show notifications when the command started/exited. detach: Whether the command should be detached from qutebrowser. cmdline: The commandline to execute.