diff options
Diffstat (limited to 'PolygLua.lua')
| -rwxr-xr-x | PolygLua.lua | 301 |
1 files changed, 301 insertions, 0 deletions
diff --git a/PolygLua.lua b/PolygLua.lua new file mode 100755 index 0000000..c2ebb02 --- /dev/null +++ b/PolygLua.lua | |||
| @@ -0,0 +1,301 @@ | |||
| 1 | #!/usr/bin/env luajit | ||
| 2 | |||
| 3 | --[[ PolygLua. Gluing things onto Lua, making it a polyglot language. | ||
| 4 | |||
| 5 | TODO - Allow -abc style, expand to -a -b -c. | ||
| 6 | |||
| 7 | TODO - Make the parsing recursive. So the command "--fancy" could have it's own options table. | ||
| 8 | --fancy option0 opt1=foo 'Random string!' --somethingElse | ||
| 9 | When to stop and hand back? | ||
| 10 | |||
| 11 | TODO - Add some sort of alias mechanism for the #! thing. _.bash'echo "This is bash."':Do() | ||
| 12 | The user can define their own aliases in a table, with defaults for bash, sh, luajit, and maybe some others. | ||
| 13 | Then extend it to changing language on the fly _.bash'echo "This is bash."':luajit("print('This is Lua.')"):Do() | ||
| 14 | |||
| 15 | ]] | ||
| 16 | |||
| 17 | -- Most of this _ stuff was copied from apt-panopticon. | ||
| 18 | local _ = {} | ||
| 19 | _.version = '0.0 crap' | ||
| 20 | |||
| 21 | |||
| 22 | _.verbosity = 2 | ||
| 23 | local log = function(v, t, s) | ||
| 24 | if v <= _.verbosity then | ||
| 25 | if 3 <= _.verbosity then t = os.date('!%F %T') .. " " .. t end | ||
| 26 | print(t .. ": " .. s) | ||
| 27 | end | ||
| 28 | io.flush() | ||
| 29 | end | ||
| 30 | |||
| 31 | -- This sets the global values, here and in the caller. | ||
| 32 | D = function(s) log(4, 'DEBUG ', s) end | ||
| 33 | I = function(s) log(3, 'INFO ', s) end | ||
| 34 | T = function(s) log(2, 'TIMEOUT ', s) end | ||
| 35 | W = function(s) log(1, 'WARNING ', s) end | ||
| 36 | E = function(s) log(0, 'ERROR ', s) end | ||
| 37 | C = function(s) log(-1, 'CRITICAL ', s) end | ||
| 38 | |||
| 39 | |||
| 40 | local optionsCommon = | ||
| 41 | { | ||
| 42 | help = {help = 'Print the help text.', | ||
| 43 | func = function(self, options, a, args, i) | ||
| 44 | print(Help) | ||
| 45 | _.usage(args, options, true) | ||
| 46 | os.exit(0) | ||
| 47 | end | ||
| 48 | }, | ||
| 49 | ['--version'] = {help = 'Print the version details.', | ||
| 50 | func = function(self, options, a, args, i) | ||
| 51 | print('This is version ' ..Version .. ' of ' .. args[0]) | ||
| 52 | os.exit(0) | ||
| 53 | end | ||
| 54 | }, | ||
| 55 | ['-q'] = {help = 'Decrease verbosity level.', | ||
| 56 | func = function(self, options, a, args, i) | ||
| 57 | if _.verbosity > -1 then _.verbosity = _.verbosity - 1 end | ||
| 58 | print('New verbosity level is ' .. _.verbosity) | ||
| 59 | end | ||
| 60 | }, | ||
| 61 | ['-v'] = {help = 'Increase verbosity level.', | ||
| 62 | func = function(self, options, a, args, i) | ||
| 63 | if _.verbosity < 4 then _.verbosity = _.verbosity + 1 end | ||
| 64 | print('New verbosity level is ' .. _.verbosity) | ||
| 65 | end | ||
| 66 | }, | ||
| 67 | } | ||
| 68 | optionsCommon['--help'] = optionsCommon['help'] | ||
| 69 | |||
| 70 | _.usage = function(args, options, all) | ||
| 71 | local h = '' | ||
| 72 | for k, v in pairs(options) do | ||
| 73 | if 'table' == type(v) then h = h .. k .. ' | ' end | ||
| 74 | end | ||
| 75 | for k, v in pairs(optionsCommon) do | ||
| 76 | if 'table' == type(v) then h = h .. k .. ' | ' end | ||
| 77 | end | ||
| 78 | print('Usage: ' .. args[0] .. ' {' .. string.sub(h, 1, -2) .. '}') | ||
| 79 | if true == all then | ||
| 80 | for k, v in pairs(options) do | ||
| 81 | if 'table' == type(v) then | ||
| 82 | if nil ~= v.help then print(k .. '\t\t' .. v.help) end | ||
| 83 | end | ||
| 84 | end | ||
| 85 | for k, v in pairs(optionsCommon) do | ||
| 86 | if 'table' == type(v) then | ||
| 87 | if nil ~= v.help then print(k .. '\t\t' .. v.help) end | ||
| 88 | end | ||
| 89 | end | ||
| 90 | end | ||
| 91 | end | ||
| 92 | |||
| 93 | _.parse = function(args, options, confFile) | ||
| 94 | local o = nil | ||
| 95 | |||
| 96 | local doIt = function(name, val, a, args, i) | ||
| 97 | local o = options[name] | ||
| 98 | if nil == o then o = optionsCommon[name] end | ||
| 99 | if nil ~= o then | ||
| 100 | if nil ~= val then o.value = val; D(name .. ' = ' .. tostring(val)) end | ||
| 101 | if nil ~= o.func then o:func(options, a, args, i) end | ||
| 102 | end | ||
| 103 | return o | ||
| 104 | end | ||
| 105 | |||
| 106 | if nil ~= confFile then | ||
| 107 | for i,v in ipairs{'/etc/', '~/.', './.'} do | ||
| 108 | local p = v .. confFile .. '.conf.lua' | ||
| 109 | local h, e = io.open(p, "r") | ||
| 110 | if nil ~= h then | ||
| 111 | D('Found configuration file '.. p) | ||
| 112 | h:close() | ||
| 113 | local ar = dofile(p) | ||
| 114 | for k, w in pairs(ar) do | ||
| 115 | if nil == doIt(k, w, k .. '=' .. tostring(w), args, i) then W('config variable not found ' .. k .. ' = ' .. tostring(w)) end | ||
| 116 | end | ||
| 117 | end | ||
| 118 | end | ||
| 119 | end | ||
| 120 | |||
| 121 | if 0 ~= #args then | ||
| 122 | for i,a in ipairs(args) do | ||
| 123 | D('Argument ' .. i .. ' = ' .. a) | ||
| 124 | local s, e = a:find("=") | ||
| 125 | if nil == s then | ||
| 126 | e = 0 | ||
| 127 | o = doIt(a:sub(1, e - 1), nil, a, args, i) | ||
| 128 | else | ||
| 129 | o = doIt(a:sub(1, e - 1), a:sub(e + 1, -1), a, args, i) | ||
| 130 | end | ||
| 131 | end | ||
| 132 | end | ||
| 133 | |||
| 134 | if nil == o then | ||
| 135 | _.usage(args, options) | ||
| 136 | os.exit(0) | ||
| 137 | end | ||
| 138 | end | ||
| 139 | |||
| 140 | _.runnable = function(c) | ||
| 141 | return ( 0 == __('which ' .. c):Do().status ) | ||
| 142 | end | ||
| 143 | |||
| 144 | _.running = function(c) | ||
| 145 | return ( 1 ~= tonumber(__("pgrep -u $USER -cf " .. c):Do().lines[1]) ) | ||
| 146 | end | ||
| 147 | |||
| 148 | _.exists = function(f) | ||
| 149 | local h, e = io.open(f, "r") | ||
| 150 | if nil == h then return false else h:close(); return true end | ||
| 151 | end | ||
| 152 | |||
| 153 | |||
| 154 | _.killEmAll = function(all) | ||
| 155 | for i,l in ipairs(all) do | ||
| 156 | local c = 0 | ||
| 157 | while 0 ~= tonumber(__("pgrep -u $USER -xc " .. l):Do().lines[1]) do | ||
| 158 | local s = 'TERM' | ||
| 159 | if c > 1 then s = 'KILL'; __("sleep " .. c):Do() end | ||
| 160 | __("pkill -" .. s .. " -u $USER -x " .. l):log():Do() | ||
| 161 | c = c + 1 | ||
| 162 | end | ||
| 163 | end | ||
| 164 | end | ||
| 165 | |||
| 166 | |||
| 167 | _.readCmd = function(cmd) | ||
| 168 | local result = {} | ||
| 169 | local output = io.popen(cmd) | ||
| 170 | if nil ~= output then | ||
| 171 | for l in output:lines() do | ||
| 172 | table.insert(result, l) | ||
| 173 | end | ||
| 174 | end | ||
| 175 | -- While this does return the same things as os.execute(), it's just as useless. | ||
| 176 | output:close() | ||
| 177 | return result | ||
| 178 | end | ||
| 179 | |||
| 180 | |||
| 181 | __ = function(c) | ||
| 182 | local exe = {status = 0, lines = {}, logging = false, showing = false, cmd = '', command = c, isScript = false, script = ''} | ||
| 183 | local n = 0 | ||
| 184 | |||
| 185 | exe.cmd = '{ ' | ||
| 186 | if 'table' == type(c) then | ||
| 187 | for i, l in ipairs(c) do | ||
| 188 | n = n + 1 | ||
| 189 | exe.cmd = exe.cmd .. l .. ' ; ' | ||
| 190 | end | ||
| 191 | elseif 'string' == type(c) then | ||
| 192 | exe.isScript = ('#!' == c:sub(1,2)) and (n == 0) | ||
| 193 | for l in string.gmatch(c, "\n*([^\n]+)\n*") do | ||
| 194 | if '' ~= l then | ||
| 195 | if exe.isScript then | ||
| 196 | if '' == exe.script then | ||
| 197 | exe.scriptFile = os.tmpname() | ||
| 198 | D('Creating temporary script file at ' .. exe.scriptFile) | ||
| 199 | exe.cmd = exe.cmd .. l:sub(3) .. ' ' .. exe.scriptFile .. ' ; ' | ||
| 200 | end | ||
| 201 | exe.script = exe.script .. l .. '\n' | ||
| 202 | else | ||
| 203 | n = n + 1 | ||
| 204 | exe.cmd = exe.cmd .. l .. ' ; ' | ||
| 205 | end | ||
| 206 | end | ||
| 207 | end | ||
| 208 | end | ||
| 209 | if exe.isScript then | ||
| 210 | local a, e = io.open(exe.scriptFile, "w") | ||
| 211 | if nil == a then E("Could not open " .. exe.scriptFile) else | ||
| 212 | a:write(exe.script) | ||
| 213 | a:close() | ||
| 214 | end | ||
| 215 | exe.cmd = exe.cmd .. 'rm ' .. exe.scriptFile .. ' ; ' | ||
| 216 | end | ||
| 217 | exe.cmd = exe.cmd .. ' } ' | ||
| 218 | if 1 == n then exe.cmd = c end | ||
| 219 | |||
| 220 | |||
| 221 | function exe:Nice(c) | ||
| 222 | if nil == c then | ||
| 223 | self.cmd = 'ionice -c3 nice -n 19 ' .. self.cmd | ||
| 224 | else | ||
| 225 | self.cmd = self.cmd .. ' ionice -c3 nice -n 19 ' .. c .. ' ' | ||
| 226 | end | ||
| 227 | return self | ||
| 228 | end | ||
| 229 | |||
| 230 | function exe:timeout(c) | ||
| 231 | -- 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. | ||
| 232 | -- --kill-after means "send KILL after TERM fails. | ||
| 233 | if nil == c then | ||
| 234 | self.cmd = 'timeout --kill-after=10.0 --foreground 42.0s ' .. self.cmd | ||
| 235 | else | ||
| 236 | self.cmd = 'timeout --kill-after=10.0 --foreground ' .. c .. ' ' .. self.cmd | ||
| 237 | end | ||
| 238 | return self | ||
| 239 | end | ||
| 240 | |||
| 241 | function exe:log() self.logging = true return self end | ||
| 242 | function exe:show() self.showing = true return self end | ||
| 243 | -- Should be called "then" but that's a reserved word. | ||
| 244 | function exe:Then(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. '; ' .. c .. ' ' return self end | ||
| 245 | function exe:And(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' && ' .. c .. ' ' return self end | ||
| 246 | function exe:Or(c) if nil == c then c = '' else c = ' ' .. c end self.cmd = self.cmd .. ' || ' .. c .. ' ' return self end | ||
| 247 | function exe:noErr() self.cmd = self.cmd .. ' 2>/dev/null ' return self end | ||
| 248 | function exe:noOut() self.cmd = self.cmd .. ' 1>/dev/null ' return self end | ||
| 249 | function exe:wait(w) self.cmd = self.cmd .. ' && touch ' .. w .. ' ' return self end | ||
| 250 | |||
| 251 | function exe:Do() | ||
| 252 | --[[ "The condition expression of a control structure can return any | ||
| 253 | value. Both false and nil are considered false. All values different | ||
| 254 | from nil and false are considered true (in particular, the number 0 | ||
| 255 | and the empty string are also true)." | ||
| 256 | says the docs, I beg to differ.]] | ||
| 257 | if true == self.logging then D(" executing - " .. self.cmd) end | ||
| 258 | --[[ Damn os.execute() | ||
| 259 | Lua 5.1 says it returns "a status code, which is system-dependent" | ||
| 260 | Lua 5.2 says it returns true/nil, "exit"/"signal", the status code. | ||
| 261 | I'm getting 7168 or 0. No idea what the fuck that is. | ||
| 262 | local ok, rslt, status = os.execute(s) | ||
| 263 | ]] | ||
| 264 | self.lines = _.readCmd(self.cmd .. '; echo "$?"', 'r') | ||
| 265 | -- The last line will be the command's returned status, fish that out and collect everything else in lines. | ||
| 266 | self.status = tonumber(self.lines[#self.lines]) | ||
| 267 | self.lines[#self.lines] = nil | ||
| 268 | if true == self.showing then for i, l in ipairs(self.lines) do I(l) end end | ||
| 269 | |||
| 270 | if (nil == self.status) then D("STATUS |" .. "NIL" .. '| ' .. self.command) | ||
| 271 | elseif (137 == self.status) or (124 == self.status) then T("timeout killed " .. self.status .. ' ' .. self.command) | ||
| 272 | elseif (0 ~= self.status) then D("STATUS |" .. self.status .. '| ' .. self.command) | ||
| 273 | end | ||
| 274 | |||
| 275 | return self | ||
| 276 | end | ||
| 277 | |||
| 278 | function exe:fork(after, host) | ||
| 279 | -- The host part is from apt-panopticon, likely needed there, but makes no sense here. | ||
| 280 | -- 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 | ||
| 281 | if nil == after then after = '' end | ||
| 282 | if '' ~= after then after = ' ; ' .. after end | ||
| 283 | self.cmd = '{ ' .. self.cmd .. after .. ' ; } & ' | ||
| 284 | if true == self.logging then D(" forking - " .. self.cmd) end | ||
| 285 | os.execute(self.cmd) | ||
| 286 | return self | ||
| 287 | end | ||
| 288 | |||
| 289 | function exe:forkOnce() | ||
| 290 | if _.running(self.command) then | ||
| 291 | D('Already running ' .. self.command) | ||
| 292 | else | ||
| 293 | self:fork() | ||
| 294 | end | ||
| 295 | end | ||
| 296 | |||
| 297 | return exe | ||
| 298 | end | ||
| 299 | |||
| 300 | |||
| 301 | return _ | ||
