Reputation: 11
I'm currently making my first python+GTK4 and I need to use some pkexec command for having root permission for stuffs like dnf install, dnf list, ecc.
I'm posting because I'm trying from a few days to make the user insert the admin password just one time instead to have to do that for every command. So I maked a test python app trying to achieve it. My goal is to have a smooth and simple user experience like Gparted or Input Remapper.
I created my Polkit policy file and I place it the right place /usr/share/polkit-1/actions/ and the policy is the following with the name "com.prova.setup.app.policy" :
`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd"
>
<policyconfig>
<action id="com.prova.setup.app">
<description>Run Input Remapper as root</description>
<message>SE MI LEGGI VUOL DIRE CHE FUNZIONA !</message>
<defaults>
<allow_any>no</allow_any>
<allow_inactive>auth_admin_keep</allow_inactive>
<allow_active>auth_admin_keep</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/python</annotate>
<annotate key="org.freedesktop.policykit.exec.argv1">/usr/bin/prova-setup</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
</policyconfig>`
This is the spec file I made to build it as an RPM to install on the Fedora system (the true program will only be for Fedora) is the following :
Name: prova-setup
Version: 0.1
Release: 1%{?dist}
Summary: A simple Python project
License: MIT
URL: https://example.com/simple_project
Source0: %{name}-%{version}.tar.gz
BuildArch: noarch
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: gtk4-devel
BuildRequires: libadwaita-devel
Requires: python3
Requires: gtk4
Requires: libadwaita
%description
This is a simple Python project to demonstrate RPM packaging.
%prep
%setup
%build
%py3_build
%install
%py3_install
install -Dm644 desktop/prova-setup.desktop %{buildroot}%{_datadir}/applications/prova-setup.desktop
install -Dm644 desktop/prova-setup.svg %{buildroot}%{_datadir}/icons/hicolor/scalable/apps/prova-setup.svg
install -Dm644 desktop/prova-setup.policy %{buildroot}%{_datadir}/polkit-1/actions/prova-setup.policy
mkdir -p %{buildroot}%{_datadir}/prova-setup
cp -r data/* %{buildroot}%{_datadir}/prova-setup/
%files
%doc README.md
%{_bindir}/prova-setup
%{python3_sitelib}/prova_setup*
%{_datadir}/applications/prova-setup.desktop
%{_datadir}/icons/hicolor/scalable/apps/prova-setup.svg
%{_datadir}/polkit-1/actions/prova-setup.policy
%{_datadir}/prova-setup/*
%post
update-desktop-database &> /dev/null || :
%changelog
* Thu Jul 01 2024 Developer <[email protected]> - 0.1-1
- Initial package
The dummy python app I wrote to learn how polkit and pkexec work is the following :
# IMPORTAZIONE DEI MODULI
import sys, gi, subprocess, time, os
# RICHIESTA DELLE VERSIONI DI GTK ED ADWAITA
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
# IMPORTO I MODULI GTK E ADWAITA
from gi.repository import Gtk, Adw, Gdk
print("sto funzionando")
# DEFINISCO LA SOTTO-CLASSE CHE RAPPRESENTA LA FINESTRA PRINCIPALE
class contenuto_finestra_principale(Gtk.ApplicationWindow):
# IMPORTO GLI ATTRIBUTI E METODI DELLA CLASSE MADRE GTK.APPLICATIONWINDOW
# UTILIZZANDO LA FUNZIONE INIT E LA SUPERCLASSE
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# DEFINISCO LE DIMENSIONI DELLA FINESTRA ALL'AVVIO
self.set_default_size(800,600)
# DEFINISCO IL TITOLO DELLA FINESTRA
self.set_title("Primo programma in GTK")
# CREO IL BOX PRINCIPALE ALL'INTERNO DELLA FINESTRA
# EGUAGLIANDO UN ATTRIBUTO DELLA CLASSE ALLA FUNZIONE CHE
# GENERA IL WIDGET NELLA LIBRERIA GTK
self.box_principale = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
# ASSEGNO IL BOX PRINCIPALE ALLA FINESTRA
self.set_child(self.box_principale)
# CREO IL PRIMO BOX INTERNO AL BOX PRINCIPALE
self.box_1 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
# ASSEGNO IL BOX_ ALLA FINESTRA
self.box_principale.append(self.box_1)
# CREO IL SECONDO BOX INTERNO AL BOX PRINCIPALE
self.box_2 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
# ASSEGNO IL BOX_2 ALLA BOX PRINCIPALE
self.box_principale.append(self.box_2)
# CREO IL BOTTONE 2
self.bottone_principale = Gtk.Button(label="CLICCAMI")
# ASSEGNO IL BOTTONE PRINCIPALE AL BOX 1
self.box_1.append(self.bottone_principale)
# ASSEGNO UNA FUNZIONE AL BOTTONE
self.bottone_principale.connect('clicked', self.funzione_bottone_principale)
# CREO IL CONTENITORE DELL'INTERRUTTORE DI PROVA
self.box_interruttore_prova = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
self.box_interruttore_prova.set_spacing(5)
# ASSEGNO IL BOX INTERRUTTORE DI PROVA AL BOX PRINCIPALE
self.box_2.append(self.box_interruttore_prova)
# CREO UN INTERRUTTORE DI PROVA
self.interuttore_prova = Gtk.Switch()
# ASSEGNO L'INTERRUTTORE AL BOX INTERRUTTORE DI PROVA
self.box_interruttore_prova.append(self.interuttore_prova)
# CREO LA TARGHETTA DI TESTO DELL'INTERRUTTORE DI PROVA
self.tag_interruttore_prova = Gtk.Label(label="Un interruttore")
# ASSEGNO LA TARGHETTA DI TESTO AL BOX DELL'INTERRUTTORE DI PROVA
self.box_interruttore_prova.append(self.tag_interruttore_prova)
# DEFINISCO LA FUNZIONE DEL BOTTONE PRINCIPALE
def funzione_bottone_principale(self, bottone_principale):
test()
# DEFINISCO LA SOTTO-CLASSE CHE PERMETTE LA CREAZIONE E MESSA SCHERMO DELLA FINESTRA PRINCIPALE
class genera_finestra_principale(Adw.Application):
# IMPORTO GLI ATTRIBUTI E METODI DELLA CLASSE MADRE ADW.APPLICATION
# UTILIZZANDO LA FUNZIONE INIT E LA SUPERCLASSE
def __init__(self, **kwargs):
super().__init__(**kwargs)
# VIENE RICHIAMATA LA FUNZIONE "CONNECT" UTILIZZANDO COME PARAMETRI "ACTIVATE"
# E LA FUNZIONE DI GENERAZIONE DELLA FINESTRA PRINCIPALE
self.connect('activate', self.attivazione_finestra_principale)
# DEFINISCO LA FUNZIONE / METODO DELLA CLASSE PER GENERARE LA FINESTRA PRINCIPALE
def attivazione_finestra_principale(self, finestra):
# CREAZIONE DELLA FINESTRA PRINCIPALE
self.finestra_principale = contenuto_finestra_principale(application=finestra)
# MANDO A SCHERMO LA FINESTRA CON LA FUNZIONE "PRESENT"
self.finestra_principale.present()
def test():
# LETTURA DELLA LISTA DELLE LIBRERIE INSTALLATE
# READING INSTALLED LIBRARY LIST
library_list_output = subprocess.Popen("pkexec dnf list installed | grep lib", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
library_list, library_list_err = library_list_output.communicate()
print(library_list)
update_list_output = subprocess.Popen(f"pkexec dnf update", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
update_list, update_list_err =update_list_output.communicate()
print(update_list)
def main():
# DEFINIZIONE DELL'ID DEL PROGRAMMA E DELLA VARIABILE CHE RAPPRESENTA LA FINESTRA DEL PROGRAMMA
finestra = genera_finestra_principale(application_id="com.prova.setup.app")
# AVVIO DELLA FINESTRA PRINCIPALE
finestra.run(sys.argv)
input("Premi un tasto qualunque per uscire ")
if __name__ == "__main__":
main()
input("Premi un tasto qualunque per uscire ")
The setup.py file is the following :
from setuptools import setup, find_packages
setup(
name="prova-setup",
version="0.1",
packages=['prova_setup'],
entry_points={
"console_scripts": [
"prova-setup=prova_setup.main:main",
],
},
)
**And here we go with the last piece, the .deskop file : **
[Desktop Entry]
Type=Application
Version=0.1
Name=Prova di Setup.py
Comment=Una semplice ed umile descrizione
Exec=prova-setup
Icon=prova-setup.svg
Terminal=true
Type=Application
Categories=Utility;
Please help figure out what I'am doing wrong, this is the only way to finish my first (FOSS) app. Thank you all for reading and sorry for the bad english but I'm not a native speaker. :)
I've tried to run it as cli, run it with GTK GUI, change names, GTK ID, and polkit action id name. also tried everything I found online.
Upvotes: 1
Views: 178
Reputation: 321
This is how I solved the problem for my app---
1) Make changes to the .policy file--
`
<policyconfig>
<vendor>[email protected]</vendor>
<vendor_url>[email protected]</vendor_url>
<action id="com.prova.setup.app">
<description>com.prova.setup.app for linux</description>
<description xml:lang="da">com.prova.setup.app for linux</description>
<message>Authentication is required to run prova.setup</message>
<message xml:lang="da">Authentication is required to run prova.setup</message>
<icon_name>prova.setup</icon_name>
<defaults>
<allow_any>auth_admin</allow_any>
<allow_inactive>auth_admin</allow_inactive>
<allow_active>auth_admin</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/usr/share/YourAppFolder/ExecutableSetupFile</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
</action>
</policyconfig>
`
2)Desktop file--- `
[Desktop Entry]
Type=Application
Name=AppName
Comment=Multi Language voice Clock app from Dibyendu Jana
Icon=/usr/share/YourAppFolder/icons/desktop.png
Exec=/usr/bin/launcherScript
Terminal=false
Categories=Utility;
Name[en_IN]=YourAppName
`
3)launcher Script (location /usr/bin/launcherScript, it will run by .Desktop file and will launch the ExecutableSetupFile file)---
`
#!/bin/bash
NONROOT=$(whoami)
pkexec --user root /usr/share/YourAppFolder/ExecutableSetupFile $NONROOT "$@"
`
Upvotes: -1