8d1f5d
-- Convenience Lua functions that can be used within Python srpm/rpm macros
8d1f5d
8d1f5d
-- Determine alternate names provided from the given name.
8d1f5d
-- Used in pythonname provides generator, python_provide and py_provides.
8d1f5d
-- If only_3_to_3_X is false/nil/unused there are 2 rules:
8d1f5d
--  python3-foo  -> python-foo, python3.X-foo
8d1f5d
--  python3.X-foo -> python-foo, python3-foo
8d1f5d
-- If only_3_to_3_X is true there is only 1 rule:
8d1f5d
--  python3-foo  -> python3X-foo
8d1f5d
-- There is no python-foo -> rule, python-foo packages are version agnostic.
8d1f5d
-- Returns a table/array with strings. Empty when no rule matched.
8d1f5d
local function python_altnames(name, only_3_to_3_X)
8d1f5d
  local xy
8d1f5d
  if only_3_to_3_X then
8d1f5d
    -- Here we hardcode the xy prefix we want to obsolete to "39", because:
8d1f5d
    -- 1. Python 3.9 will remain the main Python version in RHEL 9
8d1f5d
    -- 2. python39 in RHEL 8 is still using the dotless naming (as opposed to
8d1f5d
    --    python3.9)
8d1f5d
    xy = "39"
8d1f5d
  else
8d1f5d
    xy = rpm.expand('%{__default_python3_pkgversion}')
8d1f5d
  end
8d1f5d
  local altnames = {}
8d1f5d
  local replaced
8d1f5d
  -- NB: dash needs to be escaped!
8d1f5d
  if name:match('^python3%-') then
8d1f5d
    local prefixes = only_3_to_3_X and {} or {'python-'}
8d1f5d
    for i, prefix in ipairs({'python' .. xy .. '-', table.unpack(prefixes)}) do
8d1f5d
      replaced = name:gsub('^python3%-', prefix)
8d1f5d
      table.insert(altnames, replaced)
8d1f5d
    end
8d1f5d
  elseif name:match('^python' .. xy .. '%-') and not only_3_to_3_X then
8d1f5d
    for i, prefix in ipairs({'python-', 'python3-'}) do
8d1f5d
      replaced = name:gsub('^python' .. xy .. '%-', prefix)
8d1f5d
      table.insert(altnames, replaced)
8d1f5d
    end
8d1f5d
  end
8d1f5d
  return altnames
8d1f5d
end
8d1f5d
8d1f5d
8d1f5d
local function __python_alttags(name, evr, tag_type)
8d1f5d
  -- for the "provides" tag_type we want also unversioned provides
8d1f5d
  local only_3_to_3_X = tag_type ~= "provides"
8d1f5d
  local operator = tag_type == "provides" and ' = ' or ' < '
8d1f5d
8d1f5d
  -- global cache that tells what package NEVRs were already processed for the
8d1f5d
  -- given tag type
8d1f5d
  if __python_alttags_beenthere == nil then
8d1f5d
    __python_alttags_beenthere = {}
8d1f5d
  end
8d1f5d
  if __python_alttags_beenthere[tag_type] == nil then
8d1f5d
    __python_alttags_beenthere[tag_type] = {}
8d1f5d
  end
8d1f5d
  __python_alttags_beenthere[tag_type][name .. ' ' .. evr] = true
8d1f5d
  local alttags = {}
8d1f5d
  for i, altname in ipairs(python_altnames(name, only_3_to_3_X)) do
8d1f5d
    table.insert(alttags, altname .. operator .. evr)
8d1f5d
  end
8d1f5d
  return alttags
8d1f5d
end
8d1f5d
8d1f5d
-- For any given name and epoch-version-release, return provides except self.
8d1f5d
-- Uses python_altnames under the hood
8d1f5d
-- Returns a table/array with strings.
8d1f5d
local function python_altprovides(name, evr)
8d1f5d
  return __python_alttags(name, evr, "provides")
8d1f5d
end
8d1f5d
8d1f5d
-- For any given name and epoch-version-release, return versioned obsoletes except self.
8d1f5d
-- Uses python_altnames under the hood
8d1f5d
-- Returns a table/array with strings.
8d1f5d
local function python_altobsoletes(name, evr)
8d1f5d
  return __python_alttags(name, evr, "obsoletes")
8d1f5d
end
8d1f5d
8d1f5d
8d1f5d
local function __python_alttags_once(name, evr, tag_type)
8d1f5d
  -- global cache that tells what provides were already processed
8d1f5d
  if __python_alttags_beenthere == nil
8d1f5d
      or __python_alttags_beenthere[tag_type] == nil
8d1f5d
      or __python_alttags_beenthere[tag_type][name .. ' ' .. evr] == nil then
8d1f5d
    return __python_alttags(name, evr, tag_type)
8d1f5d
  else
8d1f5d
    return nil
8d1f5d
  end
8d1f5d
end
8d1f5d
8d1f5d
-- Like python_altprovides but only return something once.
8d1f5d
-- For each argument can only be used once, returns nil otherwise.
8d1f5d
-- Previous usage of python_altprovides counts as well.
8d1f5d
local function python_altprovides_once(name, evr)
8d1f5d
  return __python_alttags_once(name, evr, "provides")
8d1f5d
end
8d1f5d
8d1f5d
-- Like python_altobsoletes but only return something once.
8d1f5d
-- For each argument can only be used once, returns nil otherwise.
8d1f5d
-- Previous usage of python_altobsoletes counts as well.
8d1f5d
local function python_altobsoletes_once(name, evr)
8d1f5d
  return __python_alttags_once(name, evr, "obsoletes")
8d1f5d
end
8d1f5d
8d1f5d
8d1f5d
return {
8d1f5d
  python_altnames = python_altnames,
8d1f5d
  python_altprovides = python_altprovides,
8d1f5d
  python_altobsoletes = python_altobsoletes,
8d1f5d
  python_altprovides_once = python_altprovides_once,
8d1f5d
  python_altobsoletes_once = python_altobsoletes_once,
8d1f5d
}