python2项目打包rpm笔记

python2项目打包成rpm包

项目目录结构和说明

# 其中demo的是项目的根目录
# pkg是代码逻辑目录
# config是配置文件目录
# main.py是项目的开始目录
# Makefile是打包文件
# demo.spce是打包的具体步骤脚本
# setup.cfg和setup.py是python包的规范
-demo
--pkg1
----__init__.py
----...
--config
----...
--main.py
--Makefile
--demo.spec
--setup.cfg
--setup.py

setup.cfg和setup.py

# setup.cfg 示例
[metadata]
name=demo
version=1.0.0
url=http://example.com
author=author
description=description
long_description=

[options]
python_requires= >=2.7
packages= find:
scripts=
    main.py
include_package_data=True
install_requires=
  requests
  ....

[options.entry_points]
console_scripts = 
    democli =   packageName.moduleName:funcName

[options.package_data]
config= *

[easy_install]
index_url = https://mirrors.ustc.edu.cn/pypi/web/simple

[options.packages.find]
exclude = 
  .gitignore
  .git
  ...

# setup.py示例
from setuptools import setup

if __name__ == "__main__":
  setup()

Makefile示例

APP_DIR_PATH  := ${CURDIR}
DIRNAME          := $(notdir ${APP_DIR_PATH})
OBJECT_NAME := ${DIR_NAME}
RPM_TOP_DIR  := $(shell rpm -E %{_topdir})

VERSION.        := $(shell git descibe --tags --abbrev=6 2>/dev/null | sed -e 's/-/~/g')
RELEASE        := 1
RPM_SPEC     := demo.spec

.PHONY: clean rpm test

clean:
    rm -rf build demo.egg-info *~ *.swp *.pyc
    find . -type d --name __pycache__ -exec rm -rf {} +

rpm:
  mkdir -p ${RPM_TOP_DIR}/SPES
  mkdir -p ${RPM_TOP_DIR}/SOURCES
  cp ${RPM_SPEC} ${RPM_TOP_DIR}/SPECS/
  tar --exclude .git --exclude .venv --exclude env --exclude build --exclude *.egg-info -cvzf ${RPM_TOP_DIR}/SOURCES/${OBJECT_NAME}-${VERSION}.tar.gz  -C ${APP_DIR_PATH}/ ./;

  rpmbuild --bb --define "_version ${VERSION}" --define "_release ${RELEASE}" ${PRM_TOP_DIR}/SPECS/${RPM_SPEC}

demo.spec示例

Name: demo
Version:1.0.0
Release:2%{?dist}
Summary: summary
BuildArch: noarch
Group: application program/Base
License: 
URL: http://example.com
Source: %{name}-%{version}.tar.gz

BuildRequires: gcc gcc-c++
BuildRequires: python

Requires: sudo
Requires: thrift

%define _python_bytecompile_errors_terminate_build 0
%define _unpackaged_files_terminate_build 0
%define _binaries_in_noarch_packages_terminate_build 0

%define __python %{_bindir}/python
%define _objdir %{_bindir}/demo/%{name}
%define _confdir %{_sysconfdir}/%{name}
%define logdir /var/log/%{name}

%define username demo
%define groupname demo

%define pypi_index https://mirrors.ustc.edu.cn/pypi/web/simple

%description

%prep
%setup -c -n %{name}-%{version}

%build
rm -rf %{objdir}/.venv
%{__python} -m pip install virtualenv -i %{pypi_index}
%{__python} -m pip install --upgrade pip -i %{pypi_index}
%{__python} -m pip install --upgrade setuptools -i %{pypi_index}
virtualenv --python=%{__python} %{objdir}/.venv
%{objdir}/.venv/bin/python setup.py build

% install
rm -rf %{buildroot}
# install
%{objdir}/.venv/bin/python setup.py install
mkdir -p %{buildroot}%{_bindir}
ln -s %{objdir}/.venv/bin/%{name} %{buildroot}%{_bindir}/%{name}
# .venv
mkdir -p %{buildroot}%{objdir}
cp -r %{objdir}/.venv %{buildroot}%{objdir}
# config
mkdir -p %{buildroot}%{confdir}
mkdir -p %{buildroot}%{_sysconfdir}/log.d
cp -f config/demo.conf %{buildroot}%{confdir}
cp -f config/demo.log %{buildroot}%{_sysconfdir}/log.d
# log path
mkdir -p %{buildroot}%{logdir}

%pre
# 升级钱配置操作,如数据备份等

%post
grep -r "demo" /etc/sudoers > /dev/null 2>&1
if [ $? -ne 0 ];then
  echo "demo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
fi

%preun
if test $1 = 0;then
  sed -i '/demo/d' /etc/sudoers
fi

%clean
rm -f %{buildroot}

%files
%defattr(-,%{username},%{groupname},-)
%{logdir}
%{_binddir}/%{name}
%{objdir}/
%config(noreplace) %{confdir}/demo.conf
%config %{_sysconfdir}/log.d/demo.logtate

%changelog


posted @ 2022-12-18 20:15  phper-liunian  阅读(233)  评论(0编辑  收藏  举报