Blame SOURCES/macros.python3

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