* lhw changed Url http -> https
[t2sde.git] / scripts / config-functions.lua
blobc6fecb5de891887d0429da4d4ff4d985dfa84568
1 #!/usr/bin/lua
2 -- --- T2-COPYRIGHT-NOTE-BEGIN ---
3 -- T2 SDE: scripts/config-functions.lua
4 -- Copyright (C) 2006 - 2023 The T2 SDE Project
5 -- Copyright (C) 2006 - 2015 Rene Rebe <rene@exactcode.de>
6 --
7 -- This Copyright note is generated by scripts/Create-CopyPatch,
8 -- more information can be found in the files COPYING and README.
9 --
10 -- This program is free software; you can redistribute it and/or modify
11 -- it under the terms of the GNU General Public License version 2.
12 -- --- T2-COPYRIGHT-NOTE-END ---
14 packages = {} -- our internal package array of tables
16 function pkglistread(filename)
17 local f = io.open(filename, "r")
19 packages = {}
21 for line in f:lines() do
22 -- use captures to yank out the various parts:
23 -- X -----5---9 149.800 develop lua 5.1.1 / extra/development DIETLIBC 0
24 -- X -----5---- 112.400 xorg bigreqsproto 1.0.2 / base/x11 0
26 -- hm - maybe strtok as one would do in C?
28 local pkg = {}
29 pkg.status, pkg.stages, pkg.priority, pkg.repository,
30 pkg.name, pkg.ver, pkg.extraver, pkg.categories, pkg.flags =
31 string.match(line, "([XO]) *([0123456789?-]+) *([0123456789.]+) *(%S+) *(%S+) *(%S+) *(%S*) */ ([abcdefghijklmnopqrstuvwxyz0123456789/ ]+) *([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ._-]*) 0")
33 -- shortcoming of above regex
34 if pkg.categories then
35 pkg.categories = string.match(pkg.categories, "(.*%S) *");
36 else
37 print("Error: no Cateory tag - parsing:", line)
38 end
40 pkg.alias = pkg.name
41 pkg.default_status = pkg.status
43 --[[
44 print(line);
46 if pkg.categories == nil then
47 pkg.categories = "nil" end
49 if pkg.flags == nil then
50 pkg.flags = "nil" end
51 io.write("'", pkg.categories, "'", pkg.flags, "'\n")
52 ]]--
54 if pkg.alias == nil then
55 print("Error parsing: ", line)
56 else
57 packages[#packages+1] = pkg
58 end
60 end
61 f:close()
62 end
64 function pkglistwrite(filename)
65 local f = io.open(filename, "w")
67 for i, pkg in ipairs(packages) do
68 -- only write not fully disabled packages
69 if pkg.status ~= "-" then
71 f:write(pkg.status, " ", pkg.stages, " ", pkg.priority, " ",
72 pkg.repository, " ", pkg.alias, " ", pkg.ver)
74 if string.len(pkg.extraver) > 0 then
75 f:write(" ", pkg.extraver)
76 end
78 f:write(" / ", pkg.categories)
80 if string.len(pkg.flags) > 0 then
81 f:write(" ", pkg.flags)
82 end
84 f:write(" 0\n")
85 end
86 end
87 f:close()
88 end
90 -- tracks the state and also expands patterns
91 -- either called with just packages or repository/package
92 -- allowing wildcards such as perl/*
93 local function pkgswitch(mode, ...)
94 --local matched = false
95 local tr = { ["+"] = "%+",
96 ["-"] = "%-",
97 ["*"] = ".*" }
99 for i, arg in ipairs{...} do
100 -- split repo from the package and expand wildcard to regex
101 local rep, pkg = string.match(arg, "(.*)/(.*)");
102 if rep == nil then
103 rep = "*"
104 pkg = arg
107 rep = "^" .. string.gsub(rep, "([+*-])", tr) .. "$"
108 pkg = "^" .. string.gsub(pkg, "([+*-])", tr) .. "$"
110 --optimization, to skip the package traversal early
111 local pkg_match = false
112 if string.find(pkg, "*") == nil then
113 pkg_match = true
116 --print("regex> rep: " .. rep .. ", pkg: '" .. pkg .. "'")
118 for j, p in ipairs(packages) do
119 -- match
120 --[[
121 if (pkg == "linux-header") then
122 print("pkg> p.rep: " .. p.repository ..
123 ", p.alias: '" .. p.alias .. "'")
124 local s1 = string.match(p.alias, pkg)
125 local s2 = string.match(p.repository, rep)
126 if s1 == nil then s1 = "nil" end
127 if s2 == nil then s2 = "nil" end
128 print("match pkg: " .. s1)
129 print("match rep: " .. s2)
131 ]]--
132 if (string.match(p.alias, pkg) and
133 string.match(p.repository, rep)) then
134 --matched = true
135 -- if not already disabled completely
136 --print("matched rep: " .. rep .. ", pkg: " .. pkg)
137 --print("with rep: " .. p.repository ..", pkg: " .. p.alias)
138 if p.status ~= "-" then
139 --print("set to: " .. mode)
140 if mode == "=" then
141 p.status = p.default_status
142 else
143 p.status = mode
146 -- just optimization
147 if pkg_match then
148 break
153 --if matched == false then
154 -- print("Warning: pkgsel - no match for: " .. mode .. " " .. ...)
155 --end
158 function pkgenable(...)
159 pkgswitch("X", ...)
162 function pkgdisable(...)
163 pkgswitch("O", ...)
166 function pkgremove(...)
167 pkgswitch("-", ...)
170 function pkgcheck(pattern, mode)
171 -- split the pattern seperated by "|"
172 p = {}
173 for x in string.gmatch(pattern, "[^|]+") do
174 p[#p+1] = x
177 for i, pkg in ipairs(packages) do
178 for j, x in ipairs(p) do
179 if pkg.alias == x then
180 if mode == "X" then
181 if pkg.status == "X" then return true end
182 elseif mode == "O" then
183 if pkg.status == "O" then return true end
184 elseif mode == "." then
185 return 0
186 else
187 print("Syntax error near pkgcheck: " .. pattern .. " " .. mode)
192 return false
197 -- Parse pkg selection rules
199 -- Example:
200 -- X python - selects just the python package
201 -- O perl/* - deselects all perl repository packages
202 -- = glibc - sets package glibc to it's default state
203 -- include file - recursively parse file specified
206 function pkgsel_parse(filename)
207 local f = io.open(filename, "r")
208 if f == nil then
209 print("Error opening file: '" .. filename .."'")
210 return
213 for line in f:lines() do
214 line = string.gsub(line, "#.*","")
216 local action
217 local pattern
218 action, pattern = string.match(line, "(%S+) +(%S+)")
220 if action == "x" or action == "X" then
221 pkgswitch("X", pattern)
222 elseif action == "o" or action == "O" then
223 pkgswitch("O", pattern)
224 elseif action == "-" then
225 pkgswitch("-", pattern)
226 elseif action == "=" then
227 pkgswitch("=", pattern)
228 elseif action == "include" then
229 pkgsel_parse(pattern)
230 else
231 if not line == "" then
232 print("Syntax error in: " .. line)
236 f:close()
241 print "Lua Bash accel. (C) 2006-2021 by V. Ziegler, R. Rebe, ExactCODE GmbH"
243 -- register shortcuts for the functions above
244 bash.register("pkglistread")
245 bash.register("pkglistwrite")
246 bash.register("pkgcheck")
247 bash.register("pkgremove")
248 bash.register("pkgenable")
249 bash.register("pkgdisable")
250 bash.register("pkgsel_parse")