Skip to content

Commit

Permalink
Add desktop file checks
Browse files Browse the repository at this point in the history
  • Loading branch information
bbhtt committed Dec 31, 2023
1 parent 7e6acc6 commit 9afb415
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
79 changes: 79 additions & 0 deletions flatpak_builder_lint/checks/desktop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os
import tempfile
from typing import Optional

from .. import appstream, builddir, ostree
from . import Check


class DesktopfileCheck(Check):
def _load(self, path: str, appid: str) -> Optional[dict]:

desktop_path = f"{path}/files/share/applications/{appid}.desktop"

is_baseapp = appid.endswith(".BaseApp")

if not is_baseapp:
if not os.path.exists(desktop_path):
self.errors.add("desktop-file-not-installed")
elif os.path.exists(desktop_path):
data = {} # type: dict
with open(desktop_path, "r", encoding="utf-8") as file:
for line in file:
line = line.strip()
if line.startswith("#"):
continue
if line.startswith("[") and line.endswith("]"):
group = line[1:-1]
data[group] = {}
if "=" in line:
key = line.split("=", 1)[0]
value = line.split("=", 1)[1]
data[group][key] = value

return data

return None

def _validate(self, path: str, appid: str) -> None:
d = self._load(path, appid)
if d is not None:
icon = d["Desktop Entry"]["Icon"]
if icon != appid:
self.errors.add("desktop-file-wrong-icon-name")

def check_build(self, path: str) -> None:
appid = builddir.infer_appid(path)
if not appid:
return
metadata = builddir.get_metadata(path)
if not metadata:
return
is_extension = metadata.get("extension")

self._validate(f"{path}", appid)
appstream_path = f"{path}/files/share/app-info/xmls/{appid}.xml.gz"
if (
is_extension
or os.path.exists(appstream_path)
and appstream.is_console(appstream_path)
):
self.errors.discard("desktop-file-not-installed")
self.errors.discard("desktop-file-wrong-icon-name")

def check_repo(self, path: str) -> None:
self._populate_ref(path)
ref = self.repo_primary_ref
if not ref:
return
appid = ref.split("/")[1]

with tempfile.TemporaryDirectory() as tmpdir:
ret = ostree.extract_subpath(path, ref, "/", tmpdir)
if ret["returncode"] != 0:
raise RuntimeError("Failed to extract ostree repo")
appstream_path = f"{tmpdir}/files/share/app-info/xmls/{appid}.xml.gz"
self._validate(tmpdir, appid)
if os.path.exists(appstream_path) and appstream.is_console(appstream_path):
self.errors.discard("desktop-file-not-installed")
self.errors.discard("desktop-file-wrong-icon-name")
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Desktop Entry]
Version=1.0
Name=Desktop application
Comment=A desktop application
Exec=foo %u
# Icon does not match app-id
Icon=org.flathub.foo
Terminal=false
Type=Application
2 changes: 2 additions & 0 deletions tests/builddir/desktop-file/metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Application]
name=com.github.flathub_infra.desktop-file
7 changes: 7 additions & 0 deletions tests/test_builddir.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,10 @@ def test_builddir_console() -> None:

def test_builddir_metadata_spaces() -> None:
ret = run_checks("tests/builddir/metadata-spaces")

def test_builddir_desktop_file() -> None:
ret = run_checks("tests/builddir/desktop-file")
error = "desktop-file-wrong-icon-name"
found_errors = set(ret["errors"])

assert error in found_errors

0 comments on commit 9afb415

Please sign in to comment.