Skip to content

Commit

Permalink
Skip appstream checks for baseapps, extensions or console applications
Browse files Browse the repository at this point in the history
This is a workaround until flatpak-builder-lint becomes fully
flathub.json aware for builddir and repo checks

Baseapps, extensions or console applications should at minimum install
a valid appdata file to be able to skip checks

Baseapps and extensions can skip through "appstream-failed-validation"
and "appstream-missing-icon-file" while console applications can skip
"appstream-missing-icon-file"
  • Loading branch information
bbhtt authored and barthalion committed Nov 28, 2023
1 parent 3efa25a commit d47ac5c
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 15 deletions.
22 changes: 19 additions & 3 deletions flatpak_builder_lint/appstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,29 @@ def validate(path: str) -> dict:
return ret


def is_developer_name_present(path: str) -> bool:
def parse_appinfo_xml(path: str) -> list:
if not os.path.isfile(path):
raise FileNotFoundError("AppStream file not found")
raise FileNotFoundError("AppStream app-info file not found")

root = etree.parse(path)
components = root.xpath("/components/component")

developer = components[0].xpath("developer_name")
return list(components)


def is_developer_name_present(path: str) -> bool:
developer = parse_appinfo_xml(path)[0].xpath("developer_name")

return bool(developer)


def component_type(path: str) -> str:
type = parse_appinfo_xml(path)[0].attrib.get("type")

return str(type)


def is_console(path: str) -> bool:
if component_type(path) == "console-application":
return True
return False
3 changes: 3 additions & 0 deletions flatpak_builder_lint/builddir.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def parse_metadata(ini: str) -> dict:
tags = [x for x in metadata["tags"].split(";") if x]
metadata["tags"] = tags

if "ExtensionOf" in parser:
metadata["extension"] = "yes"

permissions: dict = defaultdict(set)

if "Context" in parser:
Expand Down
45 changes: 33 additions & 12 deletions flatpak_builder_lint/checks/metainfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ def _validate(self, path: str, appid: str, flathub_json: Optional[dict]) -> None
skip_appstream_check = flathub_json.get("skip-appstream-check")
skip_icons_check = flathub_json.get("skip-icons-check")

appstream_path = f"{path}/app-info/xmls/{appid}.xml.gz"
icon_path = f"{path}/app-info/icons/flatpak/128x128/{appid}.png"
appstream_path = f"{path}/files/share/app-info/xmls/{appid}.xml.gz"
icon_path = f"{path}/files/share/app-info/icons/flatpak/128x128/{appid}.png"
metainfo_dirs = [
f"{path}/metainfo",
f"{path}/appdata",
f"{path}/files/share/metainfo",
f"{path}/files/share/appdata",
]
metainfo_exts = [".appdata.xml", ".metainfo.xml"]
metainfo_path = None

is_baseapp = appid.endswith(".BaseApp")

for metainfo_dir in metainfo_dirs:
for ext in metainfo_exts:
metainfo_dirext = f"{metainfo_dir}/{appid}{ext}"
Expand All @@ -35,24 +37,40 @@ def _validate(self, path: str, appid: str, flathub_json: Optional[dict]) -> None
if not metainfo_path:
self.errors.add("appstream-metainfo-missing")

if metainfo_path:
appinfo_validation = appstream.validate(metainfo_path)
if appinfo_validation["returncode"] != 0:
self.errors.add("appstream-failed-validation")

if metainfo_path is not None:
if not appstream.is_developer_name_present(appstream_path):
self.warnings.add("appstream-missing-developer-name")

if not is_baseapp:
appinfo_validation = appstream.validate(metainfo_path)
if appinfo_validation["returncode"] != 0:
self.errors.add("appstream-failed-validation")

if not (skip_icons_check or skip_appstream_check):
if not os.path.exists(icon_path):
self.errors.add("appstream-missing-icon-file")
if not is_baseapp:
self.errors.add("appstream-missing-icon-file")

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")

flathub_json = builddir.get_flathub_json(path)
self._validate(f"{path}/files/share", appid, flathub_json)
self._validate(f"{path}", appid, flathub_json)

if is_extension:
self.errors.discard("appstream-failed-validation")
self.errors.discard("appstream-missing-icon-file")

appstream_path = f"{path}/files/share/app-info/xmls/{appid}.xml.gz"
if os.path.exists(appstream_path) and appstream.is_console(appstream_path):
self.errors.discard("appstream-missing-icon-file")

def check_repo(self, path: str) -> None:
self._populate_ref(path)
Expand All @@ -64,7 +82,10 @@ def check_repo(self, path: str) -> None:
flathub_json = ostree.get_flathub_json(path, ref)

with tempfile.TemporaryDirectory() as tmpdir:
ret = ostree.extract_subpath(path, ref, "files/share", 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, flathub_json)
if os.path.exists(appstream_path) and appstream.is_console(appstream_path):
self.errors.discard("appstream-missing-icon-file")
2 changes: 2 additions & 0 deletions tests/builddir/baseapp/metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Application]
name=org.flathub.example.BaseApp
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/builddir/console/metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Application]
name=org.flathub.example.console
8 changes: 8 additions & 0 deletions tests/builddir/extension/metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Runtime]
name=org.flathub.example.extension
runtime=org.example.Platform/x86_64/23.08
sdk=org.example.Sdk/x86_64/23.08

[ExtensionOf]
ref=runtime/org.example.Sdk/x86_64/23.08
runtime=org.example.Platform/x86_64/23.08
34 changes: 34 additions & 0 deletions tests/test_builddir.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,37 @@ def test_builddir_flathub_json() -> None:

assert errors.issubset(found_errors)
assert warnings.issubset(found_warnings)

def test_builddir_baseapp() -> None:
errors = {
"appstream-missing-appinfo-file",
"appstream-metainfo-missing"
}

ret = run_checks("tests/builddir/baseapp")
found_errors = set(ret["errors"])

assert errors == found_errors

def test_builddir_extension() -> None:
errors = {
"appstream-missing-appinfo-file",
"appstream-metainfo-missing",
"finish-args-not-defined"
}

ret = run_checks("tests/builddir/extension")
found_errors = set(ret["errors"])

assert errors == found_errors

def test_builddir_console() -> None:
errors = {
"appstream-metainfo-missing",
"finish-args-not-defined"
}

ret = run_checks("tests/builddir/console")
found_errors = set(ret["errors"])

assert errors == found_errors

0 comments on commit d47ac5c

Please sign in to comment.