b314efc
-- Convenience Lua functions that can be used within Python srpm/rpm macros
b314efc
b314efc
-- Determine alternate names provided from the given name.
b314efc
-- Used in pythonname provides generator, python_provide and py_provides.
b314efc
-- There are 2 rules:
b314efc
--  python3-foo  -> python-foo, python3X-foo
b314efc
--  python3X-foo -> python-foo, python3-foo
b314efc
-- There is no python-foo -> rule, python-foo packages are version agnostic.
b314efc
-- Returns a table/array with strings. Empty when no rule matched.
b314efc
local function python_altnames(name)
b314efc
  local xy = rpm.expand('%{__default_python3_pkgversion}')
b314efc
  local altnames = {}
b314efc
  local replaced
b314efc
  -- NB: dash needs to be escaped!
b314efc
  if name:match('^python3%-') then
b314efc
    for i, prefix in ipairs({'python-', 'python' .. xy .. '-'}) do
b314efc
      replaced = name:gsub('^python3%-', prefix)
b314efc
      table.insert(altnames, replaced)
b314efc
    end
b314efc
  elseif name:match('^python' .. xy .. '%-') then
b314efc
    for i, prefix in ipairs({'python-', 'python3-'}) do
b314efc
      replaced = name:gsub('^python' .. xy .. '%-', prefix)
b314efc
      table.insert(altnames, replaced)
b314efc
    end
b314efc
  end
b314efc
  return altnames
b314efc
end
b314efc
b314efc
125134c
-- For any given name and epoch-version-release, return provides except self.
125134c
-- Uses python_altnames under the hood
125134c
-- Returns a table/array with strings.
125134c
local function python_altprovides(name, evr)
5fe9747
  -- global cache that tells what provides were already processed
5fe9747
  if __python_altnames_provides_beenthere == nil then
5fe9747
    __python_altnames_provides_beenthere = {}
5fe9747
  end
5fe9747
  __python_altnames_provides_beenthere[name .. ' ' .. evr] = true
125134c
  local altprovides = {}
125134c
  for i, altname in ipairs(python_altnames(name)) do
125134c
    table.insert(altprovides, altname .. ' = ' .. evr)
125134c
  end
125134c
  return altprovides
125134c
end
125134c
125134c
125134c
-- Like python_altprovides but only return something once.
125134c
-- For each argument can only be used once, returns nil otherwise.
5fe9747
-- Previous usage of python_altprovides counts as well.
125134c
local function python_altprovides_once(name, evr)
125134c
  -- global cache that tells what provides were already processed
125134c
  if __python_altnames_provides_beenthere == nil then
125134c
    __python_altnames_provides_beenthere = {}
125134c
  end
125134c
  if __python_altnames_provides_beenthere[name .. ' ' .. evr] == nil then
125134c
    __python_altnames_provides_beenthere[name .. ' ' .. evr] = true
125134c
    return python_altprovides(name, evr)
125134c
  else
125134c
    return nil
125134c
  end
125134c
end
125134c
125134c
b314efc
return {
b314efc
  python_altnames = python_altnames,
125134c
  python_altprovides = python_altprovides,
125134c
  python_altprovides_once = python_altprovides_once,
b314efc
}