Adding a polkit policy to a python GTK app, what am I missing?

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.

New contributor

Herzen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật