Blame python.lua

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.
5d7727c
-- If only_3_to_3_X is false/nil/unused there are 2 rules:
1edfea6
--  python3-foo  -> python-foo, python3.X-foo
1edfea6
--  python3.X-foo -> python-foo, python3-foo
5d7727c
-- If only_3_to_3_X is true there is only 1 rule:
5d7727c
--  python3-foo  -> python3.X-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.
5d7727c
local function python_altnames(name, only_3_to_3_X)
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
5d7727c
    local prefixes = only_3_to_3_X and {} or {'python-'}
5d7727c
    for i, prefix in ipairs({'python' .. xy .. '-', table.unpack(prefixes)}) do
b314efc
      replaced = name:gsub('^python3%-', prefix)
b314efc
      table.insert(altnames, replaced)
b314efc
    end
5d7727c
  elseif name:match('^python' .. xy .. '%-') and not only_3_to_3_X 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
5d7727c
local function __python_alttags(name, evr, tag_type)
5d7727c
  -- for the "provides" tag_type we want also unversioned provides
5d7727c
  local only_3_to_3_X = tag_type ~= "provides"
5d7727c
  local operator = tag_type == "provides" and ' = ' or ' < '
5d7727c
5d7727c
  -- global cache that tells what package NEVRs were already processed for the
5d7727c
  -- given tag type
5d7727c
  if __python_alttags_beenthere == nil then
5d7727c
    __python_alttags_beenthere = {}
5d7727c
  end
5d7727c
  if __python_alttags_beenthere[tag_type] == nil then
5d7727c
    __python_alttags_beenthere[tag_type] = {}
5d7727c
  end
5d7727c
  __python_alttags_beenthere[tag_type][name .. ' ' .. evr] = true
5d7727c
  local alttags = {}
5d7727c
  for i, altname in ipairs(python_altnames(name, only_3_to_3_X)) do
5d7727c
    table.insert(alttags, altname .. operator .. evr)
5d7727c
  end
5d7727c
  return alttags
5d7727c
end
5d7727c
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)
5d7727c
  return __python_alttags(name, evr, "provides")
5d7727c
end
5d7727c
5d7727c
-- For any given name and epoch-version-release, return versioned obsoletes except self.
5d7727c
-- Uses python_altnames under the hood
5d7727c
-- Returns a table/array with strings.
5d7727c
local function python_altobsoletes(name, evr)
5d7727c
  return __python_alttags(name, evr, "obsoletes")
5d7727c
end
5d7727c
5d7727c
5d7727c
local function __python_alttags_once(name, evr, tag_type)
5fe9747
  -- global cache that tells what provides were already processed
5d7727c
  if __python_alttags_beenthere == nil
5d7727c
      or __python_alttags_beenthere[tag_type] == nil
5d7727c
      or __python_alttags_beenthere[tag_type][name .. ' ' .. evr] == nil then
5d7727c
    return __python_alttags(name, evr, tag_type)
5d7727c
  else
5d7727c
    return nil
125134c
  end
125134c
end
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)
5d7727c
  return __python_alttags_once(name, evr, "provides")
5d7727c
end
5d7727c
5d7727c
-- Like python_altobsoletes but only return something once.
5d7727c
-- For each argument can only be used once, returns nil otherwise.
5d7727c
-- Previous usage of python_altobsoletes counts as well.
5d7727c
local function python_altobsoletes_once(name, evr)
5d7727c
  return __python_alttags_once(name, evr, "obsoletes")
125134c
end
125134c
125134c
b314efc
return {
b314efc
  python_altnames = python_altnames,
125134c
  python_altprovides = python_altprovides,
5d7727c
  python_altobsoletes = python_altobsoletes,
125134c
  python_altprovides_once = python_altprovides_once,
5d7727c
  python_altobsoletes_once = python_altobsoletes_once,
b314efc
}