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

feat: enable both user and system flatpak installs #82

Merged
merged 3 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
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
68 changes: 68 additions & 0 deletions tests/example-system.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
title: uBlue First Boot
properties:
mode: "run-on-change"
actions:
pre:
- run: /full/path/to/bin --with --params
- run: /another/command run
- yafti.plugin.flatpak:
install: org.gnome.Calculator
post:
- run: /run/these/commands --after --all --screens
screens:
first-screen:
source: yafti.screen.title
values:
title: "That was pretty cool"
icon: "/path/to/icon"
description: |
Time to play overwatch
can-we-modify-your-flatpaks:
source: yafti.screen.consent
values:
title: Welcome traveler
condition:
run: flatpak remotes --system | grep fedora
description: |
This tool modifies your flatpaks and flatpak sources. If you do not want to do this exit the installer.
For new users just do it (tm)
actions:
- run: flatpak remote-delete fedora --force
- run: flatpak remove --system --noninteractive --all
applications:
source: yafti.screen.package
values:
title: Package Installation
show_terminal: true
package_manager: yafti.plugin.flatpak
package_manager_defaults:
user: false
system: true
groups:
Core:
description: All the good stuff
packages:
- Calculator: org.gnome.Calculator
- Firefox:
package: org.mozilla.firefox
system: false
user: true
Gaming:
description: GAMES GAMES GAMES
default: false
packages:
- Steam: com.valvesoftware.Steam
- Games: org.gnome.Games
Office:
description: All the work stuff
default: false
packages:
- LibreOffice: org.libreoffice.LibreOffice
- Calendar: org.gnome.Calendar
final-screen:
source: yafti.screen.title
values:
title: "All done"
icon: "/atph/to/icon"
description: |
Thanks for installing, join the community, next steps
10 changes: 8 additions & 2 deletions tests/test_screen_package_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ def test_parse_packages_groups():
"description": "hello world",
"packages": [
{"Calculator": "org.gnome.Calculator"},
{"Firefox": "org.mozilla.firefox"},
{
"Firefox": {
"package": "org.mozilla.firefox",
"system": True,
"user": False,
},
},
],
},
"Gaming": {
Expand All @@ -23,7 +29,7 @@ def test_parse_packages_groups():
expected = {
"group:Core": True,
"pkg:org.gnome.Calculator": True,
"pkg:org.mozilla.firefox": True,
'pkg:{"package": "org.mozilla.firefox", "system": true, "user": false}': True,
"group:Gaming": True,
"pkg:com.valvesoftware.Steam": True,
"pkg:org.gnome.Games": True,
Expand Down
14 changes: 7 additions & 7 deletions yafti/plugin/flatpak.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _parse_args(self, **kwargs):

async def install(
self,
pkg: str,
package: str,
user: bool = True,
system: bool = False,
assumeyes: bool = True,
Expand All @@ -161,7 +161,7 @@ async def install(
"""Install flatpak package on the host system

Args:
pkg: Name of the flatpak package to install
package: Name of the flatpak package to install
user: Install on the user installation
system: Install on the system-wide installation
assumeyes:
Expand All @@ -182,12 +182,12 @@ async def install(
)
cmd = [self.bin, "install"]
cmd.extend(args)
cmd.append(pkg)
cmd.append(package)
return await self.exec(" ".join(cmd))

async def remove(
self,
pkg: str,
package: str,
user: bool = False,
system: bool = True,
force: bool = False,
Expand All @@ -199,7 +199,7 @@ async def remove(
)
cmd = [self.bin, "remove"]
cmd.extend(args)
cmd.append(pkg)
cmd.append(package)
return self.exec(cmd)

def ls(self) -> list[ApplicationDetail]:
Expand All @@ -214,10 +214,10 @@ def __call__(self, options) -> YaftiPluginReturn:
# TODO: when a string is passed, make sure it maps to the "pkg" key.
if params.install:
if isinstance(params.install, str):
params.install = {"pkg": params.install}
params.install = {"package": params.install}
r = asyncio.ensure_future(self.install(**params.install))
else:
if isinstance(params.remove, str):
params.remove = {"pkg": params.remove}
params.remove = {"package": params.remove}
r = asyncio.ensure_future(self.remove(**params.install))
return YaftiPluginReturn(output=r.stdout, errors=r.stderr, code=r.returncode)
2 changes: 1 addition & 1 deletion yafti/screen/package/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class PackageConfig(BaseModel):
__root__: dict[str, str]
__root__: dict[str, str | dict]


class PackageGroupConfigDetails(BaseModel):
Expand Down
29 changes: 22 additions & 7 deletions yafti/screen/package/screen/install.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import asyncio
import json

from gi.repository import Gtk
from typing import Optional

import yafti.share
from yafti import events
from yafti.abc import YaftiScreen, YaftiScreenConfig
from yafti import log
from yafti.abc import YaftiScreen
from yafti.screen.console import ConsoleScreen
from yafti.screen.package.state import STATE

Expand Down Expand Up @@ -70,19 +74,18 @@ class PackageInstallScreen(YaftiScreen, Gtk.Box):
already_run = False
pulse = True

class Config(YaftiScreenConfig):
package_manager: str = "yafti.plugin.flatpak"

def __init__(
self,
title: str = "Package Installation",
package_manager: str = "yafti.plugin.flatpak",
package_manager_defaults: Optional[dict] = None,
**kwargs,
):
super().__init__(**kwargs)
from yafti.registry import PLUGINS

self.package_manager = PLUGINS.get(package_manager)
self.package_manager_defaults = package_manager_defaults or {}
self.btn_console.connect("clicked", self.toggle_console)

async def on_activate(self):
Expand Down Expand Up @@ -113,14 +116,26 @@ def draw(self):
asyncio.create_task(self.do_pulse())
return self.install(packages)

def run_package_manager(self, packge_config):
try:
config = json.loads(packge_config)
except json.decoder.JSONDecodeError as e:
log.debug("could not parse", config=packge_config, e=e)
config = {"package": packge_config}

log.debug("parsed packages config", config=config)
opts = self.package_manager_defaults.copy()
opts.update(config)
return self.package_manager.install(**opts)

async def install(self, packages: list):
total = len(packages)
yafti.share.BTN_NEXT.set_label("Installing...")
yafti.share.BTN_BACK.set_visible(False)
for idx, pkg in enumerate(packages):
r = await self.package_manager.install(pkg)
self.console.stdout(r.stdout)
self.console.stderr(r.stderr)
results = await self.run_package_manager(pkg)
self.console.stdout(results.stdout)
self.console.stderr(results.stderr)
self.pulse = False
self.pkg_progress.set_fraction((idx + 1) / total)

Expand Down
9 changes: 8 additions & 1 deletion yafti/screen/package/screen/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Config(YaftiScreenConfig):
package_manager: str
groups: Optional[PackageGroupConfig] = None
packages: Optional[list[PackageConfig]] = None
package_manager_defaults: Optional[dict] = None

def __init__(
self,
Expand All @@ -51,13 +52,15 @@ def __init__(
packages: list[PackageConfig] = None,
groups: PackageGroupConfig = None,
show_terminal: bool = True,
package_manager_defaults: Optional[dict] = None,
**kwargs,
):
super().__init__(**kwargs)
self.title = title
self.packages = groups or packages
self.show_terminal = show_terminal
self.package_manager = package_manager
self.package_manager_defaults = package_manager_defaults
STATE.load(parse_packages(self.packages))
self.pkg_carousel.connect("page-changed", self.changed)
self.draw()
Expand All @@ -67,7 +70,11 @@ def draw(self):
PackagePickerScreen(title=self.title, packages=self.packages)
)
self.pkg_carousel.append(
PackageInstallScreen(title=self.title, package_manager=self.package_manager)
PackageInstallScreen(
title=self.title,
package_manager=self.package_manager,
package_manager_defaults=self.package_manager_defaults,
)
)

def on_activate(self):
Expand Down
5 changes: 5 additions & 0 deletions yafti/screen/package/screen/picker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import partial
import json

from gi.repository import Adw, Gtk
from pydantic import BaseModel
Expand Down Expand Up @@ -79,6 +80,8 @@ def state_set(group, _, value):
d = self.packages.get(group)
for pkg in d.get("packages", []):
for pkg_name in pkg.values():
if isinstance(pkg_name, dict):
pkg_name = json.dumps(pkg_name)
STATE.set(f"pkg:{pkg_name}", value)

state_set(name, None, details.get("default", True))
Expand Down Expand Up @@ -143,6 +146,8 @@ def _build_apps(self, packages: list):
title=name,
)
_app_switcher = Gtk.Switch()
if isinstance(pkg, dict):
pkg = json.dumps(pkg)
_app_switcher.set_active(STATE.get(f"pkg:{pkg}"))
_app_switcher.set_valign(Gtk.Align.CENTER)

Expand Down
8 changes: 7 additions & 1 deletion yafti/screen/package/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import json


def parse_packages(packages: dict | list) -> dict:
output = {}

Expand All @@ -8,5 +11,8 @@ def parse_packages(packages: dict | list) -> dict:
return output

for pkgcfg in packages:
output.update({f"pkg:{package}": True for package in pkgcfg.values()})
for package in pkgcfg.values():
if isinstance(package, dict):
package = json.dumps(package)
output[f"pkg:{package}"] = True
return output
4 changes: 2 additions & 2 deletions yafti/screen/title.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import asyncio
from functools import partial
from typing import Optional, List
from typing import List, Optional

from gi.repository import Adw, Gtk

from yafti.abc import YaftiScreen, YaftiScreenConfig
from yafti import events
from yafti.abc import YaftiScreen, YaftiScreenConfig
from yafti.registry import PLUGINS

_xml = """\
Expand Down