From 0c4e8c635d9a25712d65af9b6dcf6fbad4a72b05 Mon Sep 17 00:00:00 2001 From: dvs1 Date: Mon, 21 Oct 2024 18:55:20 +1000 Subject: Restructure the APT.exe stuff. --- _.lua | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100755 _.lua (limited to '_.lua') diff --git a/_.lua b/_.lua new file mode 100755 index 0000000..c1710ee --- /dev/null +++ b/_.lua @@ -0,0 +1,170 @@ +#!/usr/bin/env luajit + + +-- Most of this _ stuff was copied from apt-panopticon. +local _ = {} + +_.version = '0.0 crap' + +_.D = function(s) print('DEBUG ' .. s) end +_.I = function(s) print('INFO ' .. s) end +_.T = function(s) print('TIMEOUT ' .. s) end +_.W = function(s) print('WARNING ' .. s) end +_.E = function(s) print('ERROR ' .. s) end +_.C = function(s) print('CRITICAL ' .. s) end +local D = _.D +local I = _.I +local T = _.T +local W = _.W +local E = _.E +local C = _.C + + +_.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} + 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 + for l in string.gmatch(c, "\n*([^\n]+)\n*") do + if '' ~= l then + n = n + 1 + exe.cmd = exe.cmd .. l .. ' ; ' + end + end + end + exe.cmd = exe.cmd .. ' } ' + if 1 == n then exe.cmd = c end + + function exe:log() + self.logging = true + return self + end + function exe:show() + self.showing = true + return self + 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:also(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 = '' 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, 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 D(l) end end + if (137 == self.status) or (124 == self.status) then + T("timeout killed " .. self.status .. ' ' .. self.command) + elseif (nil == self.status) then + I("STATUS |" .. "NIL" .. '| ' .. self.command) + elseif (0 ~= self.status) then + I("STATUS |" .. self.status .. '| ' .. self.command) + end + return self + end + function exe:fork(host) + 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 + self.cmd = '{ ' .. self.cmd .. ' ; } & ' + if true == self.logging then D(" forking - " .. self.cmd) end + os.execute(self.cmd) + return self + end + return exe +end +local __ = _._ + + +_.exists = function(c) + if 0 == __('which ' .. c):Do().status then return true end + return false +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 + + + +return _ -- cgit v1.1