From cafd4c5d4ae2de4266026d44f9340fa3de2f2b40 Mon Sep 17 00:00:00 2001 From: onefang Date: Sat, 28 Dec 2019 22:45:36 +1000 Subject: Finish the executable refactor, by using it and removing the original. --- apt-panopticommon.lua | 54 +++++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 34 deletions(-) (limited to 'apt-panopticommon.lua') diff --git a/apt-panopticommon.lua b/apt-panopticommon.lua index 53bd674..d9293f6 100644 --- a/apt-panopticommon.lua +++ b/apt-panopticommon.lua @@ -424,43 +424,24 @@ APT.testing = function(t, host) return false end -APT.execute = function (s, logit) - if nil == logit then logit = true end - if logit then D(" executing -   " .. s .. "") 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) - ]] - local f = io.popen(s .. ' ; echo "$?"', 'r') - local status = "" - local result = "" - -- The last line will be the command's returned status, collect everything else in result. - for l in f:lines() do - result = result .. status .. "\n" - status = l - end - return status, result -end - -APT.fork = function(s) - D(" forking -   " .. s .. "") - os.execute(s .. " &") -end - - APT.exe = function(c) - local exe = {status = 0, result = ''} - exe.cmd = c .. ' ' - exe.log = false + local exe = {status = 0, result = '', log = true, cmd = c .. ' '} function exe:log() self.log = true return self end function exe:Nice(c) - self.cmd = self.cmd .. 'ionice -c3 nice -n 19 ' .. 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:also(c) + if nil == c then c = '' else c = ' ' .. c end + self.cmd = self.cmd .. ';' .. c .. ' ' return self end function exe:And(c) @@ -482,7 +463,12 @@ APT.exe = function(c) return self end function exe:Do() - if self.log then D(" executing -   " .. self.cmd .. "") end + --[[ "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.log 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. @@ -491,6 +477,7 @@ APT.exe = function(c) ]] local f = io.popen(self.cmd .. ' ; echo "$?"', 'r') -- The last line will be the command's returned status, collect everything else in result. + self.status = '' -- Otherwise the result starts with 0. for l in f:lines() do self.result = self.result .. self.status .. "\n" self.status = l @@ -500,14 +487,13 @@ APT.exe = function(c) end function exe:fork() self.cmd = '{ ' .. self.cmd .. '; } &' - if self.log then D(" forking -   " .. self.cmd .. "") end + if true == self.log then D(" forking -   " .. self.cmd .. "") end os.execute(self.cmd) return self end return exe end - APT.checkExes = function (exe) local count = io.popen('ps x | grep "' .. exe .. '" | grep -v " grep " | grep -v "flock -n apt-panopticon.lock " | wc -l'):read("*l") D(count .. " " .. exe .. " commands still running.") @@ -650,7 +636,7 @@ end APT.now = 0 local status -status, APT.now = APT.execute('TZ="GMT" ls -l --time-style="+%s" results/stamp | cut -d " " -f 6-6') +APT.now = APT.exe('TZ="GMT" ls -l --time-style="+%s" results/stamp | cut -d " " -f 6-6'):Do().result APT.now = tonumber(APT.now) local start = 'now-1month' local step = '10min' -- cgit v1.1