> On 5 Mar 2022, at 19:56, Hartmut Goebel <h.goe...@goebel-consult.de> wrote:
> 
> Am 05.03.22 um 17:34 schrieb Barry Scott:
>> Have the RPM install all the pythone code and dependencies and also install 
>> a short script that
>> sets up PYTHONPATH, LD_LIBRARY_PATH, etc and execs the python3 <main>.py.
> The scripts are already created by entry-points. So basically this means to 
> reinvent the wheel. Or did I miss something?
> 
I see what are getting at - I did miss the something.
It is the setup.py that will create your entry point!

For my own understanding I turned one of my PyPI projects into an RPM.

The project source is on https://github.com/barry-scott/CLI-tools.git 
<https://github.com/barry-scott/CLI-tools.git>

First I turned the this into a tarball:

$ git archive --format=tar --prefix=cli-tools-3.1.0/ master >cli-tools-3.1.0.tar
$ gzip cli-tools-3.1.0.tar

Then I created a RPM spec file:

Summary:        cli-tools example packaging for python
Name:           cli-tools
Version:        3.1.0
Release:        1

BuildArch:      noarch
Source:         %{name}-%{version}.tar.gz
License:        APL2.0
Prefix:         /opt/barry-example

BuildRequires:  python3
BuildRequires:  python3-setuptools

Requires:       python3

%description
cli-tools example packaging for python

%prep
# unpack Source tarball
%setup

%build
cd Source
python3 setup_ssh_wait.py build

%install
cd Source
python3 setup_ssh_wait.py install --prefix=%{prefix} --root=%{buildroot}

# need to add the site-packages folder to the sys.path as
# setup.py assumes it is installing into the systems python library
cat <<EOF
import sys
import os

site_packages = 'python%d.%d/site-packages' % (sys.version_info.major, 
sys.version_info.minor)
extra_python_path = os.path.join(sys.argv[2], site_packages)

with open(sys.argv[1]) as f:
    script_lines = f.read().split('\n')

index = script_lines.index('import sys')
script_lines[index+1:index+1] = ['sys.path.insert(0, "%s")' % 
(extra_python_path,)]

with open(sys.argv[1]+'.new', 'w') as f:
    f.write('\n'.join(script_lines))
EOF >fix_bin.py

python3 fix_bin.py %{buildroot}%{prefix}/bin/ssh-wait 
%{buildroot}%{prefix}/opt/barry-example/lib

%files
/opt/barry-example/*

%changelog

Then only thing I needed to fix up was that the entry point code needs to have 
the
lib folder in /opt/barry-example added to sys.path. That is what the fix_bin.py 
is
doing.

I then use rpmbuild to create the RPM.

$ rpmbuild -bb cli-tools.spec

To use mock the steps are:

$ rpmbuild -bs cli-tools.spec
$ mock --rebuild --root fedora-35-x86_64 ../SRPMS/cli-tools-3.1.0.arc.rpm

Then I install the built file:

$ dnf install /var/lib/mock/fedora-35-x86_64/result/cli-tools-3.1.0-1.noarch.rpm

And then I can run the command:

$ /opt/barry-example/bin/ssh-wait
Usage: ssh-wait <options> <host>

ssh-wait will wait for the service to be available
on <host>.

    --verbose
        - print progress messages
    --nowait
        - check one and exit 0 if can connect 1 if cannot
    --wait-limit=<limit>
        - exit 1 if service is not avaiable within <limit> seconds.
          defaults to 600.
    --service=<service>
        - check the service <service>. Can be a service
          name of a port number. Default is ssh.

Barry

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to