Pakkaðu PyGObject forritum og forritum sem .deb pakka fyrir Linux skjáborðið – Part 4


Við höldum áfram PyGObject forritunarröðinni með þér á Linux skjáborðinu, í 4. hluta seríunnar munum við útskýra hvernig á að pakka inn forritunum og forritunum sem við bjuggum til fyrir Linux skjáborð með PyGObject sem Debian pakka.

Debian pakkar (.deb) eru mest notaða sniðið til að setja upp forrit undir Linux, \dpkg kerfinu sem fjallar um .deb pakka er sjálfgefið á öllum Debian-undirstaða Linux dreifingu eins og Ubuntu og Linux Mint. Þess vegna munum við aðeins útskýra hvernig á að pakka forritunum okkar fyrir Debian.

Búðu til Debian pakka úr PyGObject forritunum þínum

Í fyrsta lagi ættir þú að hafa grunnþekkingu um að búa til Debian pakka, þessi eftirfarandi handbók mun hjálpa þér mikið.

  1. Kynning á Debian umbúðum

Í stuttu máli, ef þú ert með verkefni sem heitir \myprogram verður það að innihalda eftirfarandi skrár og möppur svo þú getir pakkað því.

  1. debian (Mappa): Þessi mappa inniheldur allar upplýsingar um Debian pakkann skipt í margar undirskrár.
  2. po (Mappa): Po mappan inniheldur þýðingarskrár fyrir forritið (við munum útskýra það í hluta 5).
  3. mitt forrit (Skrá): Þetta er Python skráin sem við bjuggum til með PyGObject, hún er aðalskrá verkefnisins.
  4. ui.glade (Skrá): Myndræna notendaviðmótsskráin.. Ef þú bjóst til viðmót forritsins með Glade, verður þú að hafa þessa skrá með í
    verkefnið þitt.
  5. bMyprogram.desktop (Skrá): Þetta er ábyrg skrá fyrir að sýna forritið í forritavalmyndinni.
  6. setup.py (Skrá): Þessi skrá er ábyrg fyrir því að setja upp hvaða Python forrit sem er í staðbundnu kerfið, hún er mjög mikilvæg í hvaða Python forriti sem er, hún hefur líka margar aðrar leiðir til notkunar.

Auðvitað .. Það eru margar aðrar skrár og möppur sem þú getur sett inn í verkefnið þitt (reyndar geturðu sett allt sem þú vilt) en þetta eru grunnatriðin.

Nú skulum við byrja að pakka verkefni. Búðu til nýja möppu sem heitir \myprogram, búðu til skrá sem heitir \myprogram og bættu eftirfarandi kóða við hana.

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

Búðu til ui.glade skrá og fylltu hana upp með þessum kóða.

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

Það er ekkert nýtt fyrr en núna.. Við bjuggum bara til Python skrá og viðmótsskrá hennar. Búðu til \setup.py skrá í sömu möppu og bættu eftirfarandi kóða við hana, hver lína er útskýrð í athugasemdunum.

# 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. 

Búðu til \myprogram.desktop skrá í sömu möppu og bættu við eftirfarandi kóða, það er líka útskýrt í athugasemdunum.

# 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 

Við erum næstum því búin hér núna.. Við verðum bara að búa til nokkrar litlar skrár undir \debian möppunni til að veita upplýsingar um pakkann okkar fyrir \dpkg” kerfi.

Opnaðu \debian möppuna og búðu til eftirfarandi skrár.

control
compat
changelog
rules

stýring: Þessi skrá veitir grunnupplýsingar um Debian pakkann, fyrir frekari upplýsingar, vinsamlegast farðu á 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: Þetta er bara mikilvæg skrá fyrir dpkg kerfið, hún inniheldur bara töfrandi 9 töluna, láttu það vera svona.

9

breytingaskrá: Hér muntu geta bætt við breytingunum sem þú gerir á forritinu þínu, fyrir frekari upplýsingar, vinsamlegast farðu á 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

reglur: Þessi skrá er ábyrg fyrir því að keyra uppsetningarferlið á staðbundinni vél til að setja upp pakkann, þú getur skoðað frekari upplýsingar
um þessa skrá héðan: Debian Package Default Rules.

Þó að þú þurfir ekki neitt meira fyrir Python forritið þitt.

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

Núna bjuggum við til allar nauðsynlegar skrár fyrir forritið okkar með góðum árangri, nú skulum við byrja að pakka því. Fyrst skaltu ganga úr skugga um að þú hafir sett upp ósjálfstæði fyrir byggingarferlið áður en þú byrjar.

$ sudo apt-get update
$ sudo apt-get install devscripts

Ímyndaðu þér nú að \myprogram mappan sé í heimamöppunni þinni (/home/user/myprogram) til að pakka henni sem Debian pakka skaltu keyra eftirfarandi skipanir .

$ cd /home/user/myprogram
$ debuild -us -uc
[email :~/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.

Og þannig er það ! Debian pakkinn þinn var búinn til með góðum árangri:

Til að setja það upp á hvaða Debian-dreifingu sem er skaltu keyra.

$ sudo dpkg -i myprogram_1.0_all.deb

Ekki gleyma að skipta út ofangreindri skrá með nafni pakkans.. Nú eftir að þú hefur sett upp pakkann geturðu keyrt forritið úr forritavalmyndinni.

Og það mun virka..

Hér lýkur 4. hluta seríunnar okkar um PyGObject.. Í næstu kennslustund munum við útskýra hvernig á að staðsetja PyGObject forritið auðveldlega, þangað til fylgstu með því...