]> matita.cs.unibo.it Git - helm.git/blob - matita/scripts/functions.lua
tagged 0.5.0-rc1
[helm.git] / matita / scripts / functions.lua
1 #!/usr/bin/lua5.1
2
3 local tool_re = "^([%w%.]+)"
4 local test_re = "([%w%./_-]+)"
5 local rc_re = string.char(27).."%[%d+;%d+m([%a]+)"..string.char(27).."%[0m"
6 local time_re = "(%d+m%d+%.%d+s)"
7 local mark_re = "(%d+)"
8 local opt_re = "(gc%-%a+)$"
9 local sep = "%s+"
10
11 local fmt = "INSERT bench "..
12         "(result, compilation, test, time, timeuser, mark, options) "..
13         "VALUES ('%s', '%s', '%s', %d, %d, '%s', '%s');\n"
14
15 -- build the huge regex
16 local re = tool_re .. sep .. test_re .. sep .. rc_re .. sep .. 
17         time_re .. sep ..time_re .. sep ..time_re .. sep .. 
18         mark_re .. sep .. opt_re 
19
20 -- the string to cents of seconds function
21 function s2t(s)
22         local _,_,min,sec,cents = string.find(s,"(%d+)m(%d+)%.(%d+)s")
23         return min * 6000 + sec * 100 + cents
24 end
25 function t2s(s)
26   s = tonumber(s)
27   local min, sec, cents
28   cents = s % 100
29   min = math.floor(s / 6000)
30   sec = math.floor((s - min * 6000 - cents) / 100)
31   return string.format("%dm%02d.%02ds",min,sec,cents)
32 end
33
34 -- logfile to sql conversion
35 function log2sql(file)
36 local t = {}
37 local f = io.stdin
38 if file ~= "-" then 
39         f = io.open(file,"r")
40 end
41 for l in f:lines() do
42         local x,_,tool,test,rc,real,user,_,mark,opt = string.find(l,re)
43
44         if x == nil then
45     error("regex failed on '"..l.."'")
46         end
47
48         -- check the result is valid
49         if tool ~= "matitac" and tool ~= "matitac.opt" then
50                 error("unknown tool " .. tool)
51         else
52                 if tool == "matitac" then tool = "byte" else tool = "opt" end
53         end
54         if rc ~= "OK" and rc ~= "FAIL" then
55                 error("unknown result " .. rc)
56         else
57                 rc = string.lower(rc)
58         end
59         if opt ~= "gc-on" and opt ~= "gc-off" then
60                 error("unknown option "..opt)
61         end
62         real = s2t(real)
63         user = s2t(user)
64
65         -- print the sql statement
66         table.insert(t,string.format(fmt,rc,tool,test,real,user,mark,opt))
67 end
68 return table.concat(t)
69 end
70
71 -- proportions
72 function proportion(x,y,a,b)
73   local rc
74   if x == "x" then
75     rc = y * a / b
76   elseif y == "x" then
77     rc = x * b / a
78   elseif a == "x" then
79     rc = x * b / y
80   elseif b == "x" then
81     rc = y * a / x
82   end
83   return string.format("%d",rc)
84 end
85
86 -- conversion from the old db to the new one
87 function convertsql(file)
88   local f = io.open(file)
89   return string.gsub(f:read("*a"),time_re,
90     function(s)
91       return s2t(s)
92     end)
93 end
94
95 -- call the desired function and print the result
96 fun = arg[1]
97 table.remove(arg,1)
98 f = _G[fun] or error("no " .. fun .. " defined")
99 print(f(unpack(arg)) or "")
100
101 -- eof