我使用setup.py 来构建各种RPM。解决方案对我来说有点不同。我还认为它更强大有两个原因:
- 我可以显式覆盖文件的权限
- 我不需要知道用户的uid和gid。相反,我可以使用纯文本。
这是一个工作示例
from distutils.core import setup
import distutils.command.bdist_rpm
import distutils.command.install
version='13'
data_files = [
('/usr/share/blah', ['README', 'test.sh']),
]
permissions = [
('/usr/share/blah', 'test.sh', '(755, sri, sri)'),
]
class bdist_rpm(distutils.command.bdist_rpm.bdist_rpm):
def _make_spec_file(self):
spec = distutils.command.bdist_rpm.bdist_rpm._make_spec_file(self)
for path, files , perm in permissions:
##
# Add a line to the SPEC file to change the permissions of a
# specific file upon install.
#
# example:
# %attr(666, root, root) path/file
#
spec.extend(['%attr{} {}/{}'.format(perm, path, files)])
return spec
setup(name='sri-testme',
version=version,
description='This is garganbe and is only used to test the permision flag behavior',
author='Chris Gembarowski',
author_email='chrisg@summationresearch.com',
url='https://www.python.org/sigs/distutils-sig/',
data_files=data_files,
cmdclass={'bdist_rpm':bdist_rpm}
)
让我更详细地解释发生了什么。 RPM 是从 SPEC 文件构建的。 bdist_rpm 构建一个 SPEC 文件。在 SPEC 文件中,您可以通过提供 %attr 选项来选择文件的权限和所有权。
在使 test.sh 可执行并归用户“sri”所有的示例中,我将在 SPEC 文件的末尾添加 %attr(755, sri, sri)。
因此,当我覆盖 bdist_rpm._make_spec_file 的行为时,我所做的就是为每个要覆盖权限的文件添加一行。
此示例中的完整 SPEC 文件为:
%define name sri-testme
%define version 13
%define unmangled_version 13
%define release 1
Summary: This is garganbe and is only used to test the permision flag behavior
Name: %{name}
Version: %{version}
Release: %{release}
Source0: %{name}-%{unmangled_version}.tar.gz
License: UNKNOWN
Group: Development/Libraries
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot
Prefix: %{_prefix}
BuildArch: noarch
Vendor: Chris Gembarowski <chrisg@summationresearch.com>
Url: https://www.python.org/sigs/distutils-sig/
%description
UNKNOWN
%prep
%setup -n %{name}-%{unmangled_version}
%build
python setup.py build
%install
python setup.py install -O1 --root=$RPM_BUILD_ROOT --record=INSTALLED_FILES
%clean
rm -rf $RPM_BUILD_ROOT
%post
##
# sri will be turned on in the run-once script instead of here
#
%preun
#!/bin/bash
%files -f INSTALLED_FILES
%defattr(-,root,root)
%attr(755, sri, sri) /usr/share/blah/test.sh