beaa2ee
# Define the Python interpreter paths in the SRPM macros so that
beaa2ee
# - they can be used in Build/Requires
beaa2ee
# - they can be used in non-Python packages where requiring pythonX-devel would
beaa2ee
#   be an overkill
af35bb0
af35bb0
# use the underscored macros to redefine the behavior of %%python3_version etc.
beaa2ee
%__python2 /usr/bin/python2
beaa2ee
%__python3 /usr/bin/python3
beaa2ee
af35bb0
# use the non-underscored macros to refer to Python in spec, etc.
af35bb0
%python2 %__python2
af35bb0
%python3 %__python3
af35bb0
69b1b30
# See https://fedoraproject.org/wiki/Changes/PythonMacroError
69b1b30
%__python %{error:attempt to use unversioned python, define %%__python to %{__python2} or %{__python3} explicitly}
536b2ef
f0be0a2
# Users can use %%python only if they redefined %%__python (e.g. to %%__python3)
69b1b30
%python %__python
536b2ef
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.
3a654e3
%__default_python3_version 3.10
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
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'
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
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
}}
9ec9c1f
9ec9c1f
%version_no_operators() %{lua:
9ec9c1f
    local ver = rpm.expand('%1')
9ec9c1f
\
9ec9c1f
    if ver == '%1' then
9ec9c1f
        ver = rpm.expand('%version')
9ec9c1f
    end
9ec9c1f
    ver = ver:gsub('[~^]+', '')
9ec9c1f
\
9ec9c1f
    print(ver)
9ec9c1f
}