File: automate_stdio.lua

package info (click to toggle)
monotone 1.1-9
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 20,984 kB
  • ctags: 8,622
  • sloc: cpp: 86,450; sh: 6,906; perl: 924; makefile: 813; python: 517; lisp: 379; sql: 118; exp: 91; ansic: 52
file content (89 lines) | stat: -rw-r--r-- 2,379 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
--
-- data: stdio data (as read from a stdout file)
-- err: expected error code (0, 1 or 2)
-- which: which command to check if data contains input of several command
--        (0 by default)
-- band: the contents of which band to return
--       ('m', the main content, by default)
--
function parse_stdio(data, err, which, band)
  local bands = {}
  local errcodes = {}

  local begin,End,headers = string.find(data, "(.-)\n\n");
  -- FIXME: expand this to a proper header parser if
  -- more headers are added in the future
  check(string.find(headers, "^format%-version: %d$") ~= nil)
  data = string.sub(data, End + 1)

  while true do
    local begin,End,cmdnum,bnd,size = string.find(data, "(%d+):(%l):(%d+):")
    if begin == nil then break end
    cmdnum = cmdnum + 0
    if bands[cmdnum] == nil then
      bands[cmdnum] = { m = "" }
    end
    local content = string.sub(data, End+1, End+size)
    if bnd == "m" then
      bands[cmdnum].m = bands[cmdnum].m .. content
    elseif bnd == "l" then
      errcodes[cmdnum] = tonumber(content)
    else
      if bands[cmdnum][bnd] == nil then
        bands[cmdnum][bnd] = {}
      end
      table.insert(bands[cmdnum][bnd], content)
    end
    data = string.sub(data, End + 1 + size)
  end

  if which == nil then
    which = 0
  end

  check(bands[which] ~= nil)
  check(errcodes[which] ~= nil)

  check(err == errcodes[which])

  if band == nil then
    band = "m"
  end

  if bands[which][band] == nil then
     bands[which][band] = {}
  end

  return bands[which][band]
end

-- make_stdio_cmd({"cmd", "arg", "arg"}, {{"opt", "val"}, {"opt", "val"}})
function make_stdio_cmd(cmd, args)
   local function lenstr(str)
      return str:len() .. ":" .. str
   end
   local ret = ""
   if args then
      ret = ret .. "o"
      for _, c in ipairs(args) do
	 ret = ret .. lenstr(c[1]) .. lenstr(c[2])
      end
      ret = ret .. "e"
   end
   ret = ret .. "l"
   for _, c in ipairs(cmd) do
      ret = ret .. lenstr(c)
   end
   ret = ret .. "e"
   return ret
end

function run_stdio(cmd, err, which, band)
  check(mtn("automate", "stdio"), 0, true, false, cmd)
  return parse_stdio(readfile("stdout"), err, which, band)
end

function run_remote_stdio(server, cmd, err, which, band)
  check(mtn2("automate", "remote_stdio", server.address), 0, true, false, cmd)
  return parse_stdio(readfile("stdout"), err, which, band)
end