File: upload-messages.lua

package info (click to toggle)
lua-gtk 0.9%2B20100528-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,176 kB
  • ctags: 1,934
  • sloc: ansic: 9,571; sh: 373; makefile: 241
file content (289 lines) | stat: -rwxr-xr-x 6,687 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#! /usr/bin/env lua
-- vim:sw=4:sts=4
--
-- Extract all messages from the specified module's source files, and upload
-- new and changed messages to the server.
--
-- Arguments: name of the module to process
--
-- This is work in progress.  The "server" is a local installation of a
-- Lua based CMS that I haven't published anywere yet.  It has a module
-- that is a rough but working tool to translate messages via the web.
--

require "lfs"
require "socket.http"

-- configuration
server_url = "http://localhost/nab2/index.lua?q="
cookie = nil
-- end configuration


---
-- Parse a Lua data structure given as string.
-- from nab2/lib/util.lua
--
function deserialize(s)
    assert(type(s) == "string")
    if s == "" then return nil end
    local chunk = assert(loadstring("return " .. s))
    setfenv(chunk, {})
    return assert(chunk())
end




-- metatable for module
MOD = {}
MOD.__index = MOD

function new_module(modname)
    -- messages: key=ID, value={ msg, {loc, ...}}
    -- loc: { filename, linenumber, function }
    local o = { modname=modname, err_cnt=0, messages={}, languages={} }
    setmetatable(o, MOD)
    return o
end

function MOD:err(msg, ...)
    if select('#', ...) > 0 then
	msg = string.format(msg, ...)
    end
    print(string.format("%s(%d): %s", self.curr_fname, self.curr_line_nr, msg))
    self.err_cnt = self.err_cnt + 1
end



---
-- Read one C source file to extract all messages.
--
function MOD:process_source()
    self.curr_id = nil
    self.curr_fnname = nil
    for line in io.lines(self.curr_fname) do
	self.curr_line_nr = self.curr_line_nr + 1
	self:process_source_line(line)
    end
end

local macros = {
    "(LG_ERROR)%((.*)",		-- args: id, fmt, ...
    "(LG_ARGERROR)%((.*)",	-- args: nr, id, fmt, ...
    "(LG_MESSAGE)%((.*)",	-- args: id, msg
}


---
-- Read one line of a C source file to find usages of messages.
--
function MOD:process_source_line(line)
    local s, macro, args

    if line == "" then return end

    -- looking for more continuation lines
    if self.curr_line then
	-- remove trailing " (if possible)
	s = string.gsub(self.curr_line, '"%s*$', '')
	if s ~= self.curr_line then
	    line = string.gsub(line, '^%s*"', '')    -- remove leading "
	end
	self.curr_line = s .. line
	self:try_commit_message()
	return
    end

    -- look for new functions
    s = string.match(line, '^%w[%w ]-([%w_]+)%(')
    if s then
	self.curr_fnname = s
	return
    end

    -- look for new messages.
    for _, macroname in ipairs(macros) do
	macro, args = string.match(line, macroname)
	if macro then
	    self.curr_macro = macro
	    self.curr_line = args
	    self.curr_macro_line = self.curr_line_nr
	    self:try_commit_message()
	    break
	end
    end
end


---
-- The line buffer self.curr_line might be a complete statement, i.e. end
-- with a semicolon.  In this case, analyze it.
--
function MOD:try_commit_message()
    if string.sub(self.curr_line, -1) ~= ";" then return end

    -- for this macro, discard the leading argument number
    if self.curr_macro == "LG_ARGERROR" then
	self.curr_line = string.match(self.curr_line, '^[^,]+, *(.*)')
    end

    -- the rest is id, "message"
    id, msg = string.match(self.curr_line, '^(%d+),%s*"([^"]+)"')
    if id then
	self:commit_message(id, msg, self.curr_macro_line)
    end

    self.curr_macro = nil
    self.curr_line = nil
    self.curr_macro_line = nil
end


---
-- The current message should be stored.
--
function MOD:commit_message(id, msg, line_nr)

    ar = self.messages[id]
    if ar then
	if ar[1] ~= msg then
	    self:err("Redefinition of message %s %s", self.modname, id)
	    return
	end
    else
	-- new message
	ar = { msg, {} }
	self.messages[id] = ar
    end

    -- add the current location: filename, line, function
    table.insert(ar[2], { self.curr_fname, line_nr, self.curr_fnname })
end


-- fetch a list of existing messages with a hash value of name/
function MOD:get_existing_messages()
    local res = {}
    local body = "op=fetch&module=" .. self.modname
    socket.http.request {
	url = server_url .. "luagnome/upload",
	method = "POST",
	headers = { Cookie=cookie, ["Content-Length"] = #body },
	source = ltn12.source.string(body),
	sink = ltn12.sink.table(res),
    }

    res = table.concat(res)

    print("existing messages", res)

    res = deserialize(res)

    -- XXX evaluate the result
    -- res: array of entries with { msg_id, msg_nr, hashvalue }
    -- hashvalue is supposed to be a md5(message text .. locations)
end

-- Check for new/changed messages and upload them.
function MOD:upload_changes()

    -- XXX for each message, compute the hashvalue.  if it differs or no
    -- old message exists, append to an array to upload.  then serialize
    -- it and upload unless empty.

end


---
-- Make a string with the locations where a message is used.
-- @param msg  An entry from the "messages" array.
-- @return  A string with the locations.
--
function locations(msg)
    local buf = {}
    for _, loc in ipairs(msg[2]) do
	if loc[3] then
	    buf[#buf + 1] = string.format("%s(%s):%s()", loc[1], loc[2], loc[3])
	else
	    buf[#buf + 1] = string.format("%s(%s)", loc[1], loc[2])
	end
    end
    return table.concat(buf, ", ")
end

function MOD:process()
    local path, lang
    
    path = "src/" .. self.modname

    if lfs.attributes(path, "mode") ~= "directory" then
	print("Invalid module " .. self.modname)
	return 2
    end

    for fname in lfs.dir(path) do
	self.curr_fname = path .. "/" .. fname
	self.curr_line_nr = 0

	if string.match(fname, "%.c$") then
	    self:process_source()
	end
    end

    self:get_existing_messages()
    self:upload_changes()

end

-- Connect to the server, i.e. login
function server_login()
    local body, status, header, status2 = socket.http.request(
	server_url .. "login",
	"user=admin&pass=passme\n")
    if tostring(status) == "200" then
	for k,v in pairs(header) do
	    if k == "set-cookie" then
		cookie = string.match(v, "^[^;]+")
		print("COOKIE", cookie)
	    end
	end
    end

    if not cookie then
	print("Failed to login to server.")
	os.exit(1)
    end
end

-- Disconnect, i.e. logout
function server_logout()
    local t = {}
    local body, status, header, status2 = socket.http.request {
	url = server_url .. "logout",
	headers = { Cookie=cookie },
	sink = ltn12.sink.table(t)
    }
    print("LOGOUT", body, status, header, status2, table.concat(t))
end

function main()
    local modname, mod, rc

    modname = arg[1]
    if not modname then
	print "Required argument: a module's name"
	return 1
    end

    server_login()
    if true then
	mod = new_module(modname)
	rc = mod:process()
    end
    server_logout()
end

os.exit(main())