Blame SOURCES/macros.python-srpm

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