Tweaks and cleanup

This commit is contained in:
natemaia
2018-04-18 23:43:29 -07:00
parent 3aad6fe631
commit a6093d1758
9 changed files with 943 additions and 890 deletions

View File

@ -26,159 +26,159 @@ BB = {}
BB.__index = BB
function BB.create(url, builder)
local b = {}
setmetatable(b,BB)
b.url = url -- buildbot url
b.builder = builder -- builder name
b.lastChecked = 0 -- last checked build number
b.lastSuccessful = 0 -- last successful build number
b.lastResult = nil -- last json parsed result
b.lastError = nil -- last error string or nil if no error
return b
local b = {}
setmetatable(b,BB)
b.url = url -- buildbot url
b.builder = builder -- builder name
b.lastChecked = 0 -- last checked build number
b.lastSuccessful = 0 -- last successful build number
b.lastResult = nil -- last json parsed result
b.lastError = nil -- last error string or nil if no error
return b
end
function BB:_queryBuildbot(build_number)
local f = io.popen("curl --connect-timeout 1 "..self.url.."/json/builders/"..self.builder.."/builds/"..build_number)
local jsbuilder = f:read("*all")
f:close()
if #jsbuilder == 0 then
return false, "can't read from url"
end
local f = io.popen("curl --connect-timeout 1 "..self.url.."/json/builders/"..self.builder.."/builds/"..build_number)
local jsbuilder = f:read("*all")
f:close()
if #jsbuilder == 0 then
return false, "can't read from url"
end
local result_status, result = pcall(json.decode, jsbuilder, false)
if not result_status then
return false, "can't parse json data"
end
return true, result
local result_status, result = pcall(json.decode, jsbuilder, false)
if not result_status then
return false, "can't parse json data"
end
return true, result
end
function BB:_getBuildStatus(result)
if #result['text'] > 0 then
local text = result['text']
if text[1] == "build" and text[2] == "successful" and #text == 2 then
--successful
return bs.OK
if #result['text'] > 0 then
local text = result['text']
if text[1] == "build" and text[2] == "successful" and #text == 2 then
--successful
return bs.OK
else
--failed
return bs.FAILED
end
else
--failed
return bs.FAILED
--in progress
return bs.RUNNING
end
else
--in progress
return bs.RUNNING
end
end
-- Function queries buildbot to refresh builds status.
-- * if build is successful or failed it will not be queried again, number is stored in lasteChecked
-- * up to 10 last builds will be checked to find last successful build
function BB:refresh()
local last_pass_fail = 0
local nr = -1
local last_result
local iter_counter = 0
local last_pass_fail = 0
local nr = -1
local last_result
local iter_counter = 0
self.lastError = nil
self.lastResult = nil
--- there is a gap to fill in, iterate all not checked builds starting from latest
while nr > self.lastChecked or nr == -1 do
local r_status, r = self:_queryBuildbot(nr)
local s
self.lastError = nil
self.lastResult = nil
--- there is a gap to fill in, iterate all not checked builds starting from latest
while nr > self.lastChecked or nr == -1 do
local r_status, r = self:_queryBuildbot(nr)
local s
if not r_status then
self.lastError = r
return
end
if not r_status then
self.lastError = r
return
end
s = self:_getBuildStatus(r)
if not last_result then
last_result = r
s = self:_getBuildStatus(r)
if not last_result then
last_result = r
end
nr = r['number']
assert(nr > 0)
if last_pass_fail == 0 and (s == bs.OK or s == bs.FAILED) then
last_pass_fail = nr
end
if s == bs.OK then --successful
self.lastSuccessful = nr
break;
end
nr = nr - 1
iter_counter = iter_counter + 1
if iter_counter > 10 then --check max last 10 builds when searching for successful build
break;
end
end
nr = r['number']
assert(nr > 0)
if last_pass_fail == 0 and (s == bs.OK or s == bs.FAILED) then
last_pass_fail = nr
if last_pass_fail ~= 0 then
self.lastChecked = last_pass_fail
end
if s == bs.OK then --successful
self.lastSuccessful = nr
break;
if last_result then
self.lastResult = last_result
end
nr = nr - 1
iter_counter = iter_counter + 1
if iter_counter > 10 then --check max last 10 builds when searching for successful build
break;
end
end
if last_pass_fail ~= 0 then
self.lastChecked = last_pass_fail
end
if last_result then
self.lastResult = last_result
end
end
function BB:getLastSuccessful()
return self.lastSuccessful
return self.lastSuccessful
end
function BB:getCurrent()
return self.lastResult['number']
return self.lastResult['number']
end
function BB:getCurrentStatus()
return self:_getBuildStatus(self.lastResult)
return self:_getBuildStatus(self.lastResult)
end
function BB:getBuilder()
return self.builder
return self.builder
end
function BB:getError()
return self.lastError
return self.lastError
end
local function getBuilderStatus(b)
local s = "[" .. b:getBuilder()
--check if json library was loaded correctly
if not json_status then
return s .. ".<span color=\"orange\">can't find libluaX.X-json</span>]"
end
local s = "[" .. b:getBuilder()
--check if json library was loaded correctly
if not json_status then
return s .. ".<span color=\"orange\">can't find libluaX.X-json</span>]"
end
local err = b:getError()
if err then
return s .. ".<span color=\"orange\">" .. err .. "</span>]"
end
local err = b:getError()
if err then
return s .. ".<span color=\"orange\">" .. err .. "</span>]"
end
if b:getLastSuccessful() ~= 0 then
success_build_nr_str = "<span color=\"green\">".. b:getLastSuccessful() .."</span>"
else
success_build_nr_str = "-"
end
if b:getLastSuccessful() ~= 0 then
success_build_nr_str = "<span color=\"green\">".. b:getLastSuccessful() .."</span>"
else
success_build_nr_str = "-"
end
local current_build_color = bc[b:getCurrentStatus()]
current_build_nr_str = "<span color=\""..current_build_color.."\">"..b:getCurrent().."</span>"
local current_build_color = bc[b:getCurrentStatus()]
current_build_nr_str = "<span color=\""..current_build_color.."\">"..b:getCurrent().."</span>"
if current_build_color ~= "green" then
s = s .. "." .. current_build_nr_str
end
return s .. "." .. success_build_nr_str .. "]"
if current_build_color ~= "green" then
s = s .. "." .. current_build_nr_str
end
return s .. "." .. success_build_nr_str .. "]"
end
-- {{{ Buildbot widget type
local function worker(format, warg)
if #bb == 0 then --fill up bb with builders when worker function is run for the first time
for i,v in pairs(warg) do
bb[#bb+1] = BB.create(v["url"], v["builder"])
if #bb == 0 then --fill up bb with builders when worker function is run for the first time
for i,v in pairs(warg) do
bb[#bb+1] = BB.create(v["url"], v["builder"])
end
end
end
local str = ""
for i,v in pairs(bb) do
v:refresh()
str = str .. " " .. getBuilderStatus(v)
end
return {str .. " "}
local str = ""
for i,v in pairs(bb) do
v:refresh()
str = str .. " " .. getBuilderStatus(v)
end
return {str .. " "}
end
-- }}}

View File

@ -12,13 +12,13 @@ local setmetatable = setmetatable
local os = { execute = os.execute }
local table = { insert = table.insert }
local string = {
find = string.find,
match = string.match,
format = string.format,
gmatch = string.gmatch
find = string.find,
match = string.match,
format = string.format,
gmatch = string.gmatch
}
local math = {
floor = math.floor
floor = math.floor
}
-- }}}
@ -29,36 +29,36 @@ local pulse = {}
-- {{{ Helper function
local function pacmd(args)
local f = io.popen("pacmd "..args)
if f == nil then
return nil
else
local line = f:read("*all")
f:close()
return line
end
local f = io.popen("pacmd "..args)
if f == nil then
return nil
else
local line = f:read("*all")
f:close()
return line
end
end
local function escape(text)
local special_chars = { ["."] = "%.", ["-"] = "%-" }
return text:gsub("[%.%-]", special_chars)
local special_chars = { ["."] = "%.", ["-"] = "%-" }
return text:gsub("[%.%-]", special_chars)
end
local cached_sinks = {}
local function get_sink_name(sink)
if type(sink) == "string" then return sink end
-- avoid nil keys
local key = sink or 1
-- Cache requests
if not cached_sinks[key] then
local line = pacmd("list-sinks")
if line == nil then return nil end
for s in string.gmatch(line, "name: <(.-)>") do
table.insert(cached_sinks, s)
if type(sink) == "string" then return sink end
-- avoid nil keys
local key = sink or 1
-- Cache requests
if not cached_sinks[key] then
local line = pacmd("list-sinks")
if line == nil then return nil end
for s in string.gmatch(line, "name: <(.-)>") do
table.insert(cached_sinks, s)
end
end
end
return cached_sinks[key]
return cached_sinks[key]
end
@ -66,55 +66,55 @@ end
-- {{{ Pulseaudio widget type
local function worker(format, sink)
sink = get_sink_name(sink)
if sink == nil then return {0, "unknown"} end
sink = get_sink_name(sink)
if sink == nil then return {0, "unknown"} end
-- Get sink data
local data = pacmd("dump")
if sink == nil then return {0, "unknown"} end
-- Get sink data
local data = pacmd("dump")
if sink == nil then return {0, "unknown"} end
-- If mute return 0 (not "Mute") so we don't break progressbars
if string.find(data,"set%-sink%-mute "..escape(sink).." yes") then
return {0, "off"}
end
-- If mute return 0 (not "Mute") so we don't break progressbars
if string.find(data,"set%-sink%-mute "..escape(sink).." yes") then
return {0, "off"}
end
local vol = tonumber(string.match(data, "set%-sink%-volume "..escape(sink).." (0x[%x]+)"))
if vol == nil then vol = 0 end
local vol = tonumber(string.match(data, "set%-sink%-volume "..escape(sink).." (0x[%x]+)"))
if vol == nil then vol = 0 end
return { math.floor(vol/0x10000*100), "on"}
return { math.floor(vol/0x10000*100), "on"}
end
-- }}}
-- {{{ Volume control helper
function pulse.add(percent, sink)
sink = get_sink_name(sink)
if sink == nil then return end
sink = get_sink_name(sink)
if sink == nil then return end
local data = pacmd("dump")
local data = pacmd("dump")
local pattern = "set%-sink%-volume "..escape(sink).." (0x[%x]+)"
local initial_vol = tonumber(string.match(data, pattern))
local pattern = "set%-sink%-volume "..escape(sink).." (0x[%x]+)"
local initial_vol = tonumber(string.match(data, pattern))
local vol = initial_vol + percent/100*0x10000
if vol > 0x10000 then vol = 0x10000 end
if vol < 0 then vol = 0 end
local vol = initial_vol + percent/100*0x10000
if vol > 0x10000 then vol = 0x10000 end
if vol < 0 then vol = 0 end
local cmd = string.format("pacmd set-sink-volume %s 0x%x >/dev/null", sink, vol)
return os.execute(cmd)
local cmd = string.format("pacmd set-sink-volume %s 0x%x >/dev/null", sink, vol)
return os.execute(cmd)
end
function pulse.toggle(sink)
sink = get_sink_name(sink)
if sink == nil then return end
sink = get_sink_name(sink)
if sink == nil then return end
local data = pacmd("dump")
local pattern = "set%-sink%-mute "..escape(sink).." (%a%a%a?)"
local mute = string.match(data, pattern)
local data = pacmd("dump")
local pattern = "set%-sink%-mute "..escape(sink).." (%a%a%a?)"
local mute = string.match(data, pattern)
-- 0 to enable a sink or 1 to mute it.
local state = { yes = 0, no = 1}
local cmd = string.format("pacmd set-sink-mute %s %d", sink, state[mute])
return os.execute(cmd)
-- 0 to enable a sink or 1 to mute it.
local state = { yes = 0, no = 1}
local cmd = string.format("pacmd set-sink-mute %s %d", sink, state[mute])
return os.execute(cmd)
end
-- }}}

View File

@ -9,8 +9,8 @@ local io = { popen = io.popen }
local setmetatable = setmetatable
local table = { insert = table.insert }
local string = {
gsub = string.gsub,
match = string.match
gsub = string.gsub,
match = string.match
}
-- }}}
@ -22,47 +22,47 @@ local sensors = {}
-- {{{ Split helper function
local function datasplit(str)
-- Splitting strings into associative array
-- with some magic to get the values right.
str = string.gsub(str, "\n", ":")
-- Splitting strings into associative array
-- with some magic to get the values right.
str = string.gsub(str, "\n", ":")
local tbl = {}
string.gsub(str, "([^:]*)", function (v)
if string.match(v, ".") then
table.insert(tbl, v)
end
end)
local tbl = {}
string.gsub(str, "([^:]*)", function (v)
if string.match(v, ".") then
table.insert(tbl, v)
end
end)
local assoc = {}
for c = 1, #tbl, 2 do
local k = string.gsub(tbl[c], ".*_", "")
local v = tonumber(string.match(tbl[c+1], "[%d]+"))
assoc[k] = v
end
local assoc = {}
for c = 1, #tbl, 2 do
local k = string.gsub(tbl[c], ".*_", "")
local v = tonumber(string.match(tbl[c+1], "[%d]+"))
assoc[k] = v
end
return assoc
return assoc
end
-- }}}
-- {{{ Sensors widget type
local function worker(format, warg)
-- Get data from all sensors
local f = io.popen("LANG=C sensors -uA")
local lm_sensors = f:read("*all")
f:close()
-- Get data from all sensors
local f = io.popen("LANG=C sensors -uA")
local lm_sensors = f:read("*all")
f:close()
local sensor_data = string.gsub(
local sensor_data = string.gsub(
string.match(lm_sensors, warg..":\n(%s%s.-)\n[^ ]"), " ", "")
-- One of: crit, max
local divisor = "crit"
local s_data = datasplit(sensor_data)
-- One of: crit, max
local divisor = "crit"
local s_data = datasplit(sensor_data)
if s_data[divisor] and s_data[divisor] > 0 then
s_data.percent = s_data.input / s_data[divisor] * 100
end
if s_data[divisor] and s_data[divisor] > 0 then
s_data.percent = s_data.input / s_data[divisor] * 100
end
return {s_data.input, tonumber(s_data.percent)}
return {s_data.input, tonumber(s_data.percent)}
end
-- }}}