教學 3 - 分發包裝¶
到目前為止,我們一直在``開發人員模式``下運行我們的應用程式。這使我們可以輕鬆地在本地運行我們的應用程式 - 但我們真正想要的是能夠將我們的應用程式提供給其他人。
但是,我們不想教導使用者如何安裝 Python、建立虛擬環境、複製 git 儲存庫以及在開發人員模式下執行 Briefcase。我們寧願只給他們一個安裝程序,然後讓應用程式正常工作。
公文包可用於打包您的應用程式以透過這種方式進行分發。
創建您的應用程式支架¶
由於這是我們第一次打包應用程序,因此我們需要創建一些配置文件和其他腳手架來支援打包過程。從``helloworld``目錄中,運行:
(beeware-venv) $ briefcase create
[helloworld] Generating application template...
Using app template: https://github.com/beeware/briefcase-macOS-app-template.git, branch v0.3.18
...
[helloworld] Installing support package...
...
[helloworld] Installing application code...
Installing src/helloworld... done
[helloworld] Installing requirements...
...
[helloworld] Installing application resources...
...
[helloworld] Removing unneeded app content...
Removing unneeded app bundle content... done
[helloworld] Created build/helloworld/macos/app
(beeware-venv) $ briefcase create
[helloworld] Finalizing application configuration...
Targeting ubuntu:jammy (Vendor base debian)
Determining glibc version... done
Targeting glibc 2.35
Targeting Python3.10
[helloworld] Generating application template...
Using app template: https://github.com/beeware/briefcase-linux-AppImage-template.git, branch v0.3.18
...
[helloworld] Installing support package...
No support package required.
[helloworld] Installing application code...
Installing src/helloworld... done
[helloworld] Installing requirements...
...
[helloworld] Installing application resources...
...
[helloworld] Removing unneeded app content...
Removing unneeded app bundle content... done
[helloworld] Created build/helloworld/linux/ubuntu/jammy
(beeware-venv) C:\...>briefcase create
[helloworld] Generating application template...
Using app template: https://github.com/beeware/briefcase-windows-app-template.git, branch v0.3.18
...
[helloworld] Installing support package...
...
[helloworld] Installing application code...
Installing src/helloworld... done
[helloworld] Installing requirements...
...
[helloworld] Installing application resources...
...
[helloworld] Created build\helloworld\windows\app
您可能剛剛在終端機中看到了內容頁面……那麼剛剛發生了什麼?公事包做了以下事情:
它**生成了一個應用程式模板**。建立本機安裝程式需要大量檔案和配置,超出了實際應用程式的程式碼。對於同一平台上的每個應用程式來說,這個額外的腳手架幾乎都是相同的,除了正在構建的實際應用程式的名稱之外- 因此,Briefcase 為其支援的每個平台提供了一個應用程式模板。此步驟將推出模板,根據需要替換應用程式的名稱、捆綁 ID 和設定檔的其他屬性,以支援您正在建置的平台。
如果您對公文包提供的範本不滿意,您可以提供自己的範本。但是,在使用公文包的預設範本有更多經驗之前,您可能不想這樣做。
它**下載並安裝了支援包**。公事包採用的打包方法最好被描述為``可能有效的最簡單的方法``——它提供了一個完整的、獨立的 Python 解釋器,作為它構建的每個應用程式的一部分。這在空間效率方面稍顯低下 - 如果您有 5 個使用 Briefcase 打包的應用程序,那麼您將擁有 5 個 Python 解釋器副本。然而,這種方法保證每個應用程式都是完全獨立的,使用已知可與該應用程式配合使用的特定 Python 版本。
同樣,Briefcase 為每個平台提供了預設的支援包;如果需要,您可以提供自己的支援包,並將該包包含在建置過程中。如果您需要啟用 Python 解釋器中的特定選項,或者想要從標準庫中刪除執行時間不需要的模組,您可能需要執行此操作。
公文包維護支援包的本機緩存,因此一旦您下載了特定的支援包,該快取的副本將在未來的版本中使用。
As noted above, when Briefcase packages an app as a native Linux system package (the default package format for Linux), a support package is not included with the app. Instead, the app will use the Python that is provided by the distribution of Linux being targeted.
它**安裝了應用程式要求**。您的應用程式可以指定運行時所需的任何第三方模組。這些將使用``pip``安裝到應用程式的安裝程式中。
它**安裝了您的應用程式代碼**。您的應用程式將擁有自己的程式碼和資源(例如,運行時所需的映像);這些檔案被複製到安裝程式中。
它 安裝了應用程式所需的資源。 最後,它添加了安裝程式本身所需的任何其他資源。這包括需要附加到最終應用程式的圖示和啟動螢幕圖像等內容。
完成後,如果您查看專案目錄,您現在應該會看到與您的平台(macOS
、linux``或``windows
)相對應的目錄,其中包含其他檔案。這是您的應用程式的特定於平台的打包配置。
建立您的應用程式¶
現在您可以編譯您的應用程式。此步驟執行應用程式在目標平台上可執行所需的任何二進位編譯。
(beeware-venv) $ briefcase build
[helloworld] Adhoc signing app...
...
Signing build/helloworld/macos/app/Hello World.app
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 00:07
[helloworld] Built build/helloworld/macos/app/Hello World.app
在 macOS 上,build
命令不需要 編譯 任何內容,但它確實需要對二進位內容進行簽名,以便可以執行。此簽名是 臨時 簽名 - 它僅適用於 您的 機器;如果您想將應用程式分發給其他人,則需要提供完整的簽名。
(beeware-venv) $ briefcase build
[helloworld] Finalizing application configuration...
Targeting ubuntu:jammy (Vendor base debian)
Determining glibc version... done
Targeting glibc 2.35
Targeting Python3.10
[helloworld] Building application...
Build bootstrap binary...
make: Entering directory '/home/brutus/beeware-tutorial/helloworld/build/linux/ubuntu/jammy/bootstrap'
...
make: Leaving directory '/home/brutus/beeware-tutorial/helloworld/build/linux/ubuntu/jammy/bootstrap'
Building bootstrap binary... done
Installing license... done
Installing changelog... done
Installing man page... done
Updating file permissions... done
Stripping binary... done
[helloworld] Built build/helloworld/linux/ubuntu/jammy/helloworld-0.0.1/usr/bin/helloworld
此步驟完成後,build``資料夾將包含一個``helloworld-0.0.1``資料夾,其中包含 Linux
/usr``檔案系統的鏡像。該檔案系統鏡像將包含一個包含``helloworld``二進位檔案的``bin``資料夾,以及支援該二進位檔案所需的``lib``和``share``資料夾。
(beeware-venv) C:\...>briefcase build
Setting stub app details... done
[helloworld] Built build\helloworld\windows\app\src\Hello World.exe
在 Windows 上,build
命令不需要 編譯 任何內容,但它確實需要編寫一些元數據,以便應用程式知道其名稱、版本等。
觸發防毒
由於此元資料在``create``命令期間直接寫入從範本推出的預編譯二進位檔案中,因此這可能會觸發電腦上執行的防毒軟體並阻止寫入元資料。在這種情況下,指示防毒軟體允許該工具(名為``rcedit-x64.exe``)運行並重新執行上述命令。
運行您的應用程式¶
現在您可以使用 Briefcase 來運行您的應用程式:
(beeware-venv) $ briefcase run
[helloworld] Starting app...
===========================================================================
Configuring isolated Python...
Pre-initializing Python runtime...
PythonHome: /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib
PYTHONPATH:
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python311.zip
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib/lib-dynload
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/app_packages
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/app
Configure argc/argv...
Initializing Python runtime...
Installing Python NSLog handler...
Running app module: helloworld
---------------------------------------------------------------------------
(beeware-venv) $ briefcase run
[helloworld] Finalizing application configuration...
Targeting ubuntu:jammy (Vendor base debian)
Determining glibc version... done
Targeting glibc 2.35
Targeting Python3.10
[helloworld] Starting app...
===========================================================================
Install path: /home/brutus/beeware-tutorial/helloworld/build/helloworld/linux/ubuntu/jammy/helloworld-0.0.1/usr
Pre-initializing Python runtime...
PYTHONPATH:
- /usr/lib/python3.10
- /usr/lib/python3.10/lib-dynload
- /home/brutus/beeware-tutorial/helloworld/build/helloworld/linux/ubuntu/jammy/helloworld-0.0.1/usr/lib/helloworld/app
- /home/brutus/beeware-tutorial/helloworld/build/helloworld/linux/ubuntu/jammy/helloworld-0.0.1/usr/lib/helloworld/app_packages
Configure argc/argv...
Initializing Python runtime...
Running app module: helloworld
---------------------------------------------------------------------------
(beeware-venv) C:\...>briefcase run
[helloworld] Starting app...
===========================================================================
Log started: 2023-04-23 04:47:45Z
PreInitializing Python runtime...
PythonHome: C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src
PYTHONPATH:
- C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src\python39.zip
- C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src
- C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src\app_packages
- C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src\app
Configure argc/argv...
Initializing Python runtime...
Running app module: helloworld
---------------------------------------------------------------------------
這將開始使用``build``命令的輸出來執行您的本機應用程式。
您可能會注意到應用程式運行時的外觀存在一些細微的差異。例如,作業系統顯示的圖示和名稱可能與您在開發人員模式下運行時看到的圖示略有不同。這也是因為您正在使用打包的應用程序,而不僅僅是運行 Python 程式碼。從作業系統的角度來看,您現在運行的是``應用程式``,而不是``Python 程式``,這反映在應用程式的顯示方式上。
建立您的安裝程式¶
現在,您可以使用``package``命令打包您的應用程式以進行分發。 package 命令執行將鷹架專案轉換為最終的可分發產品所需的任何編譯。根據平台的不同,這可能涉及編譯安裝程式、執行程式碼簽署或執行其他預分發任務。
(beeware-venv) $ briefcase package --adhoc-sign
[helloworld] Signing app...
*************************************************************************
** WARNING: Signing with an ad-hoc identity **
*************************************************************************
This app is being signed with an ad-hoc identity. The resulting
app will run on this computer, but will not run on anyone else's
computer.
To generate an app that can be distributed to others, you must
obtain an application distribution certificate from Apple, and
select the developer identity associated with that certificate
when running 'briefcase package'.
*************************************************************************
Signing app with ad-hoc identity...
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 00:07
[helloworld] Building DMG...
Building dist/Hello World-0.0.1.dmg
[helloworld] Packaged dist/Hello World-0.0.1.dmg
dist
資料夾將包含一個名為``Hello World-0.0.1.dmg`` 的檔案。如果您在 Finder 中找到此文件,然後雙擊其圖標,您將安裝 DMG,為您提供 Hello World 應用程式的副本以及指向您的應用程式資料夾的鏈接,以便於安裝。將應用程式檔案拖曳到應用程式中,您就已經安裝了應用程式。將 DMG 檔案發送給朋友,他們應該能夠執行相同的操作。
在此範例中,我們使用了``–adhoc-sign``選項 - 也就是說,我們使用 ad hoc 憑證簽署我們的應用程式 - 只能在您的電腦上使用的臨時憑證。我們這樣做是為了讓教程簡單。設定程式碼簽署身分有點繁瑣,並且只有在您打算將應用程式分發給其他人時才``需要``它們。如果我們要發布真實的應用程式供其他人使用,我們需要指定真實的憑證。
當您準備好發布實際應用程式時,請查看有關``設定 macOS 代碼簽署身分``的公文包操作指南 <https://briefcase.readthedocs.io/en/latest/how-to/code-signing /macOS. html>`__
根據您的 Linux 發行版,打包步驟的輸出將略有不同。如果您使用的是 Debian 派生的發行版,您將看到:
(beeware-venv) $ briefcase package
[helloworld] Finalizing application configuration...
Targeting ubuntu:jammy (Vendor base debian)
Determining glibc version... done
Targeting glibc 2.35
Targeting Python3.10
[helloworld] Building .deb package...
Write Debian package control file... done
dpkg-deb: building package 'helloworld' in 'helloworld-0.0.1.deb'.
Building Debian package... done
[helloworld] Packaged dist/helloworld_0.0.1-1~ubuntu-jammy_amd64.deb
dist
資料夾將包含產生的``.deb`` 檔案。
如果您使用的是基於 RHEL 的發行版,您將看到:
(beeware-venv) $ briefcase package
[helloworld] Finalizing application configuration...
Targeting fedora:40 (Vendor base rhel)
Determining glibc version... done
Targeting glibc 2.39
Targeting Python3.12
[helloworld] Building .rpm package...
Generating rpmbuild layout... done
Write RPM spec file... done
Building source archive... done
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.Kav9H7
+ umask 022
...
+ exit 0
Building RPM package... done
[helloworld] Packaged dist/helloworld-0.0.1-1.fc40.x86_64.rpm
dist
資料夾將包含產生的``.rpm`` 檔案。
如果您使用的是基於 Arch 的發行版,您將看到:
(beeware-venv) $ briefcase package
[helloworld] Finalizing application configuration...
Targeting arch:20240101 (Vendor base arch)
Determining glibc version... done
Targeting glibc 2.38
Targeting Python3.12
[helloworld] Building .pkg.tar.zst package...
...
Building Arch package... done
[helloworld] Packaged dist/helloworld-0.0.1-1-x86_64.pkg.tar.zst
dist
資料夾將包含產生的``.pkg.tar.zst`` 檔案。
目前不支援打包其他 Linux 發行版。
如果您想為您正在使用的 Linux 發行版以外的發行版建立軟體包,Briefcase 也可以提供協助 - 但您需要安裝 Docker。
Docker Engine 的官方安裝程式可用於一系列 Unix 發行版。請遵循適合您平台的說明;但是,請確保不要以``無根``模式安裝 Docker。
安裝 Docker 後,您應該能夠啟動 Linux 容器 - 例如:
$ docker run --rm -it ubuntu:22.04
將在 Ubuntu 22.04 Docker 容器內向您顯示 Unix 提示字元(類似``root@844444e31cff9:/#``)。鍵入 Ctrl-D 退出 Docker 並返回本機 shell。
安裝 Docker 後,您可以透過傳入 Docker 映像作為參數,使用 Briefcase 為 Briefcase 支援的任何 Linux 發行版建置套件。例如,要為 Ubuntu 22.04 (Jammy) 建立 DEB 包,無論您使用什麼作業系統,都可以運行:
$ briefcase package --target ubuntu:jammy
這將為您選擇的作業系統下載 Docker 映像,建立一個能夠運行 Briefcase 建置的容器,並在映像內建置應用程式包。完成後,dist
資料夾將包含目標 Linux 發行版的軟體包。
(beeware-venv) C:\...>briefcase package
*************************************************************************
** WARNING: No signing identity provided **
*************************************************************************
Briefcase will not sign the app. To provide a signing identity,
use the `--identity` option; or, to explicitly disable signing,
use `--adhoc-sign`.
*************************************************************************
[helloworld] Building MSI...
Compiling application manifest...
Compiling... done
Compiling application installer...
helloworld.wxs
helloworld-manifest.wxs
Compiling... done
Linking application installer...
Linking... done
[helloworld] Packaged dist\Hello_World-0.0.1.msi
在此範例中,我們使用了``–adhoc-sign``選項 - 也就是說,我們使用 ad hoc 憑證簽署我們的應用程式 - 只能在您的電腦上使用的臨時憑證。我們這樣做是為了讓教程簡單。設定程式碼簽署身分有點繁瑣,並且只有在您打算將應用程式分發給其他人時才``需要``它們。如果我們要發布真實的應用程式供其他人使用,我們需要指定真實的憑證。
當您準備好發布實際應用程式時,請查看有關``設定 macOS 代碼簽署身分``的公文包操作指南 <https://briefcase.readthedocs.io/en/latest/how-to/code-signing /macOS. html>`__
此步驟完成後,dist``資料夾將包含一個名為 ``Hello_World-0.0.1.msi
的檔案。如果您雙擊此安裝程式來運行它,您應該經歷熟悉的 Windows 安裝過程。安裝完成後,開始功能表中將出現一個 「Hello World」 項目。
下一步¶
現在,我們已將應用程式打包以便在桌面平台上分發。但是當我們需要更新應用程式中的程式碼時會發生什麼?我們如何將這些更新添加到打包的應用程式中?請參閱 教程 4 以了解…