如何為單一 python 腳本建立 deb 套件?

如何為單一 python 腳本建立 deb 套件?

我有一個 python 腳本,我想將其作為 deb 包分發。它是一個指標它在 Unity 面板中顯示本地日期。我確實關注了從腳本或二進位檔案建立 .deb 套件但我無法創建 deb 包,因為它失敗了。

有人可以給我一個關於我應該做什麼的逐步指導嗎?據我所知,這個腳本依賴於python-appindicator.

筆記:
我不需要任何 Debian/Ubuntu 打包說明的連結。我見過他們中的大多數。我覺得它們對初學者不友善。

答案1

下面是 python 腳本來源套件的基本範例。雖然大多數打包教程都有點複雜,但如果您遇到問題,它們確實可以提供幫助。也就是說,我首先透過簡單地查看 Debian 軟體包來了解 Debian 軟體包的基礎知識。apt-get source類似的東西並透過例子學習。

這是您的基本來源套件佈局:

my-script/
    -- myScript
    -- debian/
        -- changelog
        -- copyright
        -- compat
        -- rules
        -- control
        -- install

dch --create在目錄中運行以建立格式正確的debian/changelog條目。

Debian/版權應該看起來像:

Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: myScript
Upstream-Contact: Name, <email@address>

Files: *
Copyright: 2011, Name, <email@address>
License: (GPL-2+ | LGPL-2 | GPL-3 | whatever)
 Full text of licence.
 .
 Unless there is a it can be found in /usr/share/common-licenses

Debian/相容版可以是:7

Debian/規則:

#!/usr/bin/make -f

%:
    dh $@ --with python2

注意前面一定要有“tab” dh $@ --with python2,不是空格。

注意:Python2 已棄用。對於單一 python 文件,只需dh $@(不含--with python)即可。

Debian/控制:

Source: my-script
Section: python
Priority: optional
Maintainer: Name, <email@address>
Build-Depends: debhelper (>= 7),
               python (>= 2.6.6-3~)
Standards-Version: 3.9.2
X-Python-Version: >= 2.6


Package: my-script
Architecture: all
Section: python
Depends: python-appindicator, ${misc:Depends}, ${python:Depends}
Description: short description
 A long description goes here.
 .
 It can contain multiple paragraphs

Debian/安裝:

myScript usr/bin/

該檔案指示哪個檔案將安裝到哪個資料夾中。

現在建構它debuild --no-tgz-check

這將創建一個功能性 deb 包。 Lintian 將就缺少 orig.tar.gz 發出一些警告,但除非您計劃創建一個適當的上游項目來發布 tarball 版本,否則您現在可能只想忽略它。

答案2

  1. 在您的家中建立一個具有任意名稱的資料夾,例如:mypyscript
  2. 打開資料夾並建立兩個名為“DEBIAN”和“usr”的資料夾
  3. 打開資料夾 DEBIAN。在那裡創建一個名為「control」的文字檔案(不含副檔名)。
  4. 打開“control”並輸入如下內容並保存在DEBIAN上

    Package: mypyscript
    Version: 0.01
    Architecture: all
    Maintainer: your name<your mail id>
    Installed-Size: 2
    Depends: python-appindicator
    Section: extras
    Priority: optional
    Homepage: your homepage
    Description: describe
    
  5. 返回到名為 mypyscript 的資料夾。打開“usr”。建立一個名為「bin」的資料夾。打開“bin”並將 pythonscript 檔案貼到此處。

  6. 您也可以建立選單項目。但這不是必要的。
  7. 返回資料夾「mypyscript」所在的主資料夾或關閉檔案瀏覽器。
  8. 打開終端機。確保該終端位於主資料夾中。鍵入dpkg -b mypyscript。幾秒鐘內你的 deb 包就準備好了

筆記:請正確填寫「控制」文件。不要使用撇號。它僅用於指示名稱。

答案3

.deb從單一 Python 3 腳本製作套件(2021 年更新)

與其他答案相比,這個答案看起來(而且)很長/但是,與已接受的答案不同,它將適用於Python 3,並且在2021 年。要求,就像一頁man

以下是如何透過單一 Python 3 腳本製作 Debian 軟體包(是的,它可以在 Ubuntu 上運行)。首先,我們來製作腳本。這將是一個簡單的 hello-world 程式。命名該檔案hello-world。將其放入名為 的資料夾中hello-world-1.0.0。將該hello-world-1.0.0資料夾放入第二個名為 的資料夾中work。不過,這裡的目錄結構有點……奇怪。debuild(我們用來建構套件的工具)會將.deb檔案放置在我們建構它的位置的上一個目錄中,因此目錄結構將如下所示:

從現在開始,除非另有說明,我將假設您在該work/hello-world-1.0.0目錄中。

work/
├─ hello-world-1.0.0/
│  ├─ hello-world

請注意,我在檔案名稱中使用了連字符,而不是下劃線,因為 Debian 不希望在套件名稱中使用下劃線。我還省略了檔案副檔名,這很好,因為我在腳本頂部添加了一個 shebang。

這是我將用作範例的 Python 腳本。如上所述,它被命名hello-world(沒有檔案副檔名)。

#!/usr/bin/env python3
def hello_world():
    print("Hello world!")

if __name__ == "__main__":
    hello_world()

這個堆疊溢位問題有何if __name__ = "__main__:"意義。 Shebang 包含在內這個問題

首先,只需透過運行來確保程式碼可以運作:

$ chmod +x ./hello-world
$ ./hello-world
Hello world!

要建立該.deb文件,您需要安裝gitdevscriptsbuild-essentiallintianpandoc軟體包。我知道其中一些軟體包是預先安裝的,但我也希望本指南能夠在 Debian 上運行,所以無論如何我都將它們包含在這裡。您可以使用這些命令安裝它們。

sudo apt-get update
sudo apt-get install git devscripts build-essential lintian pandoc

在資料夾內hello-world-1.0.0,建立一個名為 的資料夾debian。在其中建立一個名為 的資料夾source。同樣直接在debian資料夾內建立以下文件changelogcompatcontrolcopyrightinstallrules。在debian/source資料夾內建立一個名為format.

您的目錄樹現在應該如下所示

work/
├─ hello-world-1.0.0/
│  ├─ debian/
│  │  ├─ source/
│  │  │  ├─ format
│  │  ├─ changelog
│  │  ├─ compat
│  │  ├─ control
│  │  ├─ copyright
│  │  ├─ install
│  │  ├─ rules
│  ├─ hello-world

現在,了解這些文件的內容。我假設您直接位於該hello-world-1.0.0資料夾中。

debian/source/format

您通常可以忽略這一點,但如果您想知道,請參閱§5.22在 Debian 文件中。

3.0 (native)

debian/changelog

該檔案包含程式的變更日誌hello-world

hello-world (1.0.0) unstable; urgency=medium

    * Initial release:
 -- John Doe <[email protected]>  Sun, 28 Nov 2021 10:18:51 -0800

debian/compat

這設定了debhelper相容性等級。看§5.2在 Debian 文件中

10

debian/control

apt這設定了諸如工具用來管理套件的各種值。看§4.1在 Debian 文件中

Source: hello-world
Section: python
Maintainer: John Doe <[email protected]>
Build-Depends: debhelper (>= 7),
               python3 (>= 3.5)
Standards-Version: 4.5.1
Priority: optional

Package: hello-world
Architecture: all
Section: python
Depends: python3 (>=3.5), ${misc:Depends}
Description: A simple hello-world program to demenstrate how to package a
 Python 3 script as a deb file

debian/copyright

該文件包含有關原始程式碼的版權和許可的資訊。在這裡,我假設程式碼將遵循 MIT 許可證。根據需要更改此設定。看§4.2在 Debian 文件中

Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/

Files: *
Copyright: 2021 John Doe <[email protected]>
License: MIT-License

Files: debian/*
Copyright: 2021 John Doe <[email protected]>
License: MIT-License

License: MIT-License
 MIT License
 .
 Copyright (c) 2021 John Doe
 .
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 .
 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.
 .
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.

debian/install

此文件控制將哪些文件安裝到您的套件的位置。看§5.11在 Debian 文件中

hello-world usr/bin/
hello-world.1 usr/share/man/man1/

debian/rules

這就是 Debian 建立軟體包的方式。看§4.4在 Debian 文件中。警告:像其他 Makefile 一樣,使用製表符縮排。空格將無法運作。

#!/usr/bin/make -f

%:
    dh $@

我們漂亮的hello-world包應該有一個man頁面。但手冊頁的格式很複雜且難以閱讀。因此,我們將用 Markdown 編寫手冊頁,並讓pandoc我們幫您轉換它。

在主目錄(即直接在hello-world-1.0.0資料夾內)中,建立一個名為hello-world.1.md.將以下內容放入文件中:

% hello-world(1) hello-world 1.0.0
% John Doe
% November 2021

# NAME
hello-world - Prints a file until a null-character is reached

# SYNOPSIS
**hello-world** [*options*]

# DESCRIPTION
**hello-world** prints a file character-by-character until a null character is reached. The null character will not be printed

# OPTIONS

# EXAMPLES
**hello-world**
: Prints the text "Hello world!", followed by a newline, to the screen.

建置這個和包需要幾個步驟。因此,讓我們創建一個(Bash)腳本來代替。建立一個名為build.使其可執行chmod +x ./build。將以下內容放入文件中:

#!/usr/bin/env bash
pandoc hello-world.1.md -s -t man > hello-world.1
debuild --no-tgz-check -uc -us

這將產生手冊頁,並將其儲存在名為hello-world.1.然後它將建立該包。這-uc -us 意味著我們不會使用 GPG 金鑰對其進行簽名,因為這很複雜。運行腳本(./build),如果一切順利,在父目錄中就會有產生的套件work

答案4

我會快速查看,非常適合創建快速應用程式和生成 debs google it,或者您可以在這裡找到教程http://developer.ubuntu.com/

相關內容