#32 EPEL7: Introduce %py3_check_import
Merged 2 years ago by kevin. Opened 2 years ago by churchyard.
rpms/ churchyard/epel-rpm-macros epel7-py3_check_import  into  epel7

file modified
+4 -1
@@ -1,6 +1,6 @@ 

  Name:           epel-rpm-macros

  Version:        7

- Release:        30

+ Release:        31

  Summary:        Extra Packages for Enterprise Linux RPM macros

  

  Group:          System Environment/Base
@@ -54,6 +54,9 @@ 

  

  

  %changelog

+ * Wed Jul 14 2021 Miro Hrončok <mhroncok@redhat.com> - 7-31

+ - Introduce %%py3_check_import

+ 

  * Fri Apr 23 2021 Miro Hrončok <mhroncok@redhat.com> - 7-30

  - Make %%pycached compatible with RPM < 4.13

  - Fixes: rhbz#1952704

file modified
+37
@@ -115,6 +115,43 @@ 

      end

  }

  

+ # With $PATH and $PYTHONPATH set to the %%buildroot,

+ # try to import the given Python module(s).

+ # Useful as a smoke test in %%check when running tests is not feasible.

+ # Use spaces or commas as separators.

+ %py_check_import() %{expand:\\\

+   (cd %{_topdir} &&\\\

+   PATH="%{buildroot}%{_bindir}:$PATH"\\\

+   PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python_sitearch}:%{buildroot}%{python_sitelib}}"\\\

+   PYTHONDONTWRITEBYTECODE=1\\\

+   %{__python} -c "import %{lua:local m=rpm.expand('%{?*}'):gsub('[%s,]+', ', ');print(m)}"

+   )

+ }

+ %py2_check_import() %{expand:\\\

+   (cd %{_topdir} &&\\\

+   PATH="%{buildroot}%{_bindir}:$PATH"\\\

+   PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python2_sitearch}:%{buildroot}%{python2_sitelib}}"\\\

+   PYTHONDONTWRITEBYTECODE=1\\\

+   %{__python2} -c "import %{lua:local m=rpm.expand('%{?*}'):gsub('[%s,]+', ', ');print(m)}"

+   )

+ }

+ %py3_check_import() %{expand:\\\

+   (cd %{_topdir} &&\\\

+   PATH="%{buildroot}%{_bindir}:$PATH"\\\

+   PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}"\\\

+   PYTHONDONTWRITEBYTECODE=1\\\

+   %{__python3} -c "import %{lua:local m=rpm.expand('%{?*}'):gsub('[%s,]+', ', ');print(m)}"

+   )

+ }

+ %py3_other_check_import() %{expand:\\\

+   (cd %{_topdir} &&\\\

+   PATH="%{buildroot}%{_bindir}:$PATH"\\\

+   PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_other_sitearch}:%{buildroot}%{python3_other_sitelib}}"\\\

+   PYTHONDONTWRITEBYTECODE=1\\\

+   %{__python3_other} -c "import %{lua:local m=rpm.expand('%{?*}'):gsub('[%s,]+', ', ');print(m)}"

+   )

+ }

+ 

  # Backport from 4.15

  %_smp_build_ncpus %([ -z "$RPM_BUILD_NCPUS" ] \\\

    && RPM_BUILD_NCPUS="`/usr/bin/getconf _NPROCESSORS_ONLN`"; \\\

With $PATH and $PYTHONPATH set to the %buildroot,
the macro tries to import the given Python 3 module(s).
Useful as a smoke test in %check when ruining tests is not feasible.
Accepts spaces or commas as separators.

Backport of https://src.fedoraproject.org/rpms/python-rpm-macros/c/c2305ea3

Tested on adapted python-toml package with:

%check
%py_check_import toml
%py2_check_import toml
%py3_check_import toml
%py3_other_check_import toml

Results in:

Executing(%check): /bin/sh -e /var/tmp/rpm-tmp.Q3oOk0
+ umask 022
+ cd /builddir/build/BUILD
+ cd toml-0.9.4
+ cd /builddir/build
+ PATH=/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
+ PYTHONPATH=/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/lib64/python2.7/site-packages:/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/lib/python2.7/site-packages
+ PYTHONDONTWRITEBYTECODE=1
+ /usr/bin/python -c 'import toml'
+ cd /builddir/build
+ PATH=/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
+ PYTHONPATH=/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/lib64/python2.7/site-packages:/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/lib/python2.7/site-packages
+ PYTHONDONTWRITEBYTECODE=1
+ /usr/bin/python2 -c 'import toml'
+ cd /builddir/build
+ PATH=/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
+ PYTHONPATH=/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/lib64/python3.6/site-packages:/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/lib/python3.6/site-packages
+ PYTHONDONTWRITEBYTECODE=1
+ /usr/bin/python3 -c 'import toml'
+ cd /builddir/build
+ PATH=/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
+ PYTHONPATH=/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/lib64/python3.4/site-packages:/builddir/build/BUILDROOT/python-toml-0.9.4-4.el7.x86_64/usr/lib/python3.4/site-packages
+ PYTHONDONTWRITEBYTECODE=1
+ /usr/bin/python3.4 -c 'import toml'
+ exit 0

That looks good. Also, fails with errors (each tested separately, as they terminate the build):

%py_check_import yaml

...
+ /usr/bin/python -c 'import yaml'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named yaml


%py2_check_import yaml

...
+ /usr/bin/python2 -c 'import yaml'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named yaml


%py3_check_import yaml

...
+ /usr/bin/python3 -c 'import yaml'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'yaml'


%py3_other_check_import yaml

...
+ /usr/bin/python3.4 -c 'import yaml'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'yaml'

Also, treats multiple arguments fine:

<mock-chroot> sh-4.2$ rpm --eval '%py3_check_import a b,,c, d  e     f'
\
  (cd /builddir/build &&\
  ...
  /usr/bin/python3 -c "import a, b, c, d, e, f"
  )

Pull-Request has been merged by kevin

2 years ago