Πακέτο PyGObject Εφαρμογές και Προγράμματα ως Πακέτο ".deb" για την επιφάνεια εργασίας Linux – Μέρος 4


Συνεχίζουμε τη σειρά προγραμματισμού PyGObject μαζί σας στον επιτραπέζιο υπολογιστή Linux, στο 4ο μέρος της σειράς θα εξηγήσουμε πώς να πακετάρουμε τα προγράμματα και τις εφαρμογές που δημιουργήσαμε για το Επιτραπέζιος υπολογιστής Linux που χρησιμοποιεί το PyGObject ως πακέτο Debian.

Τα πακέτα Debian (.deb) είναι η πιο χρησιμοποιούμενη μορφή για την εγκατάσταση προγραμμάτων στο Linux, το σύστημα «dpkg» που ασχολείται με πακέτα .deb είναι η προεπιλογή σε όλες τις διανομές Linux που βασίζονται στο Debian, όπως το Ubuntu και το Linux Mint. Γι' αυτό θα εξηγήσουμε μόνο πώς να πακετάρουμε τα προγράμματά μας για το Debian.

Δημιουργήστε ένα πακέτο Debian από τις εφαρμογές σας PyGObject

Αρχικά, θα πρέπει να έχετε κάποιες βασικές γνώσεις σχετικά με τη δημιουργία πακέτων Debian, αυτός ο παρακάτω οδηγός θα σας βοηθήσει πολύ.

  1. Εισαγωγή στη συσκευασία Debian

Εν συντομία, εάν έχετε έργο που ονομάζεται "myprogram " θα πρέπει να περιέχει τα ακόλουθα αρχεία και φακέλους, ώστε να μπορείτε να το πακετάρετε.

  1. debian (Φάκελος): Αυτός ο φάκελος περιλαμβάνει όλες τις πληροφορίες σχετικά με το πακέτο Debian χωρισμένες σε πολλά υπο-αρχεία.
  2. po (Φάκελος): Ο φάκελος po περιλαμβάνει τα αρχεία μετάφρασης για το πρόγραμμα (Θα το εξηγήσουμε στο μέρος 5).
  3. myprogram (Αρχείο): Αυτό είναι το αρχείο Python που δημιουργήσαμε χρησιμοποιώντας το PyGObject, είναι το κύριο αρχείο του έργου.
  4. ui.glade (Αρχείο): Το αρχείο γραφικής διεπαφής χρήστη.. Εάν δημιουργήσατε τη διεπαφή της εφαρμογής χρησιμοποιώντας το Glade, πρέπει να συμπεριλάβετε αυτό το αρχείο στο
    το έργο σας.
  5. bMyprogram.desktop (Αρχείο): Αυτό είναι το υπεύθυνο αρχείο για την εμφάνιση της εφαρμογής στο μενού εφαρμογών.
  6. setup.py (Αρχείο): Αυτό το αρχείο είναι υπεύθυνο για την εγκατάσταση οποιουδήποτε προγράμματος Python στο τοπικό σύστημα, είναι πολύ σημαντικό σε οποιοδήποτε πρόγραμμα Python, έχει και πολλούς άλλους τρόπους χρήσης.

Φυσικά.. Υπάρχουν πολλά άλλα αρχεία και φάκελοι που μπορείτε να συμπεριλάβετε στο έργο σας (στην πραγματικότητα μπορείτε να συμπεριλάβετε ό,τι θέλετε) αλλά αυτά είναι τα βασικά.

Τώρα, ας αρχίσουμε να πακετάρουμε ένα έργο. Δημιουργήστε έναν νέο φάκελο με το όνομα “myprogram ”, δημιουργήστε ένα αρχείο με το όνομα “myprogram ” και προσθέστε τον ακόλουθο κώδικα σε αυτόν.

#!/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()

Δημιουργήστε ένα αρχείο ui.glade και συμπληρώστε το με αυτόν τον κωδικό.

<?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>

Δεν υπάρχει τίποτα νέο μέχρι τώρα.. Μόλις δημιουργήσαμε ένα αρχείο Python και το αρχείο διεπαφής του. Τώρα δημιουργήστε ένα αρχείο "setup.py " στον ίδιο φάκελο και προσθέστε τον ακόλουθο κώδικα σε αυτό, κάθε γραμμή εξηγείται στα σχόλια.

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. 

Τώρα δημιουργήστε ένα αρχείο "myprogram.desktop " στον ίδιο φάκελο και προσθέστε τον ακόλουθο κώδικα, όπως εξηγείται και στα σχόλια.

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 

Τελειώσαμε σχεδόν εδώ τώρα. Απλώς πρέπει να δημιουργήσουμε μερικά μικρά αρχεία στον φάκελο "debian " για να παρέχουμε πληροφορίες σχετικά με το πακέτο μας για το "dpkg " Σύστημα.

Ανοίξτε το φάκελο "debian " και δημιουργήστε τα ακόλουθα αρχεία.

control
compat
changelog
rules

control: Αυτό το αρχείο παρέχει τις βασικές πληροφορίες σχετικά με το πακέτο Debian, για περισσότερες λεπτομέρειες, επισκεφτείτε τα Πεδία ελέγχου πακέτων Debian.

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: Αυτό είναι απλώς ένα σημαντικό αρχείο για το σύστημα dpkg, περιλαμβάνει απλώς τον μαγικό αριθμό 9, αφήστε το έτσι.

9

changelog: Εδώ θα μπορείτε να προσθέσετε τις αλλαγές που κάνετε στο πρόγραμμά σας. Για περισσότερες πληροφορίες, επισκεφτείτε την Πηγή καταγραφής αλλαγών του Debian Package.

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

κανόνες: Αυτό το αρχείο είναι υπεύθυνο για την εκτέλεση της διαδικασίας εγκατάστασης στον τοπικό υπολογιστή για την εγκατάσταση του πακέτου, μπορείτε να δείτε περισσότερες πληροφορίες
σχετικά με αυτό το αρχείο από εδώ: Προεπιλεγμένοι κανόνες πακέτου Debian.

Αν και δεν θα χρειαστείτε τίποτα περισσότερο για το πρόγραμμα Python σας.

#!/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:

Τώρα δημιουργήσαμε όλα τα απαραίτητα αρχεία για το πρόγραμμά μας με επιτυχία, τώρα ας αρχίσουμε να το πακετάρουμε. Πρώτα, βεβαιωθείτε ότι έχετε εγκαταστήσει ορισμένες εξαρτήσεις για τη διαδικασία κατασκευής πριν ξεκινήσετε.

sudo apt-get update
sudo apt-get install devscripts

Τώρα φανταστείτε ότι ο φάκελος "myprogram " βρίσκεται στον αρχικό σας φάκελο (/home/user/myprogram) για να τον συσκευάσετε ως πακέτο Debian, εκτελέστε τις παρακάτω εντολές.

cd /home/user/myprogram
debuild -us -uc
Δείγμα εξόδου
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.

Και αυτό είναι! Το πακέτο Debian δημιουργήθηκε με επιτυχία:

Για να το εγκαταστήσετε σε οποιαδήποτε διανομή που βασίζεται στο Debian, εκτελέστε.

sudo dpkg -i myprogram_1.0_all.deb

Μην ξεχάσετε να αντικαταστήσετε το παραπάνω αρχείο με το όνομα του πακέτου. Τώρα αφού εγκαταστήσετε το πακέτο, μπορείτε να εκτελέσετε το πρόγραμμα από το μενού εφαρμογών.

Και θα λειτουργήσει..

Εδώ τελειώνει το 4ο μέρος της σειράς μας για το PyGObject.. Στο επόμενο μάθημα θα εξηγήσουμε πώς να τοπικοποιήσετε εύκολα την εφαρμογή PyGObject, μέχρι τότε μείνετε συντονισμένοι για αυτήν…