summaryrefslogtreecommitdiff
path: root/macros.python
diff options
context:
space:
mode:
authorCoprDistGit <infra@openeuler.org>2025-01-15 06:47:29 +0000
committerCoprDistGit <infra@openeuler.org>2025-01-15 06:47:29 +0000
commit9d624aa4e88e5eb4b0df90060dcd40e518a7458b (patch)
tree70292c74c2ab4db698615e7d823d9357eeaced7f /macros.python
parent1175fc575d157169d8592e937cba42181d0ec3f2 (diff)
automatic import of openEuler-rpm-config
Diffstat (limited to 'macros.python')
-rw-r--r--macros.python342
1 files changed, 342 insertions, 0 deletions
diff --git a/macros.python b/macros.python
new file mode 100644
index 0000000..59df735
--- /dev/null
+++ b/macros.python
@@ -0,0 +1,342 @@
+# Note that the path could itself be a python file, or a directory
+
+# Python's compile_all module only works on directories, and requires a max
+# recursion depth
+
+# Usage:
+# %py_byte_compile <interpereter> <path>
+# Example:
+# %py_byte_compile %{__python3} %{buildroot}%{_datadir}/spam/plugins/
+
+# This will terminate build on SyntaxErrors, if you want to avoid that,
+# use it in a subshell like this:
+# (%{py_byte_compile <interpereter> <path>}) || :
+
+%py_byte_compile()\
+python_binary="%1"\
+bytecode_compilation_path="%2"\
+find $bytecode_compilation_path -type f -a -name "*.py" -print0 | xargs -0 $python_binary -O -m py_compile\
+find $bytecode_compilation_path -type f -a -name "*.py" -print0 | xargs -0 $python_binary -m py_compile
+%py_setup setup.py
+%py_shbang_opts -s
+
+# Use the slashes after expand so that the command starts on the same line as
+# the macro
+%py_build() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python} %{py_setup} %{?py_setup_args} build --executable="%{__python} %{py_shbang_opts}" %{?*}
+ sleep 1
+}
+
+%pyproject_build() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python3} -mpip wheel --verbose --progress-bar off --disable-pip-version-check --use-pep517 --no-build-isolation --no-deps --wheel-dir ./build . %{?*}
+ sleep 1
+}
+
+%py_build_egg() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python} %{py_setup} %{?py_setup_args} bdist_egg %{?*}
+ sleep 1
+}
+
+%py_build_wheel() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python} %{py_setup} %{?py_setup_args} bdist_wheel %{?*}
+ sleep 1
+}
+
+%py_install() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python} %{py_setup} %{?py_setup_args} install -O1 --skip-build --root %{buildroot} %{?*}
+}
+
+%pyproject_install() %{expand:\\\
+ specifier=$(ls ./build/*.whl | xargs basename --multiple | sed -E 's/([^-]+)-([^-]+)-.+\\\.whl/\\\1==\\\2/')
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python3} -mpip install --verbose --progress-bar off --disable-pip-version-check --root %{buildroot} --no-compile --ignore-installed --no-deps --no-index --find-links ./build $specifier %{?*}
+}
+
+%py_install_egg() %{expand:\\\
+ mkdir -p %{buildroot}%{python_sitelib}
+ easy_install -m --prefix %{buildroot}%{_prefix} -Z dist/*-py%{python_version}.egg %{?*}
+}
+
+%py_install_wheel() %{expand:\\\
+ %{__python} -m pip install -I dist/%{1} --root %{buildroot} --prefix %{_prefix} --no-deps --no-index --no-warn-script-location
+ rm -rfv %{buildroot}%{_bindir}/__pycache__
+ for distinfo in %{buildroot}%{python_sitelib}/*.dist-info %{buildroot}%{python_sitearch}/*.dist-info; do
+ if [ -f ${distinfo}/direct_url.json ]; then
+ rm -fv ${distinfo}/direct_url.json
+ sed -i '/direct_url.json/d' ${distinfo}/RECORD
+ fi
+ done
+}
+
+%python_provide() %{lua:
+ function string.starts(String,Start)
+ return string.sub(String,1,string.len(Start))==Start
+ end
+ package = rpm.expand("%{?1}")
+ vr = rpm.expand("%{?epoch:%{epoch}:}%{version}-%{release}")
+ if (string.starts(package, "python2-")) then
+ --No unversioned provides as python2 is not default
+ elseif (string.starts(package, "python" .. rpm.expand("%{python3_pkgversion}") .. "-")) then
+ print("\\nProvides: python-")
+ l = 8 + string.len(rpm.expand("%{python3_pkgversion}"))
+ print(string.sub(package,l,string.len(package)))
+ print(" = ")
+ print(vr)
+ --Obsoleting the previous default python package (if it doesn't have isa)
+ if (string.sub(package, "-1") ~= ")") then
+ print("\\nObsoletes: python-")
+ print(string.sub(package,l,string.len(package)))
+ print(" < ")
+ print(vr)
+ end
+ elseif (rpm.expand("%{?python3_other_pkgversion}") ~= "" and string.starts(package, "python" .. rpm.expand("%{python3_other_pkgversion}") .. "-")) then
+ --No unversioned provides as python3_other is not default
+ elseif (string.starts(package, "pypy-")) then
+ --No unversioned provides as pypy is not default
+ elseif (string.starts(package, "pypy2-")) then
+ --No unversioned provides as pypy is not default
+ elseif (string.starts(package, "pypy3-")) then
+ --No unversioned provides as pypy is not default
+ elseif (string.starts(package, "python-")) then
+ --No unversioned provides needed for unversioned python
+ else
+ print("%python_provide: ERROR: ")
+ print(package)
+ print(" not recognized.")
+ end
+}
+
+%python_enable_dependency_generator() \
+%global __pythondist_requires %{_rpmconfigdir}/pythondistdeps.py --requires \
+%{nil}
+%__python2 /usr/bin/python2
+%python2_sitelib %(%{__python2} -Esc "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
+%python2_sitearch %(%{__python2} -Esc "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")
+%python2_version %(%{__python2} -Esc "import sys; sys.stdout.write('{0.major}.{0.minor}'.format(sys.version_info))")
+%python2_version_nodots %(%{__python2} -Esc "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))")
+
+%py2_shbang_opts -s
+
+# Use the slashes after expand so that the command starts on the same line as
+# the macro
+%py2_build() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python2} %{py_setup} %{?py_setup_args} build --executable="%{__python2} %{py2_shbang_opts}" %{?*}
+ sleep 1
+}
+
+%py2_build_egg() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python2} %{py_setup} %{?py_setup_args} bdist_egg %{?*}
+ sleep 1
+}
+
+%py2_build_wheel() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python2} %{py_setup} %{?py_setup_args} bdist_wheel %{?*}
+ sleep 1
+}
+
+%py2_install() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python2} %{py_setup} %{?py_setup_args} install -O1 --skip-build --root %{buildroot} %{?*}
+}
+
+%py2_install_egg() %{expand:\\\
+ mkdir -p %{buildroot}%{python2_sitelib}
+ easy_install-%{python2_version} -m --prefix %{buildroot}%{_prefix} -Z dist/*-py%{python2_version}.egg %{?*}
+}
+
+%py2_install_wheel() %{expand:\\\
+ pip%{python2_version} install -I dist/%{1} --root %{buildroot} --strip-file-prefix %{buildroot} --no-deps
+}
+%__python3 /usr/bin/python3
+%python3_sitelib %(%{__python3} -Ic "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
+%python3_sitearch %(%{__python3} -Ic "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")
+%python3_version %(%{__python3} -Ic "import sys; sys.stdout.write('{0.major}.{0.minor}'.format(sys.version_info))")
+%python3_version_nodots %(%{__python3} -Ic "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))")
+%python3_platform %(%{__python3} -Ic "import sysconfig; print(sysconfig.get_platform())")
+%py3dir %{_builddir}/python3-%{name}-%{version}-%{release}
+
+%py3_shbang_opts -s
+
+# Use the slashes after expand so that the command starts on the same line as
+# the macro
+%py3_build() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python3} %{py_setup} %{?py_setup_args} build --executable="%{__python3} %{py3_shbang_opts}" %{?*}
+ sleep 1
+}
+
+%py3_build_egg() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python3} %{py_setup} %{?py_setup_args} bdist_egg %{?*}
+ sleep 1
+}
+
+%py3_build_wheel() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python3} %{py_setup} %{?py_setup_args} bdist_wheel %{?*}
+ sleep 1
+}
+
+%py3_install() %{expand:\\\
+ CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
+ %{__python3} %{py_setup} %{?py_setup_args} install -O1 --skip-build --root %{buildroot} %{?*}
+}
+
+%py3_install_egg() %{expand:\\\
+ mkdir -p %{buildroot}%{python3_sitelib}
+ easy_install-%{python3_version} -m --prefix %{buildroot}%{_prefix} -Z dist/*-py%{python3_version}.egg %{?*}
+}
+
+%py3_install_wheel() %{expand:\\\
+ %{__python3} -m pip install -I dist/%{1} --root %{buildroot} --prefix %{_prefix} --no-deps --no-index --no-warn-script-location
+ rm -rfv %{buildroot}%{_bindir}/__pycache__
+ for distinfo in %{buildroot}%{python3_sitelib}/*.dist-info %{buildroot}%{python3_sitearch}/*.dist-info; do
+ if [ -f ${distinfo}/direct_url.json ]; then
+ rm -fv ${distinfo}/direct_url.json
+ sed -i '/direct_url.json/d' ${distinfo}/RECORD
+ fi
+ done
+}
+# python3_pkgversion specifies the version of Python 3 in the distro. It can be
+# a specific version (e.g. 34 in Fedora EPEL7)
+%python3_pkgversion 3
+
+# Set to /bin/true to avoid %ifdefs and %{? in specfiles
+%__python3_other /bin/true
+%py3_other_build /bin/true
+%py3_other_install /bin/true
+
+
+
+# === Macros for Build/Requires tags using Python dist tags ===
+# - https://fedoraproject.org/wiki/Changes/Automatic_Provides_for_Python_RPM_Packages
+# - These macros need to be in macros.python-srpm, because BuildRequires tags
+# get rendered as runtime requires into the metadata of SRPMs.
+
+# Converts Python dist name to a canonical format
+%py_dist_name() %{lua:\
+ name = rpm.expand("%{?1:%{1}}");\
+ canonical = string.gsub(string.lower(name), "[^%w%.]+", "-");\
+ print(canonical);\
+}
+
+# Creates Python 2 dist tag(s) after converting names to canonical format
+# Needs to first put all arguments into a list, because invoking a different
+# macro (%py_dist_name) overwrites them
+%py2_dist() %{lua:\
+ args = {}\
+ arg = 1\
+ while (true) do\
+ name = rpm.expand("%{?" .. arg .. ":%{" .. arg .. "}}");\
+ if (name == nil or name == '') then\
+ break\
+ end\
+ args[arg] = name\
+ arg = arg + 1\
+ end\
+ for arg, name in ipairs(args) do\
+ canonical = rpm.expand("%py_dist_name " .. name);\
+ print("python2dist(" .. canonical .. ") ");\
+ end\
+}
+
+# Creates Python 3 dist tag(s) after converting names to canonical format
+# Needs to first put all arguments into a list, because invoking a different
+# macro (%py_dist_name) overwrites them
+%py3_dist() %{lua:\
+ args = {}\
+ arg = 1\
+ while (true) do\
+ name = rpm.expand("%{?" .. arg .. ":%{" .. arg .. "}}");\
+ if (name == nil or name == '') then\
+ break\
+ end\
+ args[arg] = name\
+ arg = arg + 1\
+ end\
+ for arg, name in ipairs(args) do\
+ canonical = rpm.expand("%py_dist_name " .. name);\
+ print("python3dist(" .. canonical .. ") ");\
+ end\
+}
+
+# Macro to replace overly complicated references to PyPI source files.
+# Expands to the pythonhosted URL for a package
+# Accepts zero to three arguments:
+# 1: The PyPI project name, defaulting to %srcname if it is defined, then
+# %pypi_name if it is defined, then just %name.
+# 2: The PYPI version, defaulting to %version.
+# 3: The file extension, defaulting to "tar.gz". (A period will be added
+# automatically.)
+# Requires %__pypi_url and %__pypi_default_extension to be defined.
+%__pypi_url https://files.pythonhosted.org/packages/source/
+%__pypi_default_extension tar.gz
+
+%pypi_source() %{lua:
+ local src = rpm.expand('%1')
+ local ver = rpm.expand('%2')
+ local ext = rpm.expand('%3')
+ local url = rpm.expand('%__pypi_url')
+\
+ -- If no first argument, try %srcname, then %pypi_name, then %name
+ -- Note that rpm leaves macros unchanged if they are not defined.
+ if src == '%1' then
+ src = rpm.expand('%srcname')
+ end
+ if src == '%srcname' then
+ src = rpm.expand('%pypi_name')
+ end
+ if src == '%pypi_name' then
+ src = rpm.expand('%name')
+ end
+\
+ -- If no second argument, use %version
+ if ver == '%2' then
+ ver = rpm.expand('%version')
+ end
+\
+ -- If no third argument, use the preset default extension
+ if ext == '%3' then
+ ext = rpm.expand('%__pypi_default_extension')
+ end
+\
+ local first = string.sub(src, 1, 1)
+\
+ print(url .. first .. '/' .. src .. '/' .. src .. '-' .. ver .. '.' .. ext)
+}
+
+# New python macros
+%__python /usr/bin/python
+%python_sitelib %(%{__python} -c "from distutils.sysconfig import get_python_lib; import sys; sys.stdout.write(get_python_lib())")
+%python_sitearch %(%{__python} -c "from distutils.sysconfig import get_python_lib; import sys; sys.stdout.write(get_python_lib(1))")
+%python_version %(%{__python} -c "import sys; sys.stdout.write(sys.version[:3])")
+
+# New python test macros
+%pytest() %{expand:\\\
+CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}" \\
+PATH="%{buildroot}%{_bindir}:$PATH" \\
+PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}" \\
+PYTHONDONTWRITEBYTECODE=1 \\
+%{?__pytest_addopts:PYTEST_ADDOPTS="${PYTEST_ADDOPTS:-} %{__pytest_addopts}"} \\
+PYTEST_XDIST_AUTO_NUM_WORKERS=%{_smp_build_ncpus} \\
+%{_bindir}/pytest %{?*}
+}
+
+%tox() %{expand:\\\
+TOX_TESTENV_PASSENV="${TOX_TESTENV_PASSENV:-*}" \\
+CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}" \\
+PATH="%{buildroot}%{_bindir}:$PATH" \\
+PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}" \\
+PYTHONDONTWRITEBYTECODE=1 \\
+%{?__pytest_addopts:PYTEST_ADDOPTS="${PYTEST_ADDOPTS:-} %{__pytest_addopts}"} \\
+PYTEST_XDIST_AUTO_NUM_WORKERS=%{_smp_build_ncpus} \\
+%{__python3} -m tox -e py%{python3_version_nodots} --current-env %{?*}
+}