5b578a8
# nb: $RPM_BUILD_ROOT is not set when the macros are expanded (at spec parse time)
5b578a8
#     so we set it manually (to empty string), making our Python prefer the correct install scheme location
Owen W. Taylor 546e9a3
# platbase/base is explicitly set to %%{_prefix} to support custom values, such as /app for flatpaks
Owen W. Taylor 546e9a3
%python3_sitelib %(RPM_BUILD_ROOT= %{__python3} -Ic "import sysconfig; print(sysconfig.get_path('purelib', vars={'platbase': '%{_prefix}', 'base': '%{_prefix}'}))")
Owen W. Taylor 546e9a3
%python3_sitearch %(RPM_BUILD_ROOT= %{__python3} -Ic "import sysconfig; print(sysconfig.get_path('platlib', vars={'platbase': '%{_prefix}', 'base': '%{_prefix}'}))")
5b578a8
%python3_version %(RPM_BUILD_ROOT= %{__python3} -Ic "import sys; sys.stdout.write('{0.major}.{0.minor}'.format(sys.version_info))")
5b578a8
%python3_version_nodots %(RPM_BUILD_ROOT= %{__python3} -Ic "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))")
5b578a8
%python3_platform %(RPM_BUILD_ROOT= %{__python3} -Ic "import sysconfig; print(sysconfig.get_platform())")
5b578a8
%python3_platform_triplet %(RPM_BUILD_ROOT= %{__python3} -Ic "import sysconfig; print(sysconfig.get_config_var('MULTIARCH'))")
5b578a8
%python3_ext_suffix %(RPM_BUILD_ROOT= %{__python3} -Ic "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))")
4085ef4
%python3_cache_tag %(RPM_BUILD_ROOT= %{__python3} -Ic "import sys; print(sys.implementation.cache_tag)")
Orion Poplawski ec28c77
%py3dir %{_builddir}/python3-%{name}-%{version}-%{release}
Orion Poplawski ec28c77
4d31ea8
%py3_shbang_opts -s%(RPM_BUILD_ROOT= %{__python3} -Ic "import sys; print('P' if hasattr(sys.flags, 'safe_path') else '')")
e9f07b7
%py3_shbang_opts_nodash %(opts=%{py3_shbang_opts}; echo ${opts#-})
7237192
%py3_shebang_flags %(opts=%{py3_shbang_opts}; echo ${opts#-})
1979a78
%py3_shebang_fix %{expand:\\\
1979a78
  if [ -f /usr/bin/pathfix%{python3_version}.py ]; then
1979a78
    pathfix=/usr/bin/pathfix%{python3_version}.py
1979a78
  else
1979a78
    # older versions of Python don't have it and must BR /usr/bin/pathfix.py from python3-devel explicitly
1979a78
    pathfix=/usr/bin/pathfix.py
1979a78
  fi
e5429a7
  if [ -z "%{?py3_shebang_flags}" ]; then
e5429a7
    shebang_flags="-k"
e5429a7
  else
e5429a7
    shebang_flags="-ka%{py3_shebang_flags}"
e5429a7
  fi
e5429a7
  $pathfix -pni %{__python3} $shebang_flags}
Orion Poplawski ec28c77
Orion Poplawski 5ca1f52
# Use the slashes after expand so that the command starts on the same line as
Orion Poplawski 5ca1f52
# the macro
Orion Poplawski 5ca1f52
%py3_build() %{expand:\\\
4904408
  CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
4904408
  %{__python3} %{py_setup} %{?py_setup_args} build --executable="%{__python3} %{py3_shbang_opts}" %{?*}
Orion Poplawski ec28c77
}
Orion Poplawski ec28c77
Orion Poplawski 5ca1f52
%py3_build_egg() %{expand:\\\
4904408
  CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
4904408
  %{__python3} %{py_setup} %{?py_setup_args} bdist_egg %{?*}
Orion Poplawski e7af332
}
Orion Poplawski e7af332
Orion Poplawski 5ca1f52
%py3_build_wheel() %{expand:\\\
4904408
  CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
4904408
  %{__python3} %{py_setup} %{?py_setup_args} bdist_wheel %{?*}
Orion Poplawski f798c43
}
Orion Poplawski f798c43
Orion Poplawski 5ca1f52
%py3_install() %{expand:\\\
4904408
  CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
Owen W. Taylor 546e9a3
  %{__python3} %{py_setup} %{?py_setup_args} install -O1 --skip-build --root %{buildroot} --prefix %{_prefix} %{?*}
69b1b30
  rm -rfv %{buildroot}%{_bindir}/__pycache__
Orion Poplawski ec28c77
}
Orion Poplawski f798c43
Orion Poplawski 5ca1f52
%py3_install_egg() %{expand:\\\
Orion Poplawski f798c43
  mkdir -p %{buildroot}%{python3_sitelib}
69b1b30
  %{__python3} -m easy_install -m --prefix %{buildroot}%{_prefix} -Z dist/*-py%{python3_version}.egg %{?*}
2314fd9
  rm -rfv %{buildroot}%{_bindir}/__pycache__
Orion Poplawski f798c43
}
Orion Poplawski e7af332
Orion Poplawski 5ca1f52
%py3_install_wheel() %{expand:\\\
Owen W. Taylor 546e9a3
  %{__python3} -m pip install -I dist/%{1} --root %{buildroot} --prefix %{_prefix} --no-deps --no-index --no-warn-script-location
2314fd9
  rm -rfv %{buildroot}%{_bindir}/__pycache__
daf7d32
  for distinfo in %{buildroot}%{python3_sitelib}/*.dist-info %{buildroot}%{python3_sitearch}/*.dist-info; do
daf7d32
    if [ -f ${distinfo}/direct_url.json ]; then
daf7d32
      rm -fv ${distinfo}/direct_url.json
daf7d32
      sed -i '/direct_url.json/d' ${distinfo}/RECORD
daf7d32
    fi
daf7d32
  done
Orion Poplawski a73eb24
}
6c63a5b
c2305ea
# With $PATH and $PYTHONPATH set to the %%buildroot,
2d0673a
# try to import the Python 3 module(s) given as command-line args or read from file (-f).
824ef3d
# Respect the custom values of %%py3_shebang_flags or set nothing if it's undefined.
2d0673a
# Filter and check import on only top-level modules using -t flag.
2d0673a
# Exclude unwanted modules by passing their globs to -e option.
c2305ea
# Useful as a smoke test in %%check when running tests is not feasible.
2d0673a
# Use spaces or commas as separators if providing list directly.
2d0673a
# Use newlines as separators if providing list in a file.
2d0673a
%py3_check_import(e:tf:) %{expand:\\\
c2305ea
  PATH="%{buildroot}%{_bindir}:$PATH"\\\
c2305ea
  PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}"\\\
9d81ad4
  _PYTHONSITE="%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}"\\\
c2305ea
  PYTHONDONTWRITEBYTECODE=1\\\
b20d8aa
  %{lua:
824ef3d
  local command = "%{__python3} "
824ef3d
  if rpm.expand("%{?py3_shebang_flags}") ~= "" then
824ef3d
    command = command .. "-%{py3_shebang_flags}"
824ef3d
  end
824ef3d
  command = command .. " %{_rpmconfigdir}/redhat/import_all_modules.py "
b20d8aa
  -- handle multiline arguments correctly, see https://bugzilla.redhat.com/2018809
824ef3d
  local args=rpm.expand('%{?**}'):gsub("[%s\\\\]*%s+", " ")
824ef3d
  print(command .. args)
824ef3d
  }
824ef3d
}
c2305ea
6c63a5b
# This only supports Python 3.5+ and will never work with Python 2.
6c63a5b
# Hence, it has no Python version in the name.
6c63a5b
%pycached() %{lua:
985a805
  path = rpm.expand("%{?*}")
6c63a5b
  if (string.sub(path, "-3") ~= ".py") then
6c63a5b
    rpm.expand("%{error:%%pycached can only be used with paths explicitly ending with .py}")
6c63a5b
  else
6c63a5b
    print(path)
6c63a5b
    pyminor = path:match("/python3.(%d+)/") or "*"
6c63a5b
    dirname = path:match("(.*/)")
6c63a5b
    modulename = path:match(".*/([^/]+).py")
4085ef4
    -- %%python3_cache_tag is not used here because this macro supports not-installed CPythons
6c63a5b
    print("\\n" .. dirname .. "__pycache__/" .. modulename .. ".cpython-3" .. pyminor .. "{,.opt-?}.pyc")
6c63a5b
  end
6c63a5b
}
0d3f1e6
0d3f1e6
# This is intended for Python 3 only, hence also no Python version in the name.
0253654
%__pytest /usr/bin/pytest%(test %{python3_pkgversion} == 3 || echo -%{python3_version})
0d3f1e6
%pytest %{expand:\\\
0d3f1e6
  CFLAGS="${CFLAGS:-${RPM_OPT_FLAGS}}" LDFLAGS="${LDFLAGS:-${RPM_LD_FLAGS}}"\\\
0d3f1e6
  PATH="%{buildroot}%{_bindir}:$PATH"\\\
0d3f1e6
  PYTHONPATH="${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}}"\\\
0d3f1e6
  PYTHONDONTWRITEBYTECODE=1\\\
d905710
  %{?__pytest_addopts:PYTEST_ADDOPTS="${PYTEST_ADDOPTS:-} %{__pytest_addopts}"}\\\
0d3f1e6
  %__pytest}