Annotate

tools/migration/main.lua @ 4214:1674cd17557c

tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
author Matthew Wild <mwild1@gmail.com>
date Fri, 25 Feb 2011 03:32:44 +0000
parent 4213:8f169a92b4ba
child 4216:ff80a8471e86
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4214
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
1 local default_config = "./migrator.cfg.lua";
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
2
4213
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
3 -- Command-line parsing
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
4 local options = {};
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
5 local handled_opts = 0;
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
6 for i = 1, #arg do
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
7 if arg[i]:sub(1,2) == "--" then
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
8 local opt, val = arg[i]:match("([%w-]+)=?(.*)");
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
9 if opt then
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
10 options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
11 end
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
12 handled_opts = i;
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
13 else
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
14 break;
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
15 end
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
16 end
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
17 table.remove(arg, handled_opts);
4197
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
18
4213
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
19 -- Load config file
4214
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
20 local function loadfilein(file, env)
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
21 if loadin then
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
22 return loadin(env, io.open(file):read("*a"));
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
23 else
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
24 local chunk, err = loadfile(file);
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
25 if chunk then
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
26 setfenv(chunk, env);
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
27 end
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
28 return chunk, err;
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
29 end
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
30 end
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
31
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
32 local config_file = options.config or default_config;
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
33 local from_store = arg[1] or "input";
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
34 local to_store = arg[2] or "output";
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
35
4197
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
36 config = {};
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
37 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
4214
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
38 local config_chunk, err = loadfilein(config_file, config_env);
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
39 if not config_chunk then
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
40 print("There was an error loading the config file, check the file exists");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
41 print("and that the syntax is correct:");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
42 print("", err);
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
43 os.exit(1);
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
44 end
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
45
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
46 config_chunk();
4197
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
47
4213
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
48 if not package.loaded["util.json"] then
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
49 package.path = "../../?.lua;"..package.path
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
50 package.cpath = "../../?.dll;"..package.cpath
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
51 end
4197
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
52
4214
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
53 local have_err;
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
54 if #arg > 0 and #arg ~= 2 then
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
55 have_err = true;
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
56 print("Error: Incorrect number of parameters supplied.");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
57 end
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
58 if not config[from_store] then
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
59 have_err = true;
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
60 print("Error: Input store '"..from_store.."' not found in the config file.");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
61 end
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
62 if not config[to_store] then
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
63 have_err = true;
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
64 print("Error: Output store '"..to_store.."' not found in the config file.");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
65 end
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
66 if not config[from_store].type then
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
67 have_err = true;
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
68 print("Error: Input store type not specified in the config file");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
69 elseif not pcall(require, config[from_store].type) then
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
70 have_err = true;
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
71 print("Error: Unrecognised store type for '"..from_store.."': "..config[from_store].type);
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
72 end
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
73 if not config[to_store].type then
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
74 have_err = true;
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
75 print("Error: Output store type not specified in the config file");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
76 elseif not pcall(require, config[to_store].type) then
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
77 have_err = true;
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
78 print("Error: Unrecognised store type for '"..to_store.."': "..config[to_store].type);
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
79 end
4213
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
80
4214
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
81 if have_err then
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
82 print("");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
83 print("Usage: "..arg[0].." FROM_STORE TO_STORE");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
84 print("If no stores are specified, 'input' and 'output' are used.");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
85 print("");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
86 print("The available stores in your migrator config are:");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
87 print("");
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
88 for store in pairs(config) do
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
89 print("", store);
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
90 end
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
91 os.exit(1);
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
92 end
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
93
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
94 local itype = config[from_store].type;
1674cd17557c tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4213
diff changeset
95 local otype = config[to_store].type;
4213
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
96 local reader = require(itype).reader(config[from_store]);
8f169a92b4ba tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4197
diff changeset
97 local writer = require(otype).writer(config[to_store]);
4197
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
98
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
99 local json = require "util.json";
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
100
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
101 for x in reader do
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
102 --print(json.encode(x))
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
103 writer(x);
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
104 end
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
105 writer(nil); -- close
bef732980436 tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4193
diff changeset
106