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
cf8051e
# For backwards compatibility only
cf8051e
# See the comments in https://src.fedoraproject.org/rpms/python-rpm-macros/pull-request/22
cf8051e
%__python /usr/bin/python
536b2ef
f0be0a2
# Users can use %%python only if they redefined %%__python (e.g. to %%__python3)
f0be0a2
%python() %{lua:\
f0be0a2
    __python = rpm.expand("%__python")\
f0be0a2
    if __python == "/usr/bin/python" then\
f0be0a2
        rpm.expand("%{error:Cannot use %%python if %%__python wasn't redefined to something other than /usr/bin/python.}")\
f0be0a2
    else\
f0be0a2
        print(__python)\
f0be0a2
    end\
f0be0a2
}
536b2ef
Orion Poplawski 49ccad9
# python3_pkgversion specifies the version of Python 3 in the distro.  It can be
Orion Poplawski 49ccad9
# a specific version (e.g. 34 in Fedora EPEL7)
Orion Poplawski 49ccad9
%python3_pkgversion 3
Orion Poplawski 49ccad9
Orion Poplawski 49ccad9
# Set to /bin/true to avoid %ifdefs and %{? in specfiles
Orion Poplawski 49ccad9
%__python3_other /bin/true
Orion Poplawski 49ccad9
%py3_other_build /bin/true
Orion Poplawski 49ccad9
%py3_other_install /bin/true
123ad4b
123ad4b
123ad4b
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}}");\
6cc8000
        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
123ad4b
#   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
123ad4b
#   macro (%py_dist_name) overwrites them
123ad4b
%py3_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("python3dist(" .. 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:
37a004e
# 1:  The PyPI project name, defaulting to %srcname if it is defined, then
37a004e
#     %pypi_name if it is defined, then just %name.
37a004e
# 2:  The PYPI version, defaulting to %version.
37a004e
# 3:  The file extension, defaulting to "tar.gz".  (A period will be added
37a004e
#     automatically.)
37a004e
# 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
37a004e
        ver = rpm.expand('%version')
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
}