Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add human-readable names & summaries to Flatpak manifest #125

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 96 additions & 87 deletions hooks/image/70-flatpak-manifest
Original file line number Diff line number Diff line change
Expand Up @@ -34,64 +34,22 @@ def write_manifest(data):
json.dump(data, manifest)


# Build the json data structure
data = {
'flatpak': {
'remotes': {},
'runtimes': {},
'apps': {},
}
}

if os.environ.get('EIB_FLATPAK_ENABLE', 'false') != 'true':
# Write out an empty manifest so it always exists
write_manifest(data)
exit(0)

# Open the flatpak installation in the OS /var/lib/flatpak.
system_path = os.path.join(os.environ['OSTREE_VAR'], 'lib/flatpak')
print('Opening flatpak installation in', system_path)
system_file = Gio.File.new_for_path(system_path)
system = Flatpak.Installation.new_for_path(system_file, user=False)

repo_file = system_file.get_child('repo')
print('Opening ostree repo in', repo_file.get_path())
repo = OSTree.Repo.new(repo_file)
repo.open()

remotes = system.list_remotes()
for remote in remotes:
name = remote.get_name()
url = remote.get_url()
collection_id = remote.get_collection_id()

# Skip disabled remotes
if remote.get_disabled():
continue

# Skip local remotes (e.g., external apps)
if url.startswith('file://'):
continue

data['flatpak']['remotes'][name] = {
'url': url,
'collection-id': collection_id,
}

runtimes = system.list_installed_refs_by_kind(Flatpak.RefKind.RUNTIME)
for runtime in runtimes:
name = runtime.get_name()
arch = runtime.get_arch()
branch = runtime.get_branch()
def get_data_for_ref(
repo: OSTree.Repo,
installed_ref: Flatpak.InstalledRef,
) -> dict:
name = installed_ref.get_name()
arch = installed_ref.get_arch()
branch = installed_ref.get_branch()
key = '{}/{}/{}'.format(name, arch, branch)
remote = runtime.get_origin()
commit = runtime.get_commit()
size = runtime.get_installed_size()
ref = runtime.format_ref()
remote = installed_ref.get_origin()
commit = installed_ref.get_commit()
size = installed_ref.get_installed_size()
ref = installed_ref.format_ref()
date = commit_date_string(repo, commit)
version = runtime.get_appdata_version()
version = installed_ref.get_appdata_version()

data['flatpak']['runtimes'][key] = {
data = {
'name': name,
'arch': arch,
'branch': branch,
Expand All @@ -103,37 +61,88 @@ for runtime in runtimes:
'version': version,
}

apps = system.list_installed_refs_by_kind(Flatpak.RefKind.APP)
for app in apps:
name = app.get_name()
arch = app.get_arch()
branch = app.get_branch()
key = '{}/{}/{}'.format(name, arch, branch)
remote = app.get_origin()
commit = app.get_commit()
size = app.get_installed_size()
ref = app.format_ref()
date = commit_date_string(repo, commit)
version = app.get_appdata_version()
metadata = GLib.KeyFile()
metadata.load_from_bytes(app.load_metadata(), GLib.KeyFileFlags.NONE)
try:
runtime = metadata.get_string('Application', 'runtime')
except GLib.GError:
runtime = None

data['flatpak']['apps'][key] = {
'name': name,
'arch': arch,
'branch': branch,
'remote': remote,
'commit': commit,
'size': size,
'ref': ref,
'date': date,
'version': version,
'runtime': runtime,
# TODO: It would be nice to consider EIB_FLATPAK_LOCALES and extract the
# name and summary in those locales too. Unfortunately while there is
# internal API to do this easily, it is not exposed by libflatpak.
appdata_name = installed_ref.get_appdata_name()
appdata_summary = installed_ref.get_appdata_summary()
if appdata_name or appdata_summary:
data['appdata'] = {'C': {}}
if appdata_name:
data['appdata']['C']['name'] = appdata_name
if appdata_summary:
data['appdata']['C']['summary'] = appdata_summary

return key, data


def main():
# Build the json data structure
data = {
'flatpak': {
'remotes': {},
'runtimes': {},
'apps': {},
}
}

# Now write out the json to a fragment
write_manifest(data)
if os.environ.get('EIB_FLATPAK_ENABLE', 'false') != 'true':
# Write out an empty manifest so it always exists
write_manifest(data)
return

# Open the flatpak installation in the OS /var/lib/flatpak.
system_path = os.path.join(os.environ['OSTREE_VAR'], 'lib/flatpak')
print('Opening flatpak installation in', system_path)
system_file = Gio.File.new_for_path(system_path)
system = Flatpak.Installation.new_for_path(system_file, user=False)

repo_file = system_file.get_child('repo')
print('Opening ostree repo in', repo_file.get_path())
repo = OSTree.Repo.new(repo_file)
repo.open()

remotes = system.list_remotes()
for remote in remotes:
name = remote.get_name()
url = remote.get_url()
collection_id = remote.get_collection_id()

# Skip disabled remotes
if remote.get_disabled():
continue

# Skip local remotes (e.g., external apps)
if url.startswith('file://'):
continue

data['flatpak']['remotes'][name] = {
'url': url,
'collection-id': collection_id,
}

runtimes = system.list_installed_refs_by_kind(Flatpak.RefKind.RUNTIME)
for runtime in runtimes:
key, runtime_data = get_data_for_ref(repo, runtime)
data['flatpak']['runtimes'][key] = runtime_data

apps = system.list_installed_refs_by_kind(Flatpak.RefKind.APP)
for app in apps:
key, app_data = get_data_for_ref(repo, app)

metadata = GLib.KeyFile()
metadata.load_from_bytes(app.load_metadata(), GLib.KeyFileFlags.NONE)
try:
runtime = metadata.get_string('Application', 'runtime')
except GLib.GError:
runtime = None
app_data['runtime'] = runtime

data['flatpak']['apps'][key] = app_data

# Now write out the json to a fragment
write_manifest(data)


if __name__ == '__main__':
main()
Loading