dump.lua

Wed, 23 Sep 2020 14:25:20 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 23 Sep 2020 14:25:20 +0100
changeset 13
9bd292b35f23
permissions
-rw-r--r--

Add dump.lua to write state graphs to file

13
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local statewalk = require("luatraverse");
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local format = string.format;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local type = type;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local tostring = tostring;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local io, os = io, os;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local getsize;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 do
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local ok, ret = pcall(require, "getsize");
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 if ok then
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 --luacheck: ignore 143/debug
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 if type(ret) == "function" then
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 getsize = ret;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 elseif debug.getsize then
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 getsize = debug.getsize;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local function dump_state(f, config)
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if io.type(f) ~= "file" then
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local err;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 f, err = io.open(f, "w+");
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 if not f then return false, err; end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local metadata = config and config.metadata;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local start_time = os.time();
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 f:write("# Lua state graph", "\n");
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 f:write("# Generated by dump.lua v2.1", "\n");
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 f:write("# at ", os.date("!%F %R UTC", start_time), "\n");
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if metadata then
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local keys = {};
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 for k in pairs(metadata) do
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 table.insert(keys, k);
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 table.sort(keys);
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if #keys > 0 then
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 for _, k in ipairs(keys) do
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 f:write("## ", k, ": ", metadata[k], "\n");
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local seenobjects, id = {}, 0;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local function edge(from, to, how, value)
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 if from and (not seenobjects[from] or seenobjects[from] == true) then
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 id = id + 1;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 seenobjects[from] = id;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 f:write("V:", id, ":", type(from), ":", (getsize and getsize(from)) or "?", "\n");
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if not seenobjects[to] or seenobjects[to] == true then
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 id = id + 1;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 seenobjects[to] = id;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 f:write("V:", id, ":", type(to), ":", (getsize and getsize(to)) or "?", "\n");
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 if seenobjects[from] == true or seenobjects[to] == true then
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 return;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local value_rep = (value and format("%q", tostring(value))) or "?";
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 f:write("E:", seenobjects[from] or "?", ":", seenobjects[to], ":", tostring(how or "?"), ":", value_rep, "\n");
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local ignore = {f, dump_state, statewalk, seenobjects, edge};
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 statewalk.traverse({edge=edge}, ignore, seenobjects);
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 f:write("# Completed in ", tostring(os.time() - start_time), "s", "\n");
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 f:close();
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 return {
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 dump_state = dump_state;
9bd292b35f23 Add dump.lua to write state graphs to file
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 };

mercurial