5f3e4d6
# There are multiple Python 3 versions packaged, but only one can be the "main" version
5f3e4d6
# That means that it owns the "python3" namespace:
5f3e4d6
#     - python3 package name
5f3e4d6
#     - /usr/bin/python3 command
5f3e4d6
#     - python3-foo packages are meant for this version
5f3e4d6
# Other versions of Python 3 always contain the version in the namespace:
5f3e4d6
#     - python3.XX package name
5f3e4d6
#     - /usr/bin/python3.XX command
5f3e4d6
#     - python3.XX-foo packages (if allowed)
5f3e4d6
#
5f3e4d6
# Python spec files use the version defined here to determine defaults for the
5f3e4d6
# %%py_provides and %%python_provide macros, as well as for the "pythonname" generator that
5f3e4d6
# provides python3-foo for python3.XX-foo and vice versa for the default "main" version.
5919708
# E.g. in Fedora 33, python3.9-foo will provide python3-foo,
5919708
#                    python3-foo will provide python3.9-foo.
5f3e4d6
#
5f3e4d6
# There are two macros:
5f3e4d6
#
5f3e4d6
# This always contains the major.minor version (with dots), default for %%python3_version.
3c21a3a
%__default_python3_version 3.12
5f3e4d6
#
5919708
# The pkgname version that determines the alternative provide name (e.g. python3.9-foo),
5f3e4d6
# set to the same as above, but historically hasn't included the dot.
5f3e4d6
# This is left intentionally a separate macro, in case the naming convention ever changes.
5f3e4d6
%__default_python3_pkgversion %__default_python3_version
1b3e731
2b43f89
# python3_pkgversion specifies the version of Python 3 in the distro.
2b43f89
# For Fedora, this is usually just "3".
2b43f89
# It can be a specific version distro-wide (e.g. "36" in EPEL7).
2b43f89
# Alternatively, it can be overridden in spec (e.g. to "3.8") when building for alternate Python stacks.
Orion Poplawski 49ccad9
%python3_pkgversion 3
Orion Poplawski 49ccad9
b55e615
# Define the Python interpreter paths in the SRPM macros so that
b55e615
# - they can be used in Build/Requires
b55e615
# - they can be used in non-Python packages where requiring pythonX-devel would
b55e615
#   be an overkill
b55e615
b55e615
# use the underscored macros to redefine the behavior of %%python3_version etc.
b55e615
%__python2 /usr/bin/python2
a8b2654
%__python3 /usr/bin/python%{python3_pkgversion}
b55e615
b55e615
# use the non-underscored macros to refer to Python in spec, etc.
b55e615
%python2 %__python2
b55e615
%python3 %__python3
b55e615
b55e615
# See https://fedoraproject.org/wiki/Changes/PythonMacroError
b55e615
%__python %{error:attempt to use unversioned python, define %%__python to %{__python2} or %{__python3} explicitly}
b55e615
b55e615
# Users can use %%python only if they redefined %%__python (e.g. to %%__python3)
b55e615
%python %__python
b55e615
9b797df
# Define where Python wheels will be stored and the prefix of -wheel packages
9b797df
# - In Fedora we want wheel subpackages named e.g. `python-pip-wheel` that
9b797df
#   install packages into `/usr/share/python-wheels`. Both names are not
9b797df
#   versioned, because they're used by all Python 3 stacks.
9b797df
# - In RHEL we want wheel packages named e.g. `python3-pip-wheel` and
9b797df
#   `python3.11-pip-wheel` that install packages into similarly versioned
9b797df
#   locations. We want each Python stack in RHEL to have their own wheels,
9b797df
#   because the main python3 wheels (which we can't upgrade) will likely be
9b797df
#   quite old by the time we're adding new alternate Python stacks.
9b797df
# - In ELN we want to follow Fedora, because builds for ELN and Fedora rawhide
9b797df
#   need to be interoperable.
9b797df
%python_wheel_pkg_prefix python%{?rhel:%{!?eln:%{python3_pkgversion}}}
9b797df
%python_wheel_dir %{_datadir}/%{python_wheel_pkg_prefix}-wheels
9b797df
bc2b51d
fd3dc4c
### BRP scripts (and related macros)
fd3dc4c
fd3dc4c
## Automatically compile python files
fd3dc4c
%py_auto_byte_compile 1
fd3dc4c
## Should python bytecompilation errors terminate a build?
fd3dc4c
%_python_bytecompile_errors_terminate_build 1
fd3dc4c
## Should python bytecompilation compile outside python specific directories?
fd3dc4c
## This always causes errors when enabled, see https://fedoraproject.org/wiki/Changes/No_more_automagic_Python_bytecompilation_phase_3
fd3dc4c
%_python_bytecompile_extra 0
eb7a4fd
## Helper macro to unset $SOURCE_DATE_EPOCH if %%clamp_mtime_to_source_date_epoch is not set
eb7a4fd
## https://fedoraproject.org/wiki/Changes/ReproducibleBuildsClampMtimes#Python_bytecode
eb7a4fd
%__env_unset_source_date_epoch_if_not_clamp_mtime %[0%{?clamp_mtime_to_source_date_epoch} == 0 ? "env -u SOURCE_DATE_EPOCH" : "env"]
fd3dc4c
fd3dc4c
## The individual BRP scripts
10e0e53
%__brp_python_bytecompile %{__env_unset_source_date_epoch_if_not_clamp_mtime} %{_rpmconfigdir}/redhat/brp-python-bytecompile "" "%{?_python_bytecompile_errors_terminate_build}" "%{?_python_bytecompile_extra}" "%{?_smp_build_ncpus:-j%{_smp_build_ncpus}}"
fd3dc4c
%__brp_fix_pyc_reproducibility %{_rpmconfigdir}/redhat/brp-fix-pyc-reproducibility
bc2b51d
%__brp_python_hardlink %{_rpmconfigdir}/redhat/brp-python-hardlink
bc2b51d
fd3dc4c
## This macro is included in redhat-rpm-config's %%__os_install_post
fd3dc4c
# Note that the order matters:
fd3dc4c
#  1. brp-python-bytecompile can create (or replace) pyc files
fd3dc4c
#  2. brp-fix-pyc-reproducibility can modify the pyc files from above
fd3dc4c
#  3. brp-python-hardlink de-duplicates identical pyc files
fd3dc4c
%__os_install_post_python \
fd3dc4c
    %{?py_auto_byte_compile:%{?__brp_python_bytecompile}} \
fd3dc4c
    %{?py_reproducible_pyc_path:%{?__brp_fix_pyc_reproducibility} "%{py_reproducible_pyc_path}"} \
fd3dc4c
    %{?__brp_python_hardlink} \
fd3dc4c
%{nil}
fd3dc4c
bc2b51d
123ad4b
# === Macros for Build/Requires tags using Python dist tags ===
123ad4b
# - https://fedoraproject.org/wiki/Changes/Automatic_Provides_for_Python_RPM_Packages
123ad4b
# - These macros need to be in macros.python-srpm, because BuildRequires tags
123ad4b
#   get rendered as runtime requires into the metadata of SRPMs.
123ad4b
123ad4b
# Converts Python dist name to a canonical format
123ad4b
%py_dist_name() %{lua:\
123ad4b
        name = rpm.expand("%{?1:%{1}}");\
59abe83
        canonical = string.gsub(string.lower(name), "[^%w%[%]]+", "-");\
123ad4b
        print(canonical);\
123ad4b
}
123ad4b
123ad4b
# Creates Python 2 dist tag(s) after converting names to canonical format
123ad4b
#   Needs to first put all arguments into a list, because invoking a different
03a1e3b
#   macro (%%py_dist_name) overwrites them
123ad4b
%py2_dist() %{lua:\
123ad4b
        args = {}\
123ad4b
        arg = 1\
123ad4b
        while (true) do\
123ad4b
                name = rpm.expand("%{?" .. arg .. ":%{" .. arg .. "}}");\
123ad4b
                if (name == nil or name == '') then\
123ad4b
                        break\
123ad4b
                end\
123ad4b
                args[arg] = name\
123ad4b
                arg = arg + 1\
123ad4b
        end\
123ad4b
        for arg, name in ipairs(args) do\
123ad4b
                canonical = rpm.expand("%py_dist_name " .. name);\
123ad4b
                print("python2dist(" .. canonical .. ") ");\
123ad4b
        end\
123ad4b
}
123ad4b
123ad4b
# Creates Python 3 dist tag(s) after converting names to canonical format
123ad4b
#   Needs to first put all arguments into a list, because invoking a different
03a1e3b
#   macro (%%py_dist_name) overwrites them
123ad4b
%py3_dist() %{lua:\
638f809
        python3_pkgversion = rpm.expand("%python3_pkgversion");\
123ad4b
        args = {}\
123ad4b
        arg = 1\
123ad4b
        while (true) do\
123ad4b
                name = rpm.expand("%{?" .. arg .. ":%{" .. arg .. "}}");\
123ad4b
                if (name == nil or name == '') then\
123ad4b
                        break\
123ad4b
                end\
123ad4b
                args[arg] = name\
123ad4b
                arg = arg + 1\
123ad4b
        end\
123ad4b
        for arg, name in ipairs(args) do\
123ad4b
                canonical = rpm.expand("%py_dist_name " .. name);\
638f809
                print("python" .. python3_pkgversion .. "dist(" .. canonical .. ") ");\
123ad4b
        end\
123ad4b
}
123ad4b
37a004e
# Macro to replace overly complicated references to PyPI source files.
37a004e
# Expands to the pythonhosted URL for a package
37a004e
# Accepts zero to three arguments:
03a1e3b
# 1:  The PyPI project name, defaulting to %%srcname if it is defined, then
03a1e3b
#     %%pypi_name if it is defined, then just %%name.
03a1e3b
# 2:  The PYPI version, defaulting to %%version with tildes stripped.
37a004e
# 3:  The file extension, defaulting to "tar.gz".  (A period will be added
37a004e
#     automatically.)
03a1e3b
# Requires %%__pypi_url and %%__pypi_default_extension to be defined.
37a004e
%__pypi_url https://files.pythonhosted.org/packages/source/
37a004e
%__pypi_default_extension tar.gz
37a004e
37a004e
%pypi_source() %{lua:
37a004e
    local src = rpm.expand('%1')
37a004e
    local ver = rpm.expand('%2')
37a004e
    local ext = rpm.expand('%3')
37a004e
    local url = rpm.expand('%__pypi_url')
37a004e
\
37a004e
    -- If no first argument, try %srcname, then %pypi_name, then %name
37a004e
    -- Note that rpm leaves macros unchanged if they are not defined.
37a004e
    if src == '%1' then
37a004e
        src = rpm.expand('%srcname')
37a004e
    end
37a004e
    if src == '%srcname' then
37a004e
        src = rpm.expand('%pypi_name')
37a004e
    end
37a004e
    if src == '%pypi_name' then
37a004e
        src = rpm.expand('%name')
37a004e
    end
37a004e
\
37a004e
    -- If no second argument, use %version
37a004e
    if ver == '%2' then
4569c61
        ver = rpm.expand('%version'):gsub('~', '')
37a004e
    end
37a004e
\
37a004e
    -- If no third argument, use the preset default extension
37a004e
    if ext == '%3' then
37a004e
        ext = rpm.expand('%__pypi_default_extension')
37a004e
    end
37a004e
\
37a004e
    local first = string.sub(src, 1, 1)
37a004e
\
37a004e
    print(url .. first .. '/' .. src .. '/' .. src .. '-' .. ver .. '.' .. ext)
37a004e
}
8fea79b
8fea79b
%py_provides() %{lua:
8fea79b
    local python = require 'fedora.srpm.python'
5d7727c
    local rhel = rpm.expand('%{?rhel}')
8fea79b
    local name = rpm.expand('%1')
8fea79b
    if name == '%1' then
8fea79b
        rpm.expand('%{error:%%py_provides requires at least 1 argument, the name to provide}')
8fea79b
    end
8fea79b
    local evr = rpm.expand('%2')
8fea79b
    if evr == '%2' then
8fea79b
        evr = rpm.expand('%{?epoch:%{epoch}:}%{version}-%{release}')
8fea79b
    end
8fea79b
    print('Provides: ' .. name .. ' = ' .. evr .. '\\n')
5fe9747
    local provides = python.python_altprovides(name, evr)
5fe9747
    for i, provide in ipairs(provides) do
5fe9747
        print('Provides: ' .. provide .. '\\n')
8fea79b
    end
5d7727c
    -- We only generate these Obsoletes on CentOS/RHEL to provide clean upgrade
5d7727c
    -- path, e.g. python3-foo obsoletes python3.9-foo from previous RHEL.
5d7727c
    -- In Fedora this is not needed as we don't ship ecosystem packages
5d7727c
    -- for alternative Python interpreters.
5d7727c
    if rhel ~= '' then
e250f28
        -- Create Obsoletes only if the name does not end in a parenthesis,
e250f28
        -- as Obsoletes can't include parentheses.
e250f28
        -- This most commonly happens when the name contains an isa.
e250f28
        if (string.sub(name, "-1") ~= ")") then
e250f28
            local obsoletes = python.python_altobsoletes(name, evr)
e250f28
            for i, obsolete in ipairs(obsoletes) do
e250f28
                print('Obsoletes: ' .. obsolete .. '\\n')
e250f28
            end
5d7727c
        end
5d7727c
    end
8fea79b
}
763d24c
763d24c
%python_extras_subpkg(n:i:f:F) %{expand:%{lua:
763d24c
    local option_n = '-n (name of the base package)'
763d24c
    local option_i = '-i (buildroot path to metadata)'
763d24c
    local option_f = '-f (builddir path to a filelist)'
763d24c
    local option_F = '-F (skip %%files section)'
763d24c
    local value_n = rpm.expand('%{-n*}')
763d24c
    local value_i = rpm.expand('%{-i*}')
763d24c
    local value_f = rpm.expand('%{-f*}')
763d24c
    local value_F = rpm.expand('%{-F}')
763d24c
    local args = rpm.expand('%{*}')
763d24c
    if value_n == '' then
763d24c
        rpm.expand('%{error:%%%0: missing option ' .. option_n .. '}')
763d24c
    end
763d24c
    if value_i == '' and value_f == '' and value_F == '' then
763d24c
        rpm.expand('%{error:%%%0: missing option ' .. option_i .. ' or ' .. option_f .. ' or ' .. option_F .. '}')
763d24c
    end
763d24c
    if value_i ~= '' and value_f ~= '' then
763d24c
        rpm.expand('%{error:%%%0: simultaneous ' .. option_i .. ' and ' .. option_f .. ' options are not possible}')
763d24c
    end
763d24c
    if value_i ~= '' and value_F ~= '' then
763d24c
        rpm.expand('%{error:%%%0: simultaneous ' .. option_i .. ' and ' .. option_F .. ' options are not possible}')
763d24c
    end
763d24c
    if value_f ~= '' and value_F ~= '' then
763d24c
        rpm.expand('%{error:%%%0: simultaneous ' .. option_f .. ' and ' .. option_F .. ' options are not possible}')
763d24c
    end
763d24c
    if args == '' then
763d24c
        rpm.expand('%{error:%%%0 requires at least one argument with "extras" name}')
763d24c
    end
763d24c
    local requires = 'Requires: ' .. value_n .. ' = %{?epoch:%{epoch}:}%{version}-%{release}'
a44ae31
    for extras in args:gmatch('[^%s,]+') do
763d24c
        local rpmname = value_n .. '+' .. extras
763d24c
        local pkgdef = '%package -n ' .. rpmname
763d24c
        local summary = 'Summary: Metapackage for ' .. value_n .. ': ' .. extras .. ' extras'
c746b25
        local description = '%description -n ' .. rpmname .. '\\\n'
c746b25
        local current_line = 'This is a metapackage bringing in'
c746b25
        for _, word in ipairs({extras, 'extras', 'requires', 'for', value_n .. '.'}) do
c746b25
          local line = current_line .. ' ' .. word
c746b25
          if line:len() > 79 then
c746b25
            description = description .. current_line .. '\\\n'
c746b25
            current_line = word
c746b25
          else
c746b25
            current_line = line
c746b25
          end
c746b25
        end
c746b25
        description = description .. current_line .. '\\\n' ..
bc016cb
                      'It makes sure the dependencies are installed.\\\n'
763d24c
        local files = ''
763d24c
        if value_i ~= '' then
763d24c
            files = '%files -n ' .. rpmname .. '\\\n' .. '%ghost ' .. value_i
763d24c
        elseif value_f ~= '' then
763d24c
            files = '%files -n ' .. rpmname .. ' -f ' .. value_f
763d24c
        end
763d24c
        for i, line in ipairs({pkgdef, summary, requires, description, files, ''}) do
763d24c
            print(line .. '\\\n')
763d24c
        end
763d24c
    end
763d24c
}}