From 723030e9016e28d7a1d74d5f513905bf01a0de18 Mon Sep 17 00:00:00 2001 From: dvs1 Date: Sun, 27 Oct 2024 10:18:41 +1000 Subject: Rename _ to PolygLua externally. Next it'll want it's own package. Oh wait.... --- .aataaj_JACK.lua | 2 +- PolygLua.lua | 301 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ _.lua | 298 ------------------------------------------------------ aataaj.lua | 2 +- test_.lua | 2 +- 5 files changed, 304 insertions(+), 301 deletions(-) create mode 100755 PolygLua.lua delete mode 100755 _.lua diff --git a/.aataaj_JACK.lua b/.aataaj_JACK.lua index d659d50..8378e54 100755 --- a/.aataaj_JACK.lua +++ b/.aataaj_JACK.lua @@ -2,6 +2,6 @@ -- An example users .aataaj_JACK.lua script that just starts qsynth. -local _ = require '_' +local _ = require 'PolygLua' if _.runnable'qsynth' then __'qsynth':forkOnce() end diff --git a/PolygLua.lua b/PolygLua.lua new file mode 100755 index 0000000..c2ebb02 --- /dev/null +++ b/PolygLua.lua @@ -0,0 +1,301 @@ +#!/usr/bin/env luajit + +--[[ PolygLua. Gluing things onto Lua, making it a polyglot language. + +TODO - Allow -abc style, expand to -a -b -c. + +TODO - Make the parsing recursive. So the command "--fancy" could have it's own options table. + --fancy option0 opt1=foo 'Random string!' --somethingElse + When to stop and hand back? + +TODO - Add some sort of alias mechanism for the #! thing. _.bash'echo "This is bash."':Do() + The user can define their own aliases in a table, with defaults for bash, sh, luajit, and maybe some others. + Then extend it to changing language on the fly _.bash'echo "This is bash."':luajit("print('This is Lua.')"):Do() + +]] + +-- Most of this _ stuff was copied from apt-panopticon. +local _ = {} +_.version = '0.0 crap' + + +_.verbosity = 2 +local log = function(v, t, s) + if v <= _.verbosity then + if 3 <= _.verbosity then t = os.date('!%F %T') .. " " .. t end + print(t .. ": " .. s) + end + io.flush() +end + +-- This sets the global values, here and in the caller. +D = function(s) log(4, 'DEBUG ', s) end +I = function(s) log(3, 'INFO ', s) end +T = function(s) log(2, 'TIMEOUT ', s) end +W = function(s) log(1, 'WARNING ', s) end +E = function(s) log(0, 'ERROR ', s) end +C = function(s) log(-1, 'CRITICAL ', s) end + + +local optionsCommon = +{ + help = {help = 'Print the help text.', + func = function(self, options, a, args, i) + print(Help) + _.usage(args, options, true) + os.exit(0) + end + }, + ['--version'] = {help = 'Print the version details.', + func = function(self, options, a, args, i) + print('This is version ' ..Version .. ' of ' .. args[0]) + os.exit(0) + end + }, + ['-q'] = {help = 'Decrease verbosity level.', + func = function(self, options, a, args, i) + if _.verbosity > -1 then _.verbosity = _.verbosity - 1 end + print('New verbosity level is ' .. _.verbosity) + end + }, + ['-v'] = {help = 'Increase verbosity level.', + func = function(self, options, a, args, i) + if _.verbosity < 4 then _.verbosity = _.verbosity + 1 end + print('New verbosity level is ' .. _.verbosity) + end + }, +} +optionsCommon['--help'] = optionsCommon['help'] + +_.usage = function(args, options, all) + local h = '' + for k, v in pairs(options) do + if 'table' == type(v) then h = h .. k .. ' | ' end + end + for k, v in pairs(optionsCommon) do + if 'table' == type(v) then h = h .. k .. ' | ' end + end + print('Usage: ' .. args[0] .. ' {' .. string.sub(h, 1, -2) .. '}') + if true == all then + for k, v in pairs(options) do + if 'table' == type(v) then + if nil ~= v.help then print(k .. '\t\t' .. v.help) end + end + end + for k, v in pairs(optionsCommon) do + if 'table' == type(v) then + if nil ~= v.help then print(k .. '\t\t' .. v.help) end + end + end + end +end + +_.parse = function(args, options, confFile) + local o = nil + + local doIt = function(name, val, a, args, i) + local o = options[name] + if nil == o then o = optionsCommon[name] end + if nil ~= o then + if nil ~= val then o.value = val; D(name .. ' = ' .. tostring(val)) end + if nil ~= o.func then o:func(options, a, args, i) end + end + return o + end + + if nil ~= confFile then + for i,v in ipairs{'/etc/', '~/.', './.'} do + local p = v .. confFile .. '.conf.lua' + local h, e = io.open(p, "r") + if nil ~= h then + D('Found configuration file '.. p) + h:close() + local ar = dofile(p) + for k, w in pairs(ar) do + if nil == doIt(k, w, k .. '=' .. tostring(w), args, i) then W('config variable not found ' .. k .. ' = ' .. tostring(w)) end + end + end + end + end + + if 0 ~= #args then + for i,a in ipairs(args) do + D('Argument ' .. i .. ' = ' .. a) + local s, e = a:find("=") + if nil == s then + e = 0 + o = doIt(a:sub(1, e - 1), nil, a, args, i) + else + o = doIt(a:sub(1, e - 1), a:sub(e + 1, -1), a, args, i) + end + end + end + + if nil == o then + _.usage(args, options) + os.exit(0) + end +end + +_.runnable = function(c) + return ( 0 == __('which ' .. c):Do().status ) +end + +_.running = function(c) + return ( 1 ~= tonumber(__("pgrep -u $USER -cf " .. c):Do().lines[1]) ) +end + +_.exists = function(f) + local h, e = io.open(f, "r") + if nil == h then return false else h:close(); return true end +end + + +_.killEmAll = function(all) + for i,l in ipairs(all) do + local c = 0 + while 0 ~= tonumber(__("pgrep -u $USER -xc " .. l):Do().lines[1]) do + local s = 'TERM' + if c > 1 then s = 'KILL'; __("sleep " .. c):Do() end + __("pkill -" .. s .. " -u $USER -x " .. l):log():Do() + c = c + 1 + end + end +end + + +_.readCmd = function(cmd) + local result = {} + local output = io.popen(cmd) + if nil ~= output then + for l in output:lines() do + table.insert(result, l) + end + end + -- While this does return the same things as os.execute(), it's just as useless. + output:close() + return result +end + + +__ = function(c) + local exe = {status = 0, lines = {}, logging = false, showing = false, cmd = '', command = c, isScript = false, script = ''} + local n = 0 + + exe.cmd = '{ ' + if 'table' == type(c) then + for i, l in ipairs(c) do + n = n + 1 + exe.cmd = exe.cmd .. l .. ' ; ' + end + elseif 'string' == type(c) then + exe.isScript = ('#!' == c:sub(1,2)) and (n == 0) + for l in string.gmatch(c, "\n*([^\n]+)\n*") do + if '' ~= l then + if exe.isScript then + if '' == exe.script then + exe.scriptFile = os.tmpname() + D('Creating temporary script file at ' .. exe.scriptFile) + exe.cmd = exe.cmd .. l:sub(3) .. ' ' .. exe.scriptFile .. ' ; ' + end + exe.script = exe.script .. l .. '\n' + else + n = n + 1 + exe.cmd = exe.cmd .. l .. ' ; ' + end + end + end + end + if exe.isScript then + local a, e = io.open(exe.scriptFile, "w") + if nil == a then E("Could not open " .. exe.scriptFile) else + a:write(exe.script) + a:close() + end + exe.cmd = exe.cmd .. 'rm ' .. exe.scriptFile .. ' ; ' + end + exe.cmd = exe.cmd .. ' } ' + if 1 == n then exe.cmd = c end + + + function exe:Nice(c) + if nil == c then + self.cmd = 'ionice -c3 nice -n 19 ' .. self.cmd + else + self.cmd = self.cmd .. ' ionice -c3 nice -n 19 ' .. c .. ' ' + end + return self + end + + function exe:timeout(c) + -- 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. + -- --kill-after means "send KILL after TERM fails. + if nil == c then + self.cmd = 'timeout --kill-after=10.0 --foreground 42.0s ' .. self.cmd + else + self.cmd = 'timeout --kill-after=10.0 --foreground ' .. c .. ' ' .. self.cmd + end + return self + end + + function exe:log() self.logging = true return self end + function exe:show() self.showing = true return self end + -- Should be called "then" but that's a reserved word. + function exe:Then(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. '; ' .. c .. ' ' return self end + function exe:And(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' && ' .. c .. ' ' return self end + function exe:Or(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' || ' .. c .. ' ' return self end + function exe:noErr() self.cmd = self.cmd .. ' 2>/dev/null ' return self end + function exe:noOut() self.cmd = self.cmd .. ' 1>/dev/null ' return self end + function exe:wait(w) self.cmd = self.cmd .. ' && touch ' .. w .. ' ' return self end + + function exe:Do() + --[[ "The condition expression of a control structure can return any + value. Both false and nil are considered false. All values different + from nil and false are considered true (in particular, the number 0 + and the empty string are also true)." + says the docs, I beg to differ.]] + if true == self.logging then D(" executing - " .. self.cmd) end + --[[ Damn os.execute() + Lua 5.1 says it returns "a status code, which is system-dependent" + Lua 5.2 says it returns true/nil, "exit"/"signal", the status code. + I'm getting 7168 or 0. No idea what the fuck that is. + local ok, rslt, status = os.execute(s) + ]] + self.lines = _.readCmd(self.cmd .. '; echo "$?"', 'r') + -- The last line will be the command's returned status, fish that out and collect everything else in lines. + self.status = tonumber(self.lines[#self.lines]) + self.lines[#self.lines] = nil + if true == self.showing then for i, l in ipairs(self.lines) do I(l) end end + + if (nil == self.status) then D("STATUS |" .. "NIL" .. '| ' .. self.command) + elseif (137 == self.status) or (124 == self.status) then T("timeout killed " .. self.status .. ' ' .. self.command) + elseif (0 ~= self.status) then D("STATUS |" .. self.status .. '| ' .. self.command) + end + + return self + end + + function exe:fork(after, host) +-- The host part is from apt-panopticon, likely needed there, but makes no sense here. +-- 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 + if nil == after then after = '' end + if '' ~= after then after = ' ; ' .. after end + self.cmd = '{ ' .. self.cmd .. after .. ' ; } & ' + if true == self.logging then D(" forking - " .. self.cmd) end + os.execute(self.cmd) + return self + end + + function exe:forkOnce() + if _.running(self.command) then + D('Already running ' .. self.command) + else + self:fork() + end + end + + return exe +end + + +return _ diff --git a/_.lua b/_.lua deleted file mode 100755 index 43f3f59..0000000 --- a/_.lua +++ /dev/null @@ -1,298 +0,0 @@ -#!/usr/bin/env luajit - ---[[ - -TODO - I should name this project PolygLua. Gluing things onto Lua, making it a polyglot language. - PolygLuaT PoLuaGlot PoLuaGLuaT - -TODO - Add some sort of alias mechanism for the #! thing. _.bash'echo "This is bash."':Do() - The user can define their own aliases in a table, with defaults for bash, sh, luajit, and maybe some others. - Then extend it to changing language on the fly _.bash'echo "This is bash."':luajit("print('This is Lua.')"):Do() - -]] - --- Most of this _ stuff was copied from apt-panopticon. -local _ = {} -_.version = '0.0 crap' - - -_.verbosity = 2 -local log = function(v, t, s) - if v <= _.verbosity then - if 3 <= _.verbosity then t = os.date('!%F %T') .. " " .. t end - print(t .. ": " .. s) - end - io.flush() -end - --- This sets the global values, here and in the caller. -D = function(s) log(4, 'DEBUG ', s) end -I = function(s) log(3, 'INFO ', s) end -T = function(s) log(2, 'TIMEOUT ', s) end -W = function(s) log(1, 'WARNING ', s) end -E = function(s) log(0, 'ERROR ', s) end -C = function(s) log(-1, 'CRITICAL ', s) end - - -local optionsCommon = -{ - help = {help = 'Print the help text.', - func = function(self, options, a, args, i) - print(Help) - _.usage(args, options, true) - os.exit(0) - end - }, - ['--version'] = {help = 'Print the version details.', - func = function(self, options, a, args, i) - print('This is version ' ..Version .. ' of ' .. args[0]) - os.exit(0) - end - }, - ['-q'] = {help = 'Decrease verbosity level.', - func = function(self, options, a, args, i) - if _.verbosity > -1 then _.verbosity = _.verbosity - 1 end - print('New verbosity level is ' .. _.verbosity) - end - }, - ['-v'] = {help = 'Increase verbosity level.', - func = function(self, options, a, args, i) - if _.verbosity < 4 then _.verbosity = _.verbosity + 1 end - print('New verbosity level is ' .. _.verbosity) - end - }, -} -optionsCommon['--help'] = optionsCommon['help'] - -_.usage = function(args, options, all) - local h = '' - for k, v in pairs(options) do - if 'table' == type(v) then h = h .. k .. ' | ' end - end - for k, v in pairs(optionsCommon) do - if 'table' == type(v) then h = h .. k .. ' | ' end - end - print('Usage: ' .. args[0] .. ' {' .. string.sub(h, 1, -2) .. '}') - if true == all then - for k, v in pairs(options) do - if 'table' == type(v) then - if nil ~= v.help then print(k .. '\t\t' .. v.help) end - end - end - for k, v in pairs(optionsCommon) do - if 'table' == type(v) then - if nil ~= v.help then print(k .. '\t\t' .. v.help) end - end - end - end -end - -_.parse = function(args, options, confFile) - local o = nil - - local doIt = function(name, val, a, args, i) - local o = options[name] - if nil == o then o = optionsCommon[name] end - if nil ~= o then - if nil ~= val then o.value = val; D(name .. ' = ' .. tostring(val)) end - if nil ~= o.func then o:func(options, a, args, i) end - end - return o - end - - if nil ~= confFile then - for i,v in ipairs{'/etc/', '~/.', './.'} do - local p = v .. confFile .. '.conf.lua' - local h, e = io.open(p, "r") - if nil ~= h then - D('Found configuration file '.. p) - h:close() - local ar = dofile(p) - for k, w in pairs(ar) do - if nil == doIt(k, w, k .. '=' .. tostring(w), args, i) then W('config variable not found ' .. k .. ' = ' .. tostring(w)) end - end - end - end - end - - if 0 ~= #args then - for i,a in ipairs(args) do - D('Argument ' .. i .. ' = ' .. a) - local s, e = a:find("=") - if nil == s then - e = 0 - o = doIt(a:sub(1, e - 1), nil, a, args, i) - else - o = doIt(a:sub(1, e - 1), a:sub(e + 1, -1), a, args, i) - end - end - end - - if nil == o then - _.usage(args, options) - os.exit(0) - end -end - -_.runnable = function(c) - return ( 0 == __('which ' .. c):Do().status ) -end - -_.running = function(c) - return ( 1 ~= tonumber(__("pgrep -u $USER -cf " .. c):Do().lines[1]) ) -end - -_.exists = function(f) - local h, e = io.open(f, "r") - if nil == h then return false else h:close(); return true end -end - - -_.killEmAll = function(all) - for i,l in ipairs(all) do - local c = 0 - while 0 ~= tonumber(__("pgrep -u $USER -xc " .. l):Do().lines[1]) do - local s = 'TERM' - if c > 1 then s = 'KILL'; __("sleep " .. c):Do() end - __("pkill -" .. s .. " -u $USER -x " .. l):log():Do() - c = c + 1 - end - end -end - - -_.readCmd = function(cmd) - local result = {} - local output = io.popen(cmd) - if nil ~= output then - for l in output:lines() do - table.insert(result, l) - end - end - -- While this does return the same things as os.execute(), it's just as useless. - output:close() - return result -end - - -__ = function(c) - local exe = {status = 0, lines = {}, logging = false, showing = false, cmd = '', command = c, isScript = false, script = ''} - local n = 0 - - exe.cmd = '{ ' - if 'table' == type(c) then - for i, l in ipairs(c) do - n = n + 1 - exe.cmd = exe.cmd .. l .. ' ; ' - end - elseif 'string' == type(c) then - exe.isScript = ('#!' == c:sub(1,2)) and (n == 0) - for l in string.gmatch(c, "\n*([^\n]+)\n*") do - if '' ~= l then - if exe.isScript then - if '' == exe.script then - exe.scriptFile = os.tmpname() - D('Creating temporary script file at ' .. exe.scriptFile) - exe.cmd = exe.cmd .. l:sub(3) .. ' ' .. exe.scriptFile .. ' ; ' - end - exe.script = exe.script .. l .. '\n' - else - n = n + 1 - exe.cmd = exe.cmd .. l .. ' ; ' - end - end - end - end - if exe.isScript then - local a, e = io.open(exe.scriptFile, "w") - if nil == a then E("Could not open " .. exe.scriptFile) else - a:write(exe.script) - a:close() - end - exe.cmd = exe.cmd .. 'rm ' .. exe.scriptFile .. ' ; ' - end - exe.cmd = exe.cmd .. ' } ' - if 1 == n then exe.cmd = c end - - - function exe:Nice(c) - if nil == c then - self.cmd = 'ionice -c3 nice -n 19 ' .. self.cmd - else - self.cmd = self.cmd .. ' ionice -c3 nice -n 19 ' .. c .. ' ' - end - return self - end - - function exe:timeout(c) - -- 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. - -- --kill-after means "send KILL after TERM fails. - if nil == c then - self.cmd = 'timeout --kill-after=10.0 --foreground 42.0s ' .. self.cmd - else - self.cmd = 'timeout --kill-after=10.0 --foreground ' .. c .. ' ' .. self.cmd - end - return self - end - - function exe:log() self.logging = true return self end - function exe:show() self.showing = true return self end - -- Should be called "then" but that's a reserved word. - function exe:Then(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. '; ' .. c .. ' ' return self end - function exe:And(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' && ' .. c .. ' ' return self end - function exe:Or(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' || ' .. c .. ' ' return self end - function exe:noErr() self.cmd = self.cmd .. ' 2>/dev/null ' return self end - function exe:noOut() self.cmd = self.cmd .. ' 1>/dev/null ' return self end - function exe:wait(w) self.cmd = self.cmd .. ' && touch ' .. w .. ' ' return self end - - function exe:Do() - --[[ "The condition expression of a control structure can return any - value. Both false and nil are considered false. All values different - from nil and false are considered true (in particular, the number 0 - and the empty string are also true)." - says the docs, I beg to differ.]] - if true == self.logging then D(" executing - " .. self.cmd) end - --[[ Damn os.execute() - Lua 5.1 says it returns "a status code, which is system-dependent" - Lua 5.2 says it returns true/nil, "exit"/"signal", the status code. - I'm getting 7168 or 0. No idea what the fuck that is. - local ok, rslt, status = os.execute(s) - ]] - self.lines = _.readCmd(self.cmd .. '; echo "$?"', 'r') - -- The last line will be the command's returned status, fish that out and collect everything else in lines. - self.status = tonumber(self.lines[#self.lines]) - self.lines[#self.lines] = nil - if true == self.showing then for i, l in ipairs(self.lines) do I(l) end end - - if (nil == self.status) then D("STATUS |" .. "NIL" .. '| ' .. self.command) - elseif (137 == self.status) or (124 == self.status) then T("timeout killed " .. self.status .. ' ' .. self.command) - elseif (0 ~= self.status) then D("STATUS |" .. self.status .. '| ' .. self.command) - end - - return self - end - - function exe:fork(after, host) --- The host part is from apt-panopticon, likely needed there, but makes no sense here. --- 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 - if nil == after then after = '' end - if '' ~= after then after = ' ; ' .. after end - self.cmd = '{ ' .. self.cmd .. after .. ' ; } & ' - if true == self.logging then D(" forking - " .. self.cmd) end - os.execute(self.cmd) - return self - end - - function exe:forkOnce() - if _.running(self.command) then - D('Already running ' .. self.command) - else - self:fork() - end - end - - return exe -end - - -return _ diff --git a/aataaj.lua b/aataaj.lua index eaa0fef..5f6cee5 100755 --- a/aataaj.lua +++ b/aataaj.lua @@ -14,7 +14,7 @@ ### END INIT INFO ]] -local _ = require '_' +local _ = require 'PolygLua' Help = [[ diff --git a/test_.lua b/test_.lua index 6a51b45..452f4c9 100755 --- a/test_.lua +++ b/test_.lua @@ -1,7 +1,7 @@ #!/usr/bin/env luajit -local _ = require '_' +local _ = require 'PolygLua' I'Starting from the top.' -- cgit v1.1