Blame SOURCES/python.lua

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