diff --git a/job_submit_fx/functions.lua b/job_submit_fx/functions.lua index 05f90b0..4102e92 100644 --- a/job_submit_fx/functions.lua +++ b/job_submit_fx/functions.lua @@ -1,132 +1,143 @@ local f = {} -- local curl = require("cURL") local json = require("json") local http_h = require("http.headers") local http_r = require("http.request") -- convert list to set -- it facilitates checking if a value exist in the list -function f.utils_Set(list) +function f.list2set(list) local set = {} for _, l in ipairs(list) do set[l] = true end return set end +-- convert list to a string : {'a', 'b'} => ["a", "b"] +function f.list2str(list) + _list = {} + for _, l in ipairs(list) do + if type(l) == "string" then _i = '"'..l..'"' else _i = l end + table.insert(_list,_i) + end + str = '['..table.concat(_list,',')..']' + return str +end + -- printf equivalent function f.printf(s,...) return io.write(s:format(...)) end -- sprintf equivalent function f.sprintf(s,...) return s:format(...) end -- format date with %Y-%m-%dT00:00:00 format function f.format_date(year,month,day) return os.date('%Y-%m-%dT%H:%M:%S',os.time({year=year,month=month,day=day,hour=0,min=0,sec=0})) end -- print table function f.dump_table(t,size) if not t then return print(nil) end if not size then size = 0 end for key, value in pairs(t) do if size ~= 0 then for i=1,size do io.write('=') end io.write(' ') end if type(value) == 'table' then print(key) dump_table(value,size+2) else print(key, value) end end end -- http get function f.http_get(url, headers, data) local ret = nil local req = http_r.new_from_uri(url) for name, value in pairs(headers) do req.headers:append(name,value) end req.headers:upsert("content-type", "application/json") if data ~= nil then req.headers:upsert(":method", "GET") req:set_body(data) end -- slurm.log_user("url : "..url) local headers_r, stream = assert(req:go()) local body = assert(stream:get_body_as_string()) if headers_r:get ":status" == "200" then ret = json.decode(body) end return ret end function f.log_user_and_debug(fmt, ...) --[[Implicit definition of arg was removed in Lua 5.2]]-- local arg = {...} slurm.log_user(fmt, unpack(arg)) slurm.log_debug(fmt, unpack(arg)) end -- -- collect function for curl -- function collect(chunk) -- if chunk ~= nil then -- res = res .. chunk -- end -- return true -- end -- -- curl get -- function f.curl_get(url, headers) -- res = '' -- ret = nil -- curl.easy{ -- url = url, -- httpheader = headers, -- writefunction = collect -- --writefunction = io.stderr -- use io.stderr:write() -- } -- :perform() -- :close() -- if res ~= nil then -- decode = json.decode(res) -- end -- if decode ~= nil then -- if decode.found then -- ret = decode -- end -- end -- return ret -- end -- -- curl post -- function f.curl_post(url, headers, data) -- res = "" -- ret = nil -- curl.easy{ -- url = url, -- post = true, -- httpheader = headers, -- postfields = data, -- writefunction = collect -- } -- :perform() -- :close() -- if res ~= nil then -- decode = json.decode(res) -- end -- if not decode.time_out and decode ~= nil or decode.status == 200 then -- ret = decode -- end -- return ret -- end return f