flatpak: Try getting ID from /.flatpak_info

(cherry picked from commit 8b49d87526)
This commit is contained in:
Florian Bruhin 2021-03-31 09:50:25 +02:00
parent 45d34846c8
commit 8d18255563
7 changed files with 57 additions and 36 deletions

View File

@ -236,7 +236,8 @@ def _init_runtime(args: Optional[argparse.Namespace]) -> None:
if version.is_flatpak():
*parts, app_name = os.path.split(path)
assert app_name == APPNAME, app_name
flatpak_id = os.environ.get('FLATPAK_ID', 'org.qutebrowser.qutebrowser')
flatpak_id = version.flatpak_id()
assert flatpak_id is not None
path = os.path.join(*parts, 'app', flatpak_id)
_create(path)

View File

@ -27,6 +27,8 @@ import platform
import subprocess
import importlib
import collections
import pathlib
import configparser
import enum
import datetime
import getpass
@ -195,10 +197,27 @@ def is_flatpak() -> bool:
If packaged via Flatpak, the environment is has restricted access to the host
system.
"""
current_distro = distribution()
if current_distro is None:
return False
return current_distro.parsed == Distribution.kde_flatpak
return flatpak_id() is not None
_FLATPAK_INFO_PATH = '/.flatpak-info'
def flatpak_id() -> Optional[str]:
"""Get the ID of the currently running Flatpak (or None if outside of Flatpak)."""
if 'FLATPAK_ID' in os.environ:
return os.environ['FLATPAK_ID']
# 'FLATPAK_ID' was only added in Flatpak 1.2.0:
# https://lists.freedesktop.org/archives/flatpak/2019-January/001464.html
# but e.g. Ubuntu 18.04 ships 1.0.9.
info_file = pathlib.Path(_FLATPAK_INFO_PATH)
if not info_file.exists():
return None
parser = configparser.ConfigParser()
parser.read(info_file)
return parser['Application']['name']
def _git_str() -> Optional[str]:

View File

@ -448,7 +448,7 @@ def test_disabled(qtwe_version, config_stub):
@pytest.mark.fake_os('linux')
def test_no_locales_available(qtwe_version, monkeypatch, caplog):
def test_no_locales_available(qtwe_version, monkeypatch, caplog, request):
path = pathlib.Path('/doesnotexist/qtwebengine_locales')
assert not path.exists()
monkeypatch.setattr(qtargs, '_webengine_locales_path', lambda: path)

View File

@ -236,8 +236,14 @@ class TestSocketName:
(None, 'ipc-{}'.format(md5('testusername'))),
('/x', 'ipc-{}'.format(md5('testusername-/x'))),
])
@pytest.mark.parametrize('has_flatpak_id', [True, False])
@pytest.mark.skipif(not version.is_flatpak(), reason="Needs Flatpak")
def test_flatpak(self, basedir, fake_runtime_dir, expected):
def test_flatpak(self, monkeypatch, fake_runtime_dir,
basedir, expected, has_flatpak_id):
if not has_flatpak_id:
# Simulate an older Flatpak version
monkeypatch.delenv('FLATPAK_ID', raising=False)
socketname = ipc._get_socketname(basedir)
expected_path = str(
fake_runtime_dir / 'app' / 'org.qutebrowser.qutebrowser' / expected)

View File

@ -209,9 +209,7 @@ class TestStandardDir:
@pytest.mark.linux
@pytest.mark.parametrize('args_basedir', [True, False])
@pytest.mark.parametrize('has_flatpak_id', [True, False])
def test_flatpak_runtimedir(self, monkeypatch, tmp_path,
args_basedir, has_flatpak_id):
def test_flatpak_runtimedir(self, monkeypatch, tmp_path, args_basedir):
runtime_path = tmp_path / 'runtime'
runtime_path.mkdir()
runtime_path.chmod(0o0700)
@ -220,11 +218,7 @@ class TestStandardDir:
monkeypatch.setattr(version, 'is_flatpak', lambda: True)
monkeypatch.setenv('XDG_RUNTIME_DIR', str(runtime_path))
if has_flatpak_id:
monkeypatch.setenv('FLATPAK_ID', app_id)
else:
monkeypatch.delenv('FLATPAK_ID', raising=False)
monkeypatch.setenv('FLATPAK_ID', app_id)
if args_basedir:
init_args = types.SimpleNamespace(basedir=str(tmp_path))

View File

@ -770,15 +770,8 @@ class TestOpenFile:
@pytest.fixture
def sandbox_patch(self, monkeypatch):
info = version.DistributionInfo(
id='org.kde.Platform',
parsed=version.Distribution.kde_flatpak,
version=VersionNumber.parse('5.12'),
pretty='Unknown')
if not version.is_flatpak():
monkeypatch.setattr(version, 'distribution', lambda: info)
monkeypatch.setenv('FLATPAK_ID', 'org.qutebrowser.qutebrowser')
assert version.is_flatpak()
def test_cmdline_sandboxed(self, sandbox_patch,

View File

@ -304,19 +304,27 @@ def test_distribution(tmpdir, monkeypatch, os_release, expected):
assert version.distribution() == expected
@pytest.mark.parametrize('distribution, expected', [
(None, False),
(version.DistributionInfo(
id='org.kde.Platform', parsed=version.Distribution.kde_flatpak,
version=utils.VersionNumber(5, 12),
pretty='Unknown'), True),
(version.DistributionInfo(
id='arch', parsed=version.Distribution.arch, version=None,
pretty='Arch Linux'), False)
])
def test_is_flatpak(monkeypatch, distribution, expected):
monkeypatch.setattr(version, "distribution", lambda: distribution)
assert version.is_flatpak() == expected
@pytest.mark.parametrize('has_env', [True, False])
@pytest.mark.parametrize('has_file', [True, False])
def test_is_flatpak(monkeypatch, tmp_path, has_env, has_file):
if has_env:
monkeypatch.setenv('FLATPAK_ID', 'org.qutebrowser.qutebrowser')
else:
monkeypatch.delenv('FLATPAK_ID', raising=False)
fake_info_path = tmp_path / '.flatpak_info'
if has_file:
lines = [
"[Application]",
"name=org.qutebrowser.qutebrowser",
"runtime=runtime/org.kde.Platform/x86_64/5.15",
]
fake_info_path.write_text('\n'.join(lines))
else:
assert not fake_info_path.exists()
monkeypatch.setattr(version, '_FLATPAK_INFO_PATH', str(fake_info_path))
assert version.is_flatpak() == (has_env or has_file)
class GitStrSubprocessFake: