Blame SOURCES/macros.python-srpm

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