Zoeken op website

Verpak PyGObject-applicaties en -programma's als ".deb"-pakket voor de Linux Desktop – Deel 4


We vervolgen de PyGObject programmeerserie met u op de Linux-desktop, in het 4e deel van de serie leggen we uit hoe u de programma's en applicaties kunt verpakken die we hebben gemaakt voor de Linux-desktop met PyGObject als een Debian-pakket.

Debian-pakketten (.deb) zijn het meest gebruikte formaat om programma's onder Linux te installeren, het “dpkg ”-systeem dat zich bezighoudt met .deb-pakketten is de standaard op alle op Debian gebaseerde Linux-distributies zoals Ubuntu en Linux Mint. Daarom leggen we alleen uit hoe we onze programma's voor Debian kunnen verpakken.

Maak een Debian-pakket van uw PyGObject-applicaties

Ten eerste zou u enige basiskennis moeten hebben over het maken van Debian-pakketten; deze volgende gids zal u veel helpen.

  1. Inleiding tot Debian-verpakkingen

Kortom, als u een project heeft met de naam “mijnprogramma ”, moet dit de volgende bestanden en mappen bevatten, zodat u het kunt verpakken.

  1. debian (map): deze map bevat alle informatie over het Debian-pakket, onderverdeeld in vele subbestanden.
  2. po (map): De po-map bevat de vertaalbestanden voor het programma (we leggen dit uit in deel 5).
  3. mijnprogramma (Bestand): Dit is het Python-bestand dat we hebben gemaakt met PyGObject, het is het hoofdbestand van het project.
  4. ui.glade (Bestand): Het grafische gebruikersinterfacebestand. Als u de interface van de applicatie hebt gemaakt met Glade, moet u dit bestand opnemen in
    jouw project.
  5. bMijnprogramma.desktop (Bestand): Dit is het verantwoordelijke bestand voor het weergeven van de applicatie in het applicatiemenu.
  6. setup.py (Bestand): Dit bestand is verantwoordelijk voor het installeren van elk Python-programma op het lokale systeem. Het is erg belangrijk in elk Python-programma, het kan ook op vele andere manieren worden gebruikt.

Natuurlijk. Er zijn nog veel meer bestanden en mappen die u in uw project kunt opnemen (in feite kunt u alles opnemen wat u maar wilt), maar dit zijn de basisbestanden.

Laten we nu beginnen met het verpakken van een project. Maak een nieuwe map met de naam “mijnprogramma”, maak een bestand met de naam “mijnprogramma ” en voeg de volgende code eraan toe.

#!/usr/bin/python 
-*- coding: utf-8 -*- 

## Replace your name and email. 
My Name <[email > 

## Here you must add the license of the file, replace "MyProgram" with your program name. 
License: 
   MyProgram is free software: you can redistribute it and/or modify 
   it under the terms of the GNU General Public License as published by 
   the Free Software Foundation, either version 3 of the License, or 
   (at your option) any later version. 

   MyProgram is distributed in the hope that it will be useful, 
   but WITHOUT ANY WARRANTY; without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
   GNU General Public License for more details. 

   You should have received a copy of the GNU General Public License 
   along with MyProgram.  If not, see <http://www.gnu.org/licenses/>. 

from gi.repository import Gtk 
import os 

class Handler: 
  
  def openterminal(self, button): 
    ## When the user clicks on the first button, the terminal will be opened. 
    os.system("x-terminal-emulator ") 
  
  def closeprogram(self, button): 
    Gtk.main_quit() 
    
Nothing new here.. We just imported the 'ui.glade' file. 
builder = Gtk.Builder() 
builder.add_from_file("/usr/lib/myprogram/ui.glade") 
builder.connect_signals(Handler()) 
window = builder.get_object("window1") 
window.connect("delete-event", Gtk.main_quit) 
window.show_all() 
Gtk.main()

Maak een ui.glade-bestand en vul het met deze code.

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Generated with glade 3.16.1 --> 
<interface> 
  <requires lib="gtk+" version="3.10"/> 
  <object class="GtkWindow" id="window1"> 
    <property name="can_focus">False</property> 
    <property name="title" translatable="yes">My Program</property> 
    <property name="window_position">center</property> 
    <property name="icon_name">applications-utilities</property> 
    <property name="gravity">center</property> 
    <child> 
      <object class="GtkBox" id="box1"> 
        <property name="visible">True</property> 
        <property name="can_focus">False</property> 
        <property name="margin_left">5</property> 
        <property name="margin_right">5</property> 
        <property name="margin_top">5</property> 
        <property name="margin_bottom">5</property> 
        <property name="orientation">vertical</property> 
        <property name="homogeneous">True</property> 
        <child> 
          <object class="GtkLabel" id="label1"> 
            <property name="visible">True</property> 
            <property name="can_focus">False</property> 
            <property name="label" translatable="yes">Welcome to this Test Program !</property> 
          </object> 
          <packing> 
            <property name="expand">False</property> 
            <property name="fill">True</property> 
            <property name="position">0</property> 
          </packing> 
        </child> 
        <child> 
          <object class="GtkButton" id="button2"> 
            <property name="label" translatable="yes">Click on me to open the Terminal</property> 
            <property name="visible">True</property> 
            <property name="can_focus">True</property> 
            <property name="receives_default">True</property> 
            <signal name="clicked" handler="openterminal" swapped="no"/> 
          </object> 
          <packing> 
            <property name="expand">False</property> 
            <property name="fill">True</property> 
            <property name="position">1</property> 
          </packing> 
        </child> 
        <child> 
          <object class="GtkButton" id="button3"> 
            <property name="label">gtk-preferences</property> 
            <property name="visible">True</property> 
            <property name="can_focus">True</property> 
            <property name="receives_default">True</property> 
            <property name="use_stock">True</property> 
          </object> 
          <packing> 
            <property name="expand">False</property> 
            <property name="fill">True</property> 
            <property name="position">2</property> 
          </packing> 
        </child> 
        <child> 
          <object class="GtkButton" id="button4"> 
            <property name="label">gtk-about</property> 
            <property name="visible">True</property> 
            <property name="can_focus">True</property> 
            <property name="receives_default">True</property> 
            <property name="use_stock">True</property> 
          </object> 
          <packing> 
            <property name="expand">False</property> 
            <property name="fill">True</property> 
            <property name="position">3</property> 
          </packing> 
        </child> 
        <child> 
          <object class="GtkButton" id="button1"> 
            <property name="label">gtk-close</property> 
            <property name="visible">True</property> 
            <property name="can_focus">True</property> 
            <property name="receives_default">True</property> 
            <property name="use_stock">True</property> 
            <signal name="clicked" handler="closeprogram" swapped="no"/> 
          </object> 
          <packing> 
            <property name="expand">False</property> 
            <property name="fill">True</property> 
            <property name="position">4</property> 
          </packing> 
        </child> 
      </object> 
    </child> 
  </object> 
</interface>

Tot nu toe is er niets nieuws. We hebben zojuist een Python-bestand en het bijbehorende interfacebestand gemaakt. Maak nu een “setup.py ”-bestand in dezelfde map en voeg de volgende code eraan toe; elke regel wordt uitgelegd in de opmerkingen.

Here we imported the 'setup' module which allows us to install Python scripts to the local system beside performing some other tasks, you can find the documentation here: https://docs.python.org/2/distutils/apiref.html 
from distutils.core import setup 

setup(name = "myprogram", # Name of the program. 
      version = "1.0", # Version of the program. 
      description = "An easy-to-use web interface to create & share pastes easily", # You don't need any help here. 
      author = "TecMint", # Nor here. 
      author_email = "[email ",# Nor here :D 
      url = "http://example.com", # If you have a website for you program.. put it here. 
      license='GPLv3', # The license of the program. 
      scripts=['myprogram'], # This is the name of the main Python script file, in our case it's "myprogram", it's the file that we added under the "myprogram" folder. 

Here you can choose where do you want to install your files on the local system, the "myprogram" file will be automatically installed in its correct place later, so you have only to choose where do you want to install the optional files that you shape with the Python script 
      data_files = [ ("lib/myprogram", ["ui.glade"]), # This is going to install the "ui.glade" file under the /usr/lib/myprogram path. 
                     ("share/applications", ["myprogram.desktop"]) ] ) # And this is going to install the .desktop file under the /usr/share/applications folder, all the folder are automatically installed under the /usr folder in your root partition, you don't need to add "/usr/ to the path. 

Maak nu een “mijnprogramma.desktop ”-bestand in dezelfde map en voeg de volgende code toe, dit wordt ook uitgelegd in de opmerkingen.

This is the .desktop file, this file is the responsible file about showing your application in the applications menu in any desktop interface, it's important to add this file to your project, you can view more details about this file from here: https://developer.gnome.org/integration-guide/stable/desktop-files.html.en 
[Desktop Entry] 
The default name of the program. 
Name=My Program 
The name of the program in the Arabic language, this name will be used to display the application under the applications menu when the default language of the system is Arabic, use the languages codes to change the name for each language. 
Name[ar]=برنامجي 
Description of the file. 
Comment=A simple test program developed by me. 
Description of the file in Arabic. 
Comment[ar]=برنامج تجريبي بسيط تم تطويره بواسطتي. 
The command that's going to be executed when the application is launched from the applications menu, you can enter the name of the Python script or the full path if you want like /usr/bin/myprogram 
Exec=myprogram 
Do you want to run your program from the terminal? 
Terminal=false 
Leave this like that. 
Type=Application 
Enter the name of the icon you want to use for the application, you can enter a path for the icon as well like /usr/share/pixmaps/icon.png but make sure to include the icon.png file in your project folder first and in the setup.py file as well. Here we'll use the "system" icon for now. 
Icon=system 
The category of the file, you can view the available categories from the freedesktop website.
Categories=GNOME;GTK;Utility; 
StartupNotify=false 

We zijn hier nu bijna klaar. We hoeven alleen nog enkele kleine bestanden aan te maken in de map “debian ” om informatie te verstrekken over ons pakket voor de “dpkg ” systeem.

Open de map “debian ” en maak de volgende bestanden aan.

control
compat
changelog
rules

control: dit bestand biedt de basisinformatie over het Debian-pakket. Voor meer details kunt u terecht bij Debian Package Control Fields.

Source: myprogram
Maintainer: My Name <[email > 
Section: utils 
Priority: optional 
Standards-Version: 3.9.2 
Build-Depends: debhelper (>= 9), python2.7 

Package: myprogram 
Architecture: all 
Depends: python-gi 
Description: My Program 
Here you can add a short description about your program.

compat: Dit is slechts een belangrijk bestand voor het dpkg-systeem, het bevat alleen het magische getal 9, laat het zo.

9

changelog: Hier kunt u de wijzigingen toevoegen die u in uw programma aanbrengt. Ga voor meer informatie naar Debian Package Changelog Source.

myprogram (1.0) trusty; urgency=medium 

  * Add the new features here. 
  * Continue adding new changes here. 
  * And here. 

 -- My Name Here <[email >  Sat, 27 Dec 2014 21:36:33 +0200

regels: dit bestand is verantwoordelijk voor het uitvoeren van het installatieproces op de lokale machine om het pakket te installeren. U kunt meer informatie bekijken
over dit bestand vanaf hier: Debian Package Default Rules.

Al heb je niets meer nodig voor je Python-programma.

#!/usr/bin/make -f 
This file is responsible about running the installation process on the local machine to install the package, you can view more information about this file from here: https://www.debian.org/doc/manuals/maint-guide/dreq.en.html#defaultrules Though you won't need anything more for your Python program. 
%: 
    dh $@ 
override_dh_auto_install: 
    python setup.py install --root=debian/myprogram --install-layout=deb --install-scripts=/usr/bin/ # This is going to run the setup.py file to install the program as a Python script on the system, it's also going to install the "myprogram" script under /usr/bin/ using the --install-scripts option, DON'T FORGET TO REPLACE "myprogram" WITH YOUR PROGRAM NAME. 
override_dh_auto_build:

Nu we alle benodigde bestanden voor ons programma met succes hebben gemaakt, laten we nu beginnen met het verpakken ervan. Zorg er eerst voor dat u enkele afhankelijkheden voor het bouwproces hebt geïnstalleerd voordat u begint.

sudo apt-get update
sudo apt-get install devscripts

Stel je nu voor dat de map “mijnprogramma ” zich in uw thuismap bevindt (/home/user/mijnprogramma). Om het te verpakken als een Debian-pakket, voert u de volgende opdrachten uit.

cd /home/user/myprogram
debuild -us -uc
Voorbeelduitvoer
hanny@hanny-HP-Pavilion-15-Notebook-PC:~/Projects/myprogram$
debuild -us -uc dpkg-buildpackage -rfakeroot -D -us -uc
dpkg-buildpackage: source package myprogram
dpkg-buildpackage: source version 1.0
dpkg-buildpackage: source distribution trusty
dpkg-buildpackage: source changed by My Name Here
<[email >
dpkg-source --before-build myprogram
dpkg-buildpackage: host architecture i386
fakeroot debian/rules clean
dh clean
dh_testdir
dh_auto_clean
....
.....
Finished running lintian.

En dat is het ! Uw Debian-pakket is met succes aangemaakt:

Om het op een op Debian gebaseerde distributie te installeren, voert u het programma uit.

sudo dpkg -i myprogram_1.0_all.deb

Vergeet niet het bovenstaande bestand te vervangen door de naam van het pakket. Nadat u het pakket hebt geïnstalleerd, kunt u het programma uitvoeren vanuit het toepassingenmenu.

En het zal werken..

Hier eindigt het 4e deel van onze serie over PyGObject. In de volgende les leggen we uit hoe u de PyGObject-applicatie eenvoudig kunt lokaliseren, tot die tijd kunt u er op letten...