summaryrefslogtreecommitdiff
path: root/add-common-script.patch
blob: bb7892e5b30452a5cee6d26b1e626e0cf01a3882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
From 4db504b19f6dd04a44e46b43b3aff4e6cab9214d Mon Sep 17 00:00:00 2001
From: wangkerong <wangkerong@huawei.com>
Date: Wed, 13 Oct 2021 10:04:33 +0800
Subject: [PATCH] add_commom_script

---
 common.lua | 294 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 294 insertions(+)
 create mode 100644 common.lua

diff --git a/common.lua b/common.lua
new file mode 100644
index 0000000..884ee4d
--- /dev/null
+++ b/common.lua
@@ -0,0 +1,294 @@
+-- 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
+ 
+-- Sets a spec variable; echoes the result if verbose
+local function explicitset(rpmvar, value, verbose)
+  local value = value
+  if (value == nil) or (value == "") then
+    value = "%{nil}"
+  end
+  rpm.define(rpmvar .. " " .. value)
+  if verbose then
+    rpm.expand("%{warn:Setting %%{" .. rpmvar .. "} = " .. value .. "}")
+  end
+end
+ 
+-- Unsets a spec variable if it is defined; echoes the result if verbose
+local function explicitunset(rpmvar, verbose)
+  if (rpm.expand("%{" .. rpmvar .. "}") ~= "%{" .. rpmvar .. "}") then
+    rpm.define(rpmvar .. " %{nil}")
+    if verbose then
+      rpm.expand("%{warn:Unsetting %%{" .. rpmvar .. "}}")
+    end
+  end
+end
+ 
+-- Sets a spec variable, if not already set; echoes the result if verbose
+local function safeset(rpmvar, value, verbose)
+  if (rpm.expand("%{" .. rpmvar .. "}") == "%{" .. rpmvar .. "}") then
+    explicitset(rpmvar,value,verbose)
+  end
+end
+ 
+-- Aliases a list of rpm variables to the same variables suffixed with 0 (and
+-- vice versa); echoes the result if verbose
+local function zalias(rpmvars, verbose)
+  for _, sfx in ipairs({{"","0"},{"0",""}}) do
+    for _, rpmvar in ipairs(rpmvars) do
+      local toalias = "%{?" .. rpmvar .. sfx[1] .. "}"
+      if (rpm.expand(toalias) ~= "") then
+        safeset(rpmvar .. sfx[2], toalias, verbose)
+      end
+    end
+  end
+end
+ 
+-- Takes a list of rpm variable roots and a suffix and alias current<root> to
+-- <root><suffix> if it resolves to something not empty
+local function setcurrent(rpmvars, suffix, verbose)
+  for _, rpmvar in ipairs(rpmvars) do
+    if (rpm.expand("%{?" .. rpmvar .. suffix .. "}") ~= "") then
+      explicitset(  "current" .. rpmvar, "%{" .. rpmvar .. suffix .. "}", verbose)
+    else
+      explicitunset("current" .. rpmvar,                                  verbose)
+    end
+  end
+end
+ 
+-- Echo the list of rpm variables, with suffix, if set
+local function echovars(rpmvars, suffix)
+  for _, rpmvar in ipairs(rpmvars) do
+    rpmvar = rpmvar .. suffix
+    local header = string.sub("  " .. rpmvar .. ":                                               ",1,21)
+    rpm.expand("%{?" .. rpmvar .. ":%{echo:" .. header .. "%{?" .. rpmvar .. "}}}")
+  end
+end
+ 
+-- Returns an array, indexed by suffix, containing the non-empy values of
+-- <rpmvar><suffix>, with suffix an integer string or the empty string
+local function getsuffixed(rpmvar)
+  local suffixes = {}
+  zalias({rpmvar})
+  for suffix=0,9999 do
+    local value = rpm.expand("%{?" .. rpmvar .. suffix .. "}")
+    if (value ~= "") then
+      suffixes[tostring(suffix)] = value
+    end
+  end
+  -- rpm convention is to alias no suffix to zero suffix
+  -- only add no suffix if zero suffix is different
+  local value = rpm.expand("%{?" .. rpmvar .. "}")
+  if (value ~= "") and (value ~= suffixes["0"]) then
+     suffixes[""] = value
+  end
+  return suffixes
+end
+ 
+-- Returns the list of suffixes, including the empty string, for which
+-- <rpmvar><suffix> is set to a non empty value
+local function getsuffixes(rpmvar)
+  suffixes = {}
+  for suffix in pairs(getsuffixed(rpmvar)) do
+    table.insert(suffixes,suffix)
+  end
+  table.sort(suffixes,
+             function(a,b) return (tonumber(a) or 0) < (tonumber(b) or 0) end)
+  return suffixes
+end
+ 
+-- Returns the suffix for which <rpmvar><suffix> has a non-empty value that
+-- matches best the beginning of the value string
+local function getbestsuffix(rpmvar, value)
+  local best         = nil
+  local currentmatch = ""
+  for suffix, setvalue in pairs(getsuffixed(rpmvar)) do
+  if (string.len(setvalue) > string.len(currentmatch)) and
+     (string.find(value, "^" .. setvalue)) then
+      currentmatch = setvalue
+      best         = suffix
+    end
+  end
+  return best
+end
+ 
+-- %writevars core
+local function writevars(macrofile, rpmvars)
+  for _, rpmvar in ipairs(rpmvars) do
+    print("sed -i 's\029" .. string.upper("@@" .. rpmvar .. "@@") ..
+                   "\029" .. rpm.expand(  "%{" .. rpmvar .. "}" ) ..
+                   "\029g' " .. macrofile .. "\n")
+  end
+end
+ 
+-- https://github.com/rpm-software-management/rpm/issues/566
+-- Reformat a text intended to be used used in a package description, removing
+-- rpm macro generation artefacts.
+-- – remove leading and ending empty lines
+-- – trim intermediary empty lines to a single line
+-- – fold on spaces
+-- Should really be a %%{wordwrap:…} verb
+local function wordwrap(text)
+  text = rpm.expand(text .. "\n")
+  text = string.gsub(text, "\t",              "  ")
+  text = string.gsub(text, "\r",              "\n")
+  text = string.gsub(text, " +\n",            "\n")
+  text = string.gsub(text, "\n+\n",           "\n\n")
+  text = string.gsub(text, "^\n",             "")
+  text = string.gsub(text, "\n( *)[-*—][  ]+", "\n%1– ")
+  output = ""
+  for line in string.gmatch(text, "[^\n]*\n") do
+    local pos = 0
+    local advance = ""
+    for word in string.gmatch(line, "%s*[^%s]*\n?") do
+      local wl, bad = utf8.len(word)
+      if not wl then
+        print("%{warn:Invalid UTF-8 sequence detected in:}" ..
+              "%{warn:" .. word .. "}" ..
+              "%{warn:It may produce unexpected results.}")
+        wl = bad
+      end
+      if (pos == 0) then
+        advance, n = string.gsub(word, "^(%s*– ).*", "%1")
+        if (n == 0) then
+          advance = string.gsub(word, "^(%s*).*", "%1")
+        end
+        advance = string.gsub(advance, "– ", "  ")
+        pos = pos + wl
+      elseif  (pos + wl  < 81) or
+             ((pos + wl == 81) and string.match(word, "\n$")) then
+        pos = pos + wl
+      else
+        word = advance .. string.gsub(word, "^%s*", "")
+        output = output .. "\n"
+        pos = utf8.len(word)
+      end
+      output = output .. word
+      if pos > 80 then
+        pos = 0
+        if not string.match(word, "\n$") then
+          output = output .. "\n"
+        end
+      end
+    end
+  end
+  output = string.gsub(output, "\n*$", "\n")
+  return output
+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
+]]))
+  explicitset("currentname", "%{source_name}", verbose)
+end
+ 
+-- %new_package core
+local function new_package(source_name, pkg_name, name_suffix, first, verbose)
+  -- Safety net when the wrapper is used in conjunction with traditional syntax
+  if (not first) and (not source_name) then
+    rpm.expand([[
+%{warn:Something already set a package name. However, %%{source_name} is not set.}
+%{warn:Please set %%{source_name} to the SRPM name to ensure reliable processing.}
+]])
+    if name_suffix then
+      print(rpm.expand("%package        " .. name_suffix))
+    else
+      print(rpm.expand("%package     -n " .. pkg_name))
+    end
+    return
+  end
+  -- New processing
+  if not (pkg_name or name_suffix or source_name) then
+    rpm.expand([[
+%{error:You need to set %%{source_name} or provide explicit package naming!}
+]])
+  end
+  if name_suffix then
+    print(rpm.expand("%package        "  .. name_suffix))
+    explicitset("currentname", "%{source_name}-" .. name_suffix, verbose)
+  else
+    if not source_name then
+      source_name = pkg_name
+    end
+    if (pkg_name == source_name) then
+      safeset("source_name", source_name, verbose)
+      print(rpm.expand("Name:           %{source_name}"))
+    else
+      if source_name and first then
+        srcpkg(verbose)
+      end
+      print(rpm.expand("%package     -n " .. pkg_name))
+    end
+    explicitset("currentname", pkg_name, verbose)
+  end
+end
+ 
+return {
+  read          = read,
+  hasflag       = hasflag,
+  readflag      = readflag,
+  explicitset   = explicitset,
+  explicitunset = explicitunset,
+  safeset       = safeset,
+  zalias        = zalias,
+  setcurrent    = setcurrent,
+  echovars      = echovars,
+  getsuffixed   = getsuffixed,
+  getsuffixes   = getsuffixes,
+  getbestsuffix = getbestsuffix,
+  writevars     = writevars,
+  wordwrap      = wordwrap,
+  new_package   = new_package,
+}
-- 
2.27.0