diff options
Diffstat (limited to 'PolygLua.lua')
-rwxr-xr-x | PolygLua.lua | 405 |
1 files changed, 0 insertions, 405 deletions
diff --git a/PolygLua.lua b/PolygLua.lua deleted file mode 100755 index 17abd88..0000000 --- a/PolygLua.lua +++ /dev/null | |||
@@ -1,405 +0,0 @@ | |||
1 | #!/usr/bin/env luajit | ||
2 | |||
3 | --[[ PolygLua. Gluing things onto Lua, making it a polyglot language. | ||
4 | |||
5 | TODO - Make the parsing recursive. So the command '--fancy' could have it's own options table. | ||
6 | --fancy option0 opt1=foo 'Random string!' --somethingElse | ||
7 | When to stop and hand back? First time we see a ' -bar' or ' --foo'? | ||
8 | |||
9 | TODO - Add some sort of alias mechanism for the #! thing. _.bash'echo "This is bash."':Do() | ||
10 | The user can define their own aliases in a table, with defaults for bash, sh, luajit, and maybe some others. | ||
11 | Then extend it to changing language on the fly _.bash'echo "This is bash."':luajit("print('This is Lua.')"):Do() | ||
12 | |||
13 | TODO - APT also has +/- for adding and removing option values into an options entry. | ||
14 | Not sure if that is being used. | ||
15 | |||
16 | ]] | ||
17 | |||
18 | -- Most of this _ stuff was copied from apt-panopticon. | ||
19 | local _ = {} | ||
20 | _.version = '0.0 crap' | ||
21 | |||
22 | |||
23 | _.verbosity = 2 | ||
24 | local log = function(v, t, s) | ||
25 | if v <= _.verbosity then | ||
26 | if 3 <= _.verbosity then t = os.date('!%F %T') .. ' ' .. t end | ||
27 | print(t .. ': ' .. s) | ||
28 | end | ||
29 | io.flush() | ||
30 | end | ||
31 | |||
32 | -- This sets the global values, here and in the caller. The "_G." part isn't needed, it's just there to make things more obvious to the reader. | ||
33 | _G.D = function(s) log(4, 'DEBUG ', s) end | ||
34 | _G.I = function(s) log(3, 'INFO ', s) end | ||
35 | _G.T = function(s) log(2, 'TIMEOUT ', s) end | ||
36 | _G.W = function(s) log(1, 'WARNING ', s) end | ||
37 | _G.E = function(s) log(0, 'ERROR ', s) end | ||
38 | _G.C = function(s) log(-1, 'CRITICAL ', s) end | ||
39 | |||
40 | |||
41 | local optionsCommon = | ||
42 | { | ||
43 | help = {help = 'Print the help text.', | ||
44 | func = function(self, options, a, args, i, name) | ||
45 | for i,v in ipairs{'/usr/share/doc/' .. name, '/usr/local/share/doc/' .. name, './'} do | ||
46 | local p = v .. 'README.md' | ||
47 | local h = io.open(p, 'r') | ||
48 | if nil ~= h then | ||
49 | D('Found README file '.. p) | ||
50 | Help = h:read('*a') | ||
51 | h:close() | ||
52 | end | ||
53 | end | ||
54 | |||
55 | print(Help) | ||
56 | _.usage(args, options, true) | ||
57 | os.exit(0) | ||
58 | end | ||
59 | }, | ||
60 | version = {help = 'Print the version details.', | ||
61 | func = function(self, options, a, args, i) | ||
62 | print('This is version ' ..Version .. ' of ' .. args[0]) | ||
63 | os.exit(0) | ||
64 | end | ||
65 | }, | ||
66 | ['-q'] = {help = 'Decrease verbosity level.', | ||
67 | func = function(self, options, a, args, i) | ||
68 | if _.verbosity > -1 then _.verbosity = _.verbosity - 1 end | ||
69 | print('New verbosity level is ' .. _.verbosity) | ||
70 | end | ||
71 | }, | ||
72 | ['-v'] = {help = 'Increase verbosity level.', | ||
73 | func = function(self, options, a, args, i) | ||
74 | if _.verbosity < 4 then _.verbosity = _.verbosity + 1 end | ||
75 | print('New verbosity level is ' .. _.verbosity) | ||
76 | end | ||
77 | }, | ||
78 | } | ||
79 | |||
80 | _.usage = function(args, options, all) | ||
81 | local h = '' | ||
82 | for k, v in pairs(options) do | ||
83 | if 'table' == type(v) then h = h .. k .. ' | ' end | ||
84 | end | ||
85 | for k, v in pairs(optionsCommon) do | ||
86 | if 'table' == type(v) then h = h .. k .. ' | ' end | ||
87 | end | ||
88 | print('Usage: ' .. args[0] .. ' {' .. string.sub(h, 1, -2) .. '}') | ||
89 | if true == all then | ||
90 | for k, v in pairs(options) do | ||
91 | if 'table' == type(v) then | ||
92 | if nil ~= v.help then print(k .. '\t\t' .. v.help) end | ||
93 | end | ||
94 | end | ||
95 | for k, v in pairs(optionsCommon) do | ||
96 | if 'table' == type(v) then | ||
97 | if nil ~= v.help then print(k .. '\t\t' .. v.help) end | ||
98 | end | ||
99 | end | ||
100 | end | ||
101 | end | ||
102 | |||
103 | _.parse = function(args, options, name) | ||
104 | local o = nil | ||
105 | |||
106 | local doIt = function(name, val, a, args, i) | ||
107 | local o = options[name] | ||
108 | if nil == o then o = optionsCommon[name] end | ||
109 | if nil ~= o then | ||
110 | if nil ~= val then o.value = val; D(name .. ' = ' .. tostring(val)) end | ||
111 | if nil ~= o.func then o:func(options, a, args, i, name) end | ||
112 | end | ||
113 | return o | ||
114 | end | ||
115 | |||
116 | if nil ~= name then | ||
117 | for i,v in ipairs{'/etc/', '~/.', './.'} do | ||
118 | local p = v .. name .. '.conf.lua' | ||
119 | local h = io.open(p, 'r') | ||
120 | if nil ~= h then | ||
121 | D('Found configuration file '.. p) | ||
122 | h:close() | ||
123 | local ar = dofile(p) | ||
124 | for k, w in pairs(ar) do | ||
125 | if nil == doIt(k, w, k .. '=' .. tostring(w), args, i) then W('config variable not found ' .. k .. ' = ' .. tostring(w)) end | ||
126 | end | ||
127 | end | ||
128 | end | ||
129 | end | ||
130 | |||
131 | if 0 ~= #args then | ||
132 | for i,a in ipairs(args) do | ||
133 | D('Argument ' .. i .. ' = ' .. a) | ||
134 | local ds = 0 | ||
135 | if ('-' == a:sub(1, 1)) and ('-' ~= a:sub(2, 2)) then ds = 1 end | ||
136 | if '--' == a:sub(1, 2) then ds = 2; a = a:sub(3, -1) end | ||
137 | local s, e = a:find('=', 1, true) | ||
138 | local k , v | ||
139 | if not s then | ||
140 | e = 0 | ||
141 | v = nil | ||
142 | else | ||
143 | v = a:sub(e + 1, -1) | ||
144 | end | ||
145 | k = a:sub(1, e - 1) | ||
146 | if 1 == ds then | ||
147 | for j = 2, #k do | ||
148 | o = doIt('-' .. k:sub(j, j), v, a, args, i) | ||
149 | end | ||
150 | else | ||
151 | o = doIt(k, v, a, args, i) | ||
152 | end | ||
153 | end | ||
154 | end | ||
155 | |||
156 | if nil == o then | ||
157 | _.usage(args, options) | ||
158 | os.exit(0) | ||
159 | end | ||
160 | end | ||
161 | |||
162 | _.runnable = function(c) | ||
163 | return ( 0 == __('which ' .. c):Do().status ) | ||
164 | end | ||
165 | |||
166 | _.running = function(c) | ||
167 | return ( 1 ~= tonumber(__('pgrep -u $USER -cf ' .. c):Do().lines[1]) ) | ||
168 | end | ||
169 | |||
170 | _.exists = function(f) | ||
171 | local h = io.open(f, 'r') | ||
172 | if nil == h then return false else h:close(); return true end | ||
173 | end | ||
174 | |||
175 | |||
176 | _.killEmAll = function(all) | ||
177 | for i,l in ipairs(all) do | ||
178 | local c = 0 | ||
179 | while 0 ~= tonumber(__('pgrep -u $USER -xc ' .. l):Do().lines[1]) do | ||
180 | local s = 'TERM' | ||
181 | if c > 1 then s = 'KILL'; __('sleep ' .. c):Do() end | ||
182 | __('pkill -' .. s .. ' -u $USER -x ' .. l):log():Do() | ||
183 | c = c + 1 | ||
184 | end | ||
185 | end | ||
186 | end | ||
187 | |||
188 | |||
189 | _.readCmd = function(cmd) | ||
190 | local result = {} | ||
191 | local output = io.popen(cmd) | ||
192 | if nil ~= output then | ||
193 | for l in output:lines() do | ||
194 | table.insert(result, l) | ||
195 | end | ||
196 | end | ||
197 | -- While this does return the same things as os.execute(), it's just as useless. | ||
198 | output:close() | ||
199 | return result | ||
200 | end | ||
201 | |||
202 | |||
203 | _G.__ = function(c) | ||
204 | local exe = {status = 0, lines = {}, logging = false, showing = false, cmd = '', command = c, isScript = false, script = ''} | ||
205 | local n = 0 | ||
206 | |||
207 | exe.cmd = '{ ' | ||
208 | if 'table' == type(c) then | ||
209 | for i, l in ipairs(c) do | ||
210 | n = n + 1 | ||
211 | exe.cmd = exe.cmd .. l .. ' ; ' | ||
212 | end | ||
213 | elseif 'string' == type(c) then | ||
214 | exe.isScript = (n == 0) and ('#!' == c:sub(1,2)) | ||
215 | for l in string.gmatch(c, '\n*([^\n]+)\n*') do | ||
216 | if '' ~= l then | ||
217 | if exe.isScript then | ||
218 | if '' == exe.script then | ||
219 | exe.scriptFile = os.tmpname() | ||
220 | D('Creating temporary script file at ' .. exe.scriptFile) | ||
221 | exe.cmd = exe.cmd .. l:sub(3) .. ' ' .. exe.scriptFile .. ' ; ' | ||
222 | -- PHP wants this to be executable. | ||
223 | __('chmod u+x ' .. exe.scriptFile) | ||
224 | end | ||
225 | exe.script = exe.script .. l .. '\n' | ||
226 | else | ||
227 | n = n + 1 | ||
228 | exe.cmd = exe.cmd .. l .. ' ; ' | ||
229 | end | ||
230 | end | ||
231 | end | ||
232 | end | ||
233 | if exe.isScript then | ||
234 | local a, e = io.open(exe.scriptFile, 'w') | ||
235 | if nil == a then E('Could not open ' .. exe.scriptFile .. ' - ' .. e) else | ||
236 | a:write(exe.script) | ||
237 | a:close() | ||
238 | end | ||
239 | exe.cmd = exe.cmd .. 'rm ' .. exe.scriptFile .. ' ; ' | ||
240 | end | ||
241 | exe.cmd = exe.cmd .. ' } ' | ||
242 | if 1 == n then exe.cmd = c end | ||
243 | |||
244 | |||
245 | function exe:Nice(c) | ||
246 | if nil == c then | ||
247 | self.cmd = 'ionice -c3 nice -n 19 ' .. self.cmd | ||
248 | else | ||
249 | self.cmd = self.cmd .. ' ionice -c3 nice -n 19 ' .. c .. ' ' | ||
250 | end | ||
251 | return self | ||
252 | end | ||
253 | |||
254 | function exe:timeout(c) | ||
255 | -- timeout returns a status of - command status if --preserve-status; "128+9" (actually 137) if --kill-after ends up being done; 124 if it had to TERM; command status if all went well. | ||
256 | -- --kill-after means "send KILL after TERM fails". | ||
257 | if nil == c then | ||
258 | self.cmd = 'timeout --kill-after=10.0 --foreground 42.0s ' .. self.cmd | ||
259 | else | ||
260 | self.cmd = 'timeout --kill-after=10.0 --foreground ' .. c .. ' ' .. self.cmd | ||
261 | end | ||
262 | return self | ||
263 | end | ||
264 | |||
265 | function exe:log() self.logging = true return self end | ||
266 | function exe:show() self.showing = true return self end | ||
267 | -- Should be called "then" but that's a reserved word. | ||
268 | function exe:Then(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. '; ' .. c .. ' ' return self end | ||
269 | function exe:And(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' && ' .. c .. ' ' return self end | ||
270 | function exe:Or(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' || ' .. c .. ' ' return self end | ||
271 | function exe:noErr() self.cmd = self.cmd .. ' 2>/dev/null ' return self end | ||
272 | function exe:noOut() self.cmd = self.cmd .. ' 1>/dev/null ' return self end | ||
273 | function exe:wait(w) self.cmd = self.cmd .. ' && touch ' .. w .. ' ' return self end | ||
274 | |||
275 | function exe:Do() | ||
276 | --[[ "The condition expression of a control structure can return any | ||
277 | value. Both false and nil are considered false. All values different | ||
278 | from nil and false are considered true (in particular, the number 0 | ||
279 | and the empty string are also true)." | ||
280 | says the docs, I beg to differ.]] | ||
281 | if true == self.logging then D(' executing - ' .. self.cmd) end | ||
282 | --[[ Damn os.execute() | ||
283 | Lua 5.1 says it returns "a status code, which is system-dependent" | ||
284 | Lua 5.2 says it returns true/nil, "exit"/"signal", the status code. | ||
285 | I'm getting 7168 or 0. No idea what the fuck that is. | ||
286 | local ok, rslt, status = os.execute(s) | ||
287 | ]] | ||
288 | self.lines = _.readCmd(self.cmd .. '; echo "$?"', 'r') | ||
289 | -- The last line will be the command's returned status, fish that out and collect everything else in lines. | ||
290 | self.status = tonumber(self.lines[#self.lines]) | ||
291 | self.lines[#self.lines] = nil | ||
292 | if true == self.showing then for i, l in ipairs(self.lines) do I(l) end end | ||
293 | |||
294 | if (nil == self.status) then D('STATUS |' .. 'NIL' .. '| ' .. self.command) | ||
295 | elseif (137 == self.status) or (124 == self.status) then T('timeout killed ' .. self.status .. ' ' .. self.command) | ||
296 | elseif (0 ~= self.status) then D('STATUS |' .. self.status .. '| ' .. self.command) | ||
297 | end | ||
298 | |||
299 | return self | ||
300 | end | ||
301 | |||
302 | function exe:fork(after, host) | ||
303 | -- The host part is from apt-panopticon, likely needed there, but makes no sense here. | ||
304 | -- if nil ~= host then self.cmd = self.cmd .. '; r=$?; if [ $r -ge 124 ]; then echo "$r ' .. host .. ' failed forked command ' .. string.gsub(self.cmd, '"', "'") .. '"; fi' end | ||
305 | if nil == after then after = '' end | ||
306 | if '' ~= after then after = ' ; ' .. after end | ||
307 | self.cmd = '{ ' .. self.cmd .. after .. ' ; } & ' | ||
308 | if true == self.logging then D(' forking - ' .. self.cmd) end | ||
309 | os.execute(self.cmd) | ||
310 | return self | ||
311 | end | ||
312 | |||
313 | function exe:forkOnce() | ||
314 | if _.running(self.command) then | ||
315 | D('Already running ' .. self.command) | ||
316 | else | ||
317 | self:fork() | ||
318 | end | ||
319 | end | ||
320 | |||
321 | return exe | ||
322 | end | ||
323 | |||
324 | |||
325 | |||
326 | _.who = __[[whoami]]:noErr():Do().lines[1] | ||
327 | _.dir = __[[pwd]]:noErr():Do().lines[1] | ||
328 | |||
329 | |||
330 | |||
331 | if (arg[0] == './PolygLua.lua') or (arg[0] == 'PolygLua.lua') then | ||
332 | local function goAway(txt) | ||
333 | local luas = __'ls -d1 /usr/share/lua/*':noErr():Do() | ||
334 | for i,l in ipairs(luas.lines) do | ||
335 | local lua = '/usr/local/share/lua/' .. l:sub(16) .. '/PolygLua.lua' | ||
336 | if _.exists(lua) then | ||
337 | if 'root' == _.who then | ||
338 | print(txt .. ' ' .. lua) | ||
339 | __('rm ' .. lua):Do() | ||
340 | end | ||
341 | end | ||
342 | end | ||
343 | end | ||
344 | |||
345 | local options = | ||
346 | { | ||
347 | install = | ||
348 | { | ||
349 | help = 'Command to install PolygLua.lua', | ||
350 | func = function(self, options, a, args, i) | ||
351 | if 'root' ~= _.who then | ||
352 | E'Need to be root user to install.' | ||
353 | else | ||
354 | print('INSTALLING PolygLua.lua!!!') | ||
355 | |||
356 | local luas = __'ls -d1 /usr/share/lua/*':noErr():Do() | ||
357 | for i,l in ipairs(luas.lines) do | ||
358 | local lua = '/usr/local/share/lua/' .. l:sub(16) .. '/PolygLua.lua' | ||
359 | if _.exists(lua) then | ||
360 | print(lua .. ' installed') | ||
361 | else | ||
362 | if 'root' == _.who then | ||
363 | print('Installing ' .. lua) | ||
364 | __('mkdir -p /usr/local/share/lua/' .. l:sub(16) .. ' ; ln -s ' .. _.dir .. '/PolygLua.lua ' .. lua):Do() | ||
365 | else | ||
366 | print(lua .. ' NOT installed') | ||
367 | end | ||
368 | end | ||
369 | end | ||
370 | |||
371 | end | ||
372 | end | ||
373 | }, | ||
374 | uninstall = | ||
375 | { | ||
376 | help = 'Command to uninstall PolygLua.lua', | ||
377 | func = function(self, options, a, args, i) | ||
378 | if 'root' ~= _.who then | ||
379 | E'Need to be root user to uninstall.' | ||
380 | else | ||
381 | print('UNINSTALLING PolygLua.lua!!!') | ||
382 | goAway('Uninstalling') | ||
383 | end | ||
384 | end | ||
385 | }, | ||
386 | purge = | ||
387 | { | ||
388 | help = 'Command to purge PolygLua.lua', | ||
389 | func = function(self, options, a, args, i) | ||
390 | if 'root' ~= _.who then | ||
391 | E'Need to be root user to purge.' | ||
392 | else | ||
393 | print('PURGING PolygLua.lua!!!') | ||
394 | goAway('Purging') | ||
395 | end | ||
396 | end | ||
397 | }, | ||
398 | } | ||
399 | |||
400 | _.parse(arg, options) | ||
401 | end | ||
402 | |||
403 | |||
404 | |||
405 | return _ | ||