Skip to content

Commit

Permalink
Added support for gpg key import
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Lauridsen committed Apr 19, 2023
1 parent 869c6bb commit 4b5c05c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
23 changes: 22 additions & 1 deletion yumex/backend/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class TransactionResult:
completed: bool
data: dict = field(default_factory=dict)
error: str = ""
key_install: bool = False
key_values: tuple = None


class YumexRootBackend(Client):
Expand All @@ -51,6 +53,7 @@ def __init__(self, presenter) -> None:
self.presenter = presenter
self.dnl_frac = 0.0
self._locked = False
self.gpg_confirm = None

@property
def progress(self) -> YumexProgress:
Expand All @@ -64,6 +67,7 @@ def __enter__(self) -> Self:
def __exit__(self, exc_type, exc_value, exc_traceback) -> None:
if self._locked:
self.unlock()
self.Exit()

def on_TransactionEvent(self, event, data) -> None:
# Do your stuff here
Expand Down Expand Up @@ -111,7 +115,9 @@ def on_RPMProgress(

def on_GPGImport(self, pkg_id, userid, hexkeyid, keyurl, timestamp) -> None:
# TODO: Handle GPG key inport verification
pass
values = (pkg_id, userid, hexkeyid, keyurl, timestamp)
self.gpg_confirm = values
log(f"received signal : GPGImport {repr(values)}")

def on_DownloadStart(self, num_files, num_bytes) -> None:
"""Starting a new parallel download batch"""
Expand Down Expand Up @@ -174,6 +180,7 @@ def build_transaction(self, pkgs: list[YumexPackage]) -> TransactionResult:
error=_("Couldn't build transaction\n") + "\n".join(result.data),
)
except DaemonError as e:
log(" --> RootBackendError : " + str(e))
return TransactionResult(
False, error=_("Exception in Dnf Backend\n") + str(e)
)
Expand All @@ -193,13 +200,27 @@ def run_transaction(self) -> TransactionResult:
rc, msgs = self.RunTransaction()
if rc == 0:
return TransactionResult(True, data={"msgs": msgs})
elif rc == 1: # gpg keys need to be confirmed
return TransactionResult(
False, key_install=True, key_values=self.gpg_confirm
)
else:
return TransactionResult(False, error="\n".join(msgs))
except DaemonError as e:
return TransactionResult(
False, error=_("Exception in Dnf Backend : ") + str(e)
)

def do_gpg_import(self):
(
pkg_id,
userid,
hexkeyid,
keyurl,
timestamp,
) = self.gpg_confirm
self.ConfirmGPGImport(hexkeyid, True)

@staticmethod
def id_to_nevra(id) -> tuple[str, str]:
n, e, v, r, a, repo = id.split(",")
Expand Down
38 changes: 37 additions & 1 deletion yumex/ui/dialogs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gi.repository import Adw
from gi.repository import Adw, GLib

from yumex.utils.types import MainWindow

Expand All @@ -17,3 +17,39 @@ def response(dialog, result, *args):
dialog.set_close_response("quit")
dialog.connect("response", response)
dialog.present()


class GPGDialog(Adw.MessageDialog):
def __init__(self, win: MainWindow, key_values: tuple):
super().__init__()
(
pkg_id,
userid,
hexkeyid,
keyurl,
timestamp,
) = key_values
self.set_heading(_("Install GPG Key"))
body = f"""
{userid}
{pkg_id}
"""
self.set_body(body)
self.install_key = False
self._loop = GLib.MainLoop()
self.add_response("yes", _("Yes"))
self.set_response_appearance("yes", Adw.ResponseAppearance.DESTRUCTIVE)
self.add_response("no", _("No"))
self.set_default_response("no")
self.set_close_response("no")
self.connect("response", self.response)

def response(self, dialog, result, *args):
self.install_key = result == "yes"
self._loop.quit()
self.close()

def show(self):
self.present()
self._loop.run()
42 changes: 37 additions & 5 deletions yumex/ui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from yumex.backend.dnf import YumexPackage
from yumex.backend.presenter import YumexPresenter
from yumex.constants import ROOTDIR, APP_ID, PACKAGE_COLUMNS
from yumex.ui.dialogs import GPGDialog
from yumex.ui.flatpak_result import YumexFlatpakResult
from yumex.ui.flatpak_view import YumexFlatpakView
from yumex.ui.pachage_view import YumexPackageView
Expand Down Expand Up @@ -153,6 +154,7 @@ def show_message(self, title, timeout=2):
toast = Adw.Toast(title=title)
toast.set_timeout(timeout)
self.toast_overlay.add_toast(toast)
log(f"show_message : {title}")
return toast

def load_packages(self, pkg_filter: PackageFilter):
Expand All @@ -179,11 +181,29 @@ def _do_transaction(self, queued):
transaction_result.show()
if transaction_result.confirm:
# run the transaction
self.progress.show()
self.progress.set_title(_("Running Transaction"))
result: TransactionResult = root_backend.run_transaction()
if result.completed:
return True
while True:
self.progress.show()
self.progress.set_title(_("Running Transaction"))
result: TransactionResult = root_backend.run_transaction()
if result.completed:
return True
if result.key_install and result.key_values:
self.progress.hide()
dialog = GPGDialog(self, result.key_values)
dialog.set_transient_for(self)
dialog.show()
log(f"Install key: {dialog.install_key}")
if dialog.install_key:
log("Re-run transaction and import GPG keys")
# tell the backend to import this gpg key in next run
root_backend.do_gpg_import()
# rebuild the transaction again, before re-run
root_backend.build_transaction(queued)
continue
else:
return True
else:
break
if result.error:
self.show_message(result.error)
return False
Expand Down Expand Up @@ -248,6 +268,18 @@ def on_package_selection_changed(self, widget, pkg: YumexPackage):

def on_testing(self, *args):
"""Used to test gui stuff <Shift><Ctrl>T to activate"""
values = (
"rpmfusion-nonfree-release,0,38,1,noarch,rpmfusion-nonfree",
"RPM Fusion nonfree repository for Fedora (2020) <[email protected]>",
"94843C65",
"file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmfusion-nonfree-fedora-38",
1571667044,
)
dialog = GPGDialog(self, values)
dialog.set_transient_for(self)
dialog.show()
if dialog.install_key:
log("Re-run transaction and import GPG keys")
pass

def on_apply_actions_clicked(self, *_args):
Expand Down

0 comments on commit 4b5c05c

Please sign in to comment.