python3.5create.rpm和pyinstaller生成的executab

2024-06-01 05:06:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我用pyinstaller生成了一个构建。 我需要创建.rpm包,它将把可执行文件放入/usr/bin/,并创建一个systemd服务来运行该可执行文件。在

我找到这个了 https://docs.python.org/3/distutils/builtdist.htmlhttps://docs.python.org/2.0/dist/creating-rpms.html

但这并不能给我一个完整的画面。在

  1. 有可能做到吗?

  2. 我需要使用什么工具集?(基本上,how来制作它)。

  3. 如果可能-示例代码


Tags: httpsorgcreating可执行文件docsbinusrdist
1条回答
网友
1楼 · 发布于 2024-06-01 05:06:24

首先,忘掉bdist_rpm。它用于distutils/setuptools项目,因此您需要一个setup.py脚本,该脚本在幕后调用pyinstaller来绑定可执行文件,以某种方式重新定义install_scripts命令,以便能够打包二进制可执行文件,并处理systemd单元文件的打包。相反,请编写一个spec文件,它是rpm构建和安装包的说明手册。在

设置

这是要使用的示例项目。在

so-51640995
├── bacon.service
├── bacon.spec
├── bacon.timer
└── spam.py

spam.py

这里没有魔法-调用后打印eggs。将通过pyinstaller绑定到名为bacon的二进制文件。我没有调用项目spam来避免歧义,因为pyinstaller还创建了一个扩展名为.spec的文件,因此运行它不会覆盖rpm规范文件。在

^{pr2}$

bacon.service

调用二进制文件bacon的简单服务。在

[Unit]
Description=Bacon emitting eggs

[Service]
ExecStart=/usr/bin/bacon
Restart=always

bacon.timer

将每隔10秒调用bacon。在

[Unit]
Description=Timer for bacon to emit eggs from time to time

[Timer]
OnUnitInactiveSec=10s
OnBootSec=10s
Unit=bacon.service

[Install]
WantedBy=timers.target

bacon.spec

包装说明。在%build部分,我们将spam.py捆绑,然后将捆绑的可执行文件dist/spamsystemd单元文件一起安装到/usr/bin/bacon。在

Name: bacon
Version: 1
Release: 1
Summary: bacon that shouts 'eggs!' from time to time
License: MIT
Requires: systemd

%description
bacon that shouts 'eggs!' from time to time

%build
pyinstaller  onefile %{_sourcedir}/spam.py

%install
mkdir -p %{buildroot}%{_bindir}
mkdir -p %{buildroot}%{_unitdir}
install -m 755 dist/spam %{buildroot}%{_bindir}/bacon
install -m 755 %{_sourcedir}/bacon.service %{buildroot}%{_unitdir}/bacon.service
install -m 755 %{_sourcedir}/bacon.timer %{buildroot}%{_unitdir}/bacon.timer

%files
%{_bindir}/bacon
%{_unitdir}/bacon.service
%{_unitdir}/bacon.timer

构建包

有很多教程深入解释了如何构建rpm包,例如Fedora Packaging Guidelines,因此只需在这里列出最小的命令序列:

$ # install the bare minimum of required packages
$ sudo dnf install rpm-build rpm-devel rpmdevtools
$ # first-time setup of build dirs
$ rpmdev-setuptree
$ # copy the source files
$ cp * $HOME/rpmbuild/SOURCES/
$ # invoke the build
$ rpmbuild -ba bacon.spec

测试程序包

$ sudo rpm -ivp $HOME/rpmbuild/RPMS/x86_64/bacon-1-1.x86_64.rpm

编辑:如注释中所述,使用-U代替{}。引用rpm人的话:

The general form of an rpm upgrade command is

 rpm {-U| upgrade} [install-options] PACKAGE_FILE ...

This upgrades or installs the package currently installed to a newer version. This is the same as install, except all other version(s) of the package are removed after the new package is installed.

所以使用

$ sudo rpm -Uvp $HOME/rpmbuild/RPMS/x86_64/bacon-1-1.x86_64.rpm

用于测试安装。在

现在bacon应该可以从命令行获得:

$ bacon
eggs!

启动计时器:

$ sudo systemctl start bacon.timer
$ systemctl status bacon.timer
● bacon.timer - Timer for bacon to emit eggs from time to time
   Loaded: loaded (/usr/lib/systemd/system/bacon.timer; disabled; vendor preset: disabled)
   Active: active (waiting) since Tue 2018-08-07 15:36:28 CEST; 29s ago
  Trigger: Tue 2018-08-07 15:36:58 CEST; 979ms left

检查日志:

$ sudo journalctl -u bacon
  Logs begin at Mon 2017-07-03 12:49:51 CEST, end at Tue 2018-08-07 15:37:02 CEST.  
Aug 07 15:36:28 XXX systemd[1]: Started Bacon emitting eggs.
Aug 07 15:36:28 XXX bacon[128222]: eggs!
Aug 07 15:36:28 XXX systemd[1]: bacon.service: Service hold-off time over, scheduling restart.
Aug 07 15:36:28 XXX systemd[1]: Stopped Bacon emitting eggs.
Aug 07 15:36:28 XXX systemd[1]: Started Bacon emitting eggs.
Aug 07 15:36:28 XXX bacon[128224]: eggs!
Aug 07 15:36:28 XXX systemd[1]: bacon.service: Service hold-off time over, scheduling restart.
Aug 07 15:36:28 XXX systemd[1]: Stopped Bacon emitting eggs.
Aug 07 15:36:28 XXX systemd[1]: Started Bacon emitting eggs.
Aug 07 15:36:29 XXX bacon[128226]: eggs!
...

一旦验证工作正常,停止计时器并卸载bacon

$ sudo systemctl stop bacon.timer
$ sudo rpm -e bacon
$ sudo systemctl daemon-reload
$ sudo systemctl reset-failed

相关问题 更多 >