new_package: create basic SRPM header as fallback

Make SRPM handover between macros even more graceful and reliable by auto-creating
a basic SRPM header before attempting declaration of a different sub-package.

With this change things will just work as long as the default %{source_name}
%{source_summary} and %{source_description} are set by something to sensible values.
This commit is contained in:
Nicolas Mailhot 2020-05-20 11:13:02 +02:00
parent e416a7b3da
commit a52af8dced
2 changed files with 94 additions and 24 deletions

View File

@ -1,5 +1,45 @@
-- Convenience Lua functions that can be used within rpm macros -- Convenience Lua functions that can be used within rpm macros
-- Reads an rpm variable. Unlike a basic rpm.expand("{?foo}"), returns nil if
-- the variable is unset, which is convenient in lua tests and enables
-- differentiating unset variables from variables set to ""
local function read(rpmvar)
if not rpmvar or
(rpm.expand("%{" .. rpmvar .. "}") == "%{" .. rpmvar .. "}") then
return nil
else
return rpm.expand("%{?" .. rpmvar .. "}")
end
end
-- Returns true if the macro that called this function had flag set
-- For example, hasflag("z") would give the following results:
-- %foo -z bar → true
-- %foo -z → true
-- %foo → false
local function hasflag(flag)
return (rpm.expand("%{-" .. flag .. "}") ~= "")
end
-- Returns the argument passed to flag in the macro that called this function
-- For example, readflag("z") would give the following results:
-- %foo -z bar → bar
-- %foo → nil
-- %foo -z "" → empty string
-- %foo -z '' → empty string
local function readflag(flag)
if not hasflag(flag) then
return nil
else
local a = rpm.expand("%{-" .. flag .. "*}")
-- Handle "" and '' as empty strings
if (a == '""') or (a == "''") then
a = ''
end
return a
end
end
-- Set a spec variable -- Set a spec variable
-- Echo the result if verbose -- Echo the result if verbose
local function explicitset(rpmvar, value, verbose) local function explicitset(rpmvar, value, verbose)
@ -183,46 +223,76 @@ local function wordwrap(text)
return output return output
end end
-- Because rpmbuild will fail if a subpackage is declared before the source
-- package itself, provide a source package declaration shell as fallback.
local function srcpkg(verbose)
if verbose then
rpm.expand([[
%{echo:Creating a header for the SRPM from %%{source_name}, %%{source_summary} and}
%{echo:%%{source_description}. If that is not the intended result, please declare the}
%{echo:SRPM header and set %%{source_name} in your spec file before calling a macro}
%{echo:that creates other package headers.}
]])
end
print(rpm.expand([[
Name: %{source_name}
Summary: %{source_summary}
%description
%wordwrap -v source_description
]]))
set("currentname", "%{source_name}", verbose)
end
-- The processing core of %new_package -- The processing core of %new_package
local function new_package(source_name, pkg_name, name_suffix, previous_name, verbose) local function new_package(source_name, pkg_name, name_suffix, first, verbose)
-- Safety net when the wrapper is used next to traditional syntax in a spec -- Safety net when the wrapper is used in conjunction with traditional syntax
if (source_name == "") and (previous_name ~= "") then if (not first) and (not source_name) then
rpm.expand( rpm.expand([[
"%{warn:Something already set the following package name: " .. previous_name .. ".}" .. %{warn:Something already set a package name. However, %%{source_name} is not set.}
"%{warn:However, %%{source_name} is empty. Please set the SRPM name in %%{source_name}!}") %{warn:Please set %%{source_name} to the SRPM name to ensure reliable processing.}
if (name_suffix ~= "") then ]])
print(rpm.expand("\n%package " .. name_suffix)) if name_suffix then
print(rpm.expand("%package " .. name_suffix))
else else
print(rpm.expand("\n%package -n " .. pkg_name)) print(rpm.expand("%package -n " .. pkg_name))
end end
return return
end end
-- New processing -- New processing
if (pkg_name == "") and (name_suffix ~= "") then if name_suffix and not pkg_name then
pkg_name = name_suffix pkg_name = name_suffix
if (source_name ~= "") then if source_name then
pkg_name = source_name .. "-" .. name_suffix pkg_name = source_name .. "-" .. name_suffix
end end
end end
if (pkg_name == "") then if not pkg_name then
if (source_name == "") then if not source_name then
rpm.expand("%{error:You need to set %%{source_name} or provide explicit package naming!}") rpm.expand([[
%{error:You need to set %%{source_name} or provide explicit package naming!}
]])
else else
pkg_name = source_name pkg_name = source_name
end end
end end
if (source_name == "") then if not source_name then
source_name = pkg_name source_name = pkg_name
end end
if (pkg_name == source_name) then if (pkg_name == source_name) then
safeset("source_name", source_name, verbose) safeset("source_name", source_name, verbose)
print(rpm.expand("\nName: %{source_name}")) print(rpm.expand("Name: %{source_name}"))
else else
print(rpm.expand("\n%package -n " .. pkg_name)) if source_name and first then
srcpkg(verbose)
end end
print(rpm.expand("%package -n " .. pkg_name))
end
set("currentname", pkg_name, verbose)
end end
return { return {
read = read,
hasflag = hasflag,
readflag = readflag,
explicitset = explicitset, explicitset = explicitset,
explicitunset = explicitunset, explicitunset = explicitunset,
safeset = safeset, safeset = safeset,

View File

@ -48,10 +48,10 @@ print(fedora.wordwrap(variable))
# location within the spec file. # location within the spec file.
%new_package(n:v) %{lua: %new_package(n:v) %{lua:
local fedora = require "fedora.common" local fedora = require "fedora.common"
local source_name = rpm.expand("%{?source_name}") local pkg_name = fedora.readflag("n")
local pkg_name = rpm.expand("%{-n*}") local verbose = fedora.hasflag("v")
local name_suffix = rpm.expand("%{?1}") local name_suffix = fedora.read("1")
local previous_name = rpm.expand("%{?name}") local source_name = fedora.read("source_name")
local verbose = (rpm.expand("%{-v}") ~= "") local first = not ( fedora.read("name") or fedora.read("currentname") )
fedora.new_package(source_name, pkg_name, name_suffix, previous_name, verbose) fedora.new_package(source_name, pkg_name, name_suffix, first, verbose)
} }