Blame SOURCES/macros.python-srpm

61e6f0
# python3_pkgversion specifies the version of Python 3 in the distro.  It can be
61e6f0
# a specific version (e.g. 34 in Fedora EPEL7)
61e6f0
%python3_pkgversion 3
61e6f0
61e6f0
# Set to /bin/true to avoid %ifdefs and %{? in specfiles
61e6f0
%__python3_other /bin/true
61e6f0
%py3_other_build /bin/true
61e6f0
%py3_other_install /bin/true
61e6f0
61e6f0
61e6f0
61e6f0
# === Macros for Build/Requires tags using Python dist tags ===
61e6f0
# - https://fedoraproject.org/wiki/Changes/Automatic_Provides_for_Python_RPM_Packages
61e6f0
# - These macros need to be in macros.python-srpm, because BuildRequires tags
61e6f0
#   get rendered as runtime requires into the metadata of SRPMs.
61e6f0
61e6f0
# Converts Python dist name to a canonical format
61e6f0
%py_dist_name() %{lua:\
61e6f0
        name = rpm.expand("%{?1:%{1}}");\
61e6f0
        canonical = string.gsub(string.lower(name), "[^%w%.]+", "-");\
61e6f0
        print(canonical);\
61e6f0
}
61e6f0
61e6f0
# Creates Python 2 dist tag(s) after converting names to canonical format
61e6f0
#   Needs to first put all arguments into a list, because invoking a different
61e6f0
#   macro (%py_dist_name) overwrites them
61e6f0
%py2_dist() %{lua:\
61e6f0
        args = {}\
61e6f0
        arg = 1\
61e6f0
        while (true) do\
61e6f0
                name = rpm.expand("%{?" .. arg .. ":%{" .. arg .. "}}");\
61e6f0
                if (name == nil or name == '') then\
61e6f0
                        break\
61e6f0
                end\
61e6f0
                args[arg] = name\
61e6f0
                arg = arg + 1\
61e6f0
        end\
61e6f0
        for arg, name in ipairs(args) do\
61e6f0
                canonical = rpm.expand("%py_dist_name " .. name);\
61e6f0
                print("python2dist(" .. canonical .. ") ");\
61e6f0
        end\
61e6f0
}
61e6f0
5764f1
# RHEL 9+ and Fedora compatibility macro
5764f1
# Only use in macro backports, not intended to be used in spec files!
5764f1
# In the future, the %%python3_pkgversion macro has a dot, e.g. 3.9 or 3.11
5764f1
# However, in RHEL 8 at least, it does not, e.g. 38, 39
5764f1
# This is a helpful macro that determines the proper "Python version" string with dot
5764f1
# from %%python3_pkgversion without actually having Python installed.
5764f1
# For values other than 3X, it should expand to %%python3_pkgversion unchanged.
5764f1
# Examples of %%python3_pkgversion -> %%_python3_pkgversion_with_dot:
5764f1
#   3    -> 3
5764f1
#   38   -> 3.8
5764f1
#   39   -> 3.9
5764f1
#   310  -> 3.10
5764f1
#   3.12 -> 3.12
5764f1
#   4    -> 4
5764f1
#   412  -> 412
5764f1
%_python3_pkgversion_with_dot %{lua:print((rpm.expand("%python3_pkgversion"):gsub('^3(%d)', '3.%1')))}
5764f1
61e6f0
# Creates Python 3 dist tag(s) after converting names to canonical format
61e6f0
#   Needs to first put all arguments into a list, because invoking a different
61e6f0
#   macro (%py_dist_name) overwrites them
61e6f0
%py3_dist() %{lua:\
5764f1
        python3_pkgversion_with_dot = rpm.expand("%_python3_pkgversion_with_dot")\
61e6f0
        args = {}\
61e6f0
        arg = 1\
61e6f0
        while (true) do\
61e6f0
                name = rpm.expand("%{?" .. arg .. ":%{" .. arg .. "}}");\
61e6f0
                if (name == nil or name == '') then\
61e6f0
                        break\
61e6f0
                end\
61e6f0
                args[arg] = name\
61e6f0
                arg = arg + 1\
61e6f0
        end\
61e6f0
        for arg, name in ipairs(args) do\
61e6f0
                canonical = rpm.expand("%py_dist_name " .. name);\
5764f1
                print("python" .. python3_pkgversion_with_dot .. "dist(" .. canonical .. ") ");\
61e6f0
        end\
61e6f0
}
61e6f0
61e6f0
# Macro to replace overly complicated references to PyPI source files.
61e6f0
# Expands to the pythonhosted URL for a package
61e6f0
# Accepts zero to three arguments:
61e6f0
# 1:  The PyPI project name, defaulting to %srcname if it is defined, then
61e6f0
#     %pypi_name if it is defined, then just %name.
fe4572
# 2:  The PYPI version, defaulting to %version with tildes stripped.
61e6f0
# 3:  The file extension, defaulting to "tar.gz".  (A period will be added
61e6f0
#     automatically.)
61e6f0
# Requires %__pypi_url and %__pypi_default_extension to be defined.
61e6f0
%__pypi_url https://files.pythonhosted.org/packages/source/
61e6f0
%__pypi_default_extension tar.gz
61e6f0
61e6f0
%pypi_source() %{lua:
61e6f0
    local src = rpm.expand('%1')
61e6f0
    local ver = rpm.expand('%2')
61e6f0
    local ext = rpm.expand('%3')
61e6f0
    local url = rpm.expand('%__pypi_url')
61e6f0
\
61e6f0
    -- If no first argument, try %srcname, then %pypi_name, then %name
61e6f0
    -- Note that rpm leaves macros unchanged if they are not defined.
61e6f0
    if src == '%1' then
61e6f0
        src = rpm.expand('%srcname')
61e6f0
    end
61e6f0
    if src == '%srcname' then
61e6f0
        src = rpm.expand('%pypi_name')
61e6f0
    end
61e6f0
    if src == '%pypi_name' then
61e6f0
        src = rpm.expand('%name')
61e6f0
    end
61e6f0
\
61e6f0
    -- If no second argument, use %version
61e6f0
    if ver == '%2' then
fe4572
        ver = rpm.expand('%version'):gsub('~', '')
61e6f0
    end
61e6f0
\
61e6f0
    -- If no third argument, use the preset default extension
61e6f0
    if ext == '%3' then
61e6f0
        ext = rpm.expand('%__pypi_default_extension')
61e6f0
    end
61e6f0
\
61e6f0
    local first = string.sub(src, 1, 1)
61e6f0
\
61e6f0
    print(url .. first .. '/' .. src .. '/' .. src .. '-' .. ver .. '.' .. ext)
61e6f0
}
80f01a
80f01a
# Python packages in RHEL 8 should not provide unversioned python- names
80f01a
# so this macro here is just a compatibility layer and only provides the given name.
80f01a
%py_provides() %{lua:
80f01a
    local name = rpm.expand('%1')
80f01a
    if name == '%1' then
80f01a
        rpm.expand('%{error:%%py_provides requires at least 1 argument, the name to provide}')
80f01a
    end
80f01a
    local evr = rpm.expand('%2')
80f01a
    if evr == '%2' then
80f01a
        evr = rpm.expand('%{?epoch:%{epoch}:}%{version}-%{release}')
80f01a
    end
80f01a
    print('Provides: ' .. name .. ' = ' .. evr .. '\\n')
80f01a
}