python打包RPM并部署

上篇文章讲述了python打whl方法,本篇文章主讲python打rpm包,在上篇文章的基础上进行调整
修改:
1、以MANIFEST.in、setup.cfg替换build.py,位置处于项目根目录

  • MANIFEST.in文件通常用来管理项目外部文件,其实它也可以用来管理内部文件,它将决定哪些文件可以被打进包内根目录

2、以scripts文件夹代替install_monitor.sh文件,位置处于项目根目录
3、默认打包文件有README.rst/README.md、setup.cfg、MANIFEST.in、其它的文件则需要手动在 MANIFEST.in 里添加(格式include)

  • setup.cfg
# name/version与setup.py中一致
[metadata]
name=monitor  # 打包名称
version=1.0.0  # 打包版本
author=libai  # 打包作者
author_email=libai@github.com  # 打包者邮箱
url=http://github.com/libai/test-project-python  # 通常为GitHub上的包链接或者readthedocs的包链接
license=Proprietary  # 项目许可证
description=Demo project in python  # 描述信息
long_description=file:README.md  # 打包说明文件为README.md
long_description_content_type=text/markdown  # 长描述

[options]
python_requires = >=3.7*  # python版本要求
packages=
install_requires=  # 依赖的python库,使用pip安装本项目时会自动检查和安装依赖
	geopy
	geographiclib
	numpy
	pandas
setup_requires=  # 打包时候的依赖
	pip
	wheel
	Cython
	twine
tests_require=  # 测试时候的依赖
	pylint
	pytest
# 如上所示信息,全都在metadata标签内,表示不管打wheel包还是rpm都会启用的配置
[bdist_wheel]  # 打wheel包时需要的参数配置,不写就是没有额外配置

[bdist_rpm]  # 打rpm时需要的参数配置
release=1  # 发行版本号,默认是1
requires=依赖其它软件包名
vendor=Li Bai <libai@github.com>
build_script=scripts/rpm_build.spec  # rpm包被编译时执行的脚本
install_script=scripts/rpm_install.spec  # 安装时执行的脚本
post_install=scripts/rpm_postin.spec  # 安装后执行的脚本
post_uninstall=scripts/rpm_postun.spec  # 卸载时执行的脚本


  • MANIFEST.in
include README.md
include *.conf
include *.yaml
include entry.py
include setup.py
include setup.cfg
include requirements.txt
include MANIFEST.in
recursive-include monitor *.py
recursive-include scripts *.*
  • scripts文件夹

    • rpm_build.spec
    • rpm_install.spec
    • rpm_postin.spec
    • rpm_postun.spec
  • rpm_build.spec

# variables
%define project_libs %{_builddir}/../../../../libs/linux
# build wheelpackage
python3.7 -m pip install --no-cache-dir --no-index --find-links %{project_libs} -r requirements.txt
python3.7 setup.py bdist_wheel

# build private venv
python3.7 -m venv venv

  • rpm_install.spec
# variables
%define project_libs %{_builddir}/../../../../libs/linux
%define release_home %{_buildroot}/opt/%{name}/%{name}-%{version}
%define release_libs %{_buildroot}/opt/%{name}/%{name}-%{version}/libs/linux

# home entries
mkdir -p %{release_home}
install -D entry.py %{release_home}/entry.py

# log entries
mkdir -p %{release_home}/log
touch %{release_home}/log/%{name}.log
touch %{release_home}/log/%{name}-e.log

# venv entries (copy to destination, patch, scripts, and cleanup)
export CURRENT_VENV=`pwd`/venv
export RELEASE_VENV=/opt/%{name}/%{name}-%{version}/venv
cp -r venv %{release_home}
find %{release_home}/venv/bin/* -type f | xargs sed -i "s/${CURRENT_VENV//\//\\/}/${RELEASE_VENV//\//\\/}/g"
find %{release_home}/venv -type d -name "__pycache__" -exec rm -r {} +

# libs entries (install to current venv then save wheels to release_libs)
mkdir -p %{release_libs}
venv/bin/python3.7 -m pip install --no-cache-dir --no-index --find-links %{project_libs} -r requirements.txt
venv/bin/python3.7 -m pip install --no-cache-dir --no-index --find-links %{project_libs} dist/*.whl
venv/bin/python3.7 -m pip list --formatfreeze > %{release_home}/requirements.txt
venv/bin/python3.7 -m pip wheel -w %{project_libs} --no-index --find-links %{project_libs} --find-links dist -r %{release_home}/requirements.txt

# supervisor entry
install -D %{name}.conf %{buildroot}/etc/supervisor/conf.d/%{name}.conf

# gather installed files (w/o directories)
(cd %{buildroot} && find . type f -or -type l) | sed "s/\.//" | grep "\S" | sed "s/^/'/;s/$/'/" > INSTALLED_FILES

  • rpm_postin.spec
# install packages
/opt/%{name}/%{name}-%{version}/venv/bin/python3.7 -m pip install --no-cache-dir --no-index --find-links /opt/%{name}/%{name}-%{version}/libs/linux -r /opt/%{name}/%{name}-%{version}/requirements.txt

# update supervisor
/usr/local/bin/supervisorctl update

  • rpm_postun.spec
# cleanup home directory
rm -rf /opt/%{name}/%{name}-%{version}/venv
find /opt/%{name}/%{name}-%{version} -depth -type d -exec rmdir {} + 

# update supervisor
/usr/local/bin/supervisorctl update
  • gitlab-ci.yml
default:
	image:docker镜像仓库

variable:
	PROJECT: 项目名称,与setup.py中name对应
	VERSION: 项目版本,与setup.py中version对应
	RELEASE: 1

before_script:
	-python3.7 -V
	-pip3 config set global.index-url 包的镜像地址,比如豆瓣源、清华源、阿里源
	-pip3 config set global.trusted-host 地址,比如0.0.0.0
	-pip3 config set global.username 用户名,进网站获取包的账户名
	-pip3 config set global.password 密码,进网站获取包的密码
	-pip3 install --no-cache-dir -r requirements.txt

make:
	stage:build
	script:
		- echo "Building wheel package"
		- python3.7 setup.py bdist_wheel

		- echo "Building rpm package"
		- python3.7 setup.py bdist_rpm
	artifacts:
		paths:
			- dist/*.whl
			- dist/$PROJECT-$VERSION-$RELEASE.x86_64.rpm

test:
	stage: test
	script:
		- echo "Running lint project"
		- python3.7 -m pylint --exit-zero $PROJECT
		
		- echo "Running unit project"
		- python3.7 pytest -v --junitxml=report.xml $PROJECT

		- echo "Testing wheel package"
		- pip3 install --no-cache-dir --force-reinstall dist/*.whl
		
		- echo "Testing rpm package"
		- rpm -ivh --nodeps dist/$PROJECT-$VERSION-$RELEASE.x86_64.rpm

	needs:
		-job:make
		artifacts:true
	artifacts:
		when:always
		reports:
			junit:report.xml

save:
	stage: deploy
	script:
		- echo "Releasing wheel package"
		- twine upload --non-interactive --repository-url 上传的包存储地址 dist/*.whl
		
		- echo "Releasing rpm package"
		- curl -u $上面的用户名:上面的密码 --upload-file dist/$PROJECT-$VERSION-$RELEASE.x86_64.rpm 上传的包存储位置/$PROJECT-$VERSION-$RELEASE.x86_64.rpm
		
	needs:
		- job: make
		artifacts: true

		- job: test
	
	rules:
		- if "$CI_COMMIT_REF_NAME == 'dev_test'"
	

spec语法:https://www.yuque.com/python_study/environment/ts4gsu
rpm命令:https://zhuanlan.zhihu.com/p/387081422
参考文章:https://www.jianshu.com/p/c16319a401cd
https://blog.csdn.net/ustczhng2012/article/details/117562053
https://cloud.tencent.com/developer/article/1417208?from=14588
https://www.cnblogs.com/andy-linux/p/10007929.html
https://blog.csdn.net/weixin_39575047/article/details/111619599
https://blog.csdn.net/mutong_wu/article/details/104006775
https://blog.csdn.net/lynn_kong/article/details/17540207
https://www.cnblogs.com/yinzhengjie/p/14124623.html
https://www.imooc.com/article/253042
————————————————
版权声明:本文为CSDN博主「学无止境gwx」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_49278803/article/details/120048323

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学无止境gwx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值