Mercurial > ~dholland > hg > swallowtail > index.cgi
comparison shelltools/query-pr/query.py @ 46:73e6dac29391
new stuff (checkpoint when moved between machines)
author | David A. Holland |
---|---|
date | Tue, 12 Aug 2014 21:55:08 -0400 |
parents | 298c8a7f5181 |
children | bcd1d06838fd |
comparison
equal
deleted
inserted
replaced
45:233dcc4d80e1 | 46:73e6dac29391 |
---|---|
47 cursor.execute(qtext, args) | 47 cursor.execute(qtext, args) |
48 result = cursor.fetchall() | 48 result = cursor.fetchall() |
49 cursor.close() | 49 cursor.close() |
50 return result | 50 return result |
51 # end querydb | 51 # end querydb |
52 | |
53 ############################################################ | |
54 # | |
55 | 52 |
56 ############################################################ | 53 ############################################################ |
57 # query class for searches | 54 # query class for searches |
58 | 55 |
59 class Query: | 56 class Query: |
110 # XXX | 107 # XXX |
111 assert(0) | 108 assert(0) |
112 # end daterange_constraint | 109 # end daterange_constraint |
113 | 110 |
114 ############################################################ | 111 ############################################################ |
115 # arg handling | |
116 | |
117 def getargs(classification_schemes, classification_schemetypes): | |
118 global outmaterial | |
119 global outformat | |
120 | |
121 p = argparse.ArgumentParser(program_description) | |
122 | |
123 # note: -h/--help is built in by default | |
124 p.add_argument("-v", "--version", | |
125 action='version' version=program_version, | |
126 help="Print program version and exit") | |
127 | |
128 p.add_argument("--short", | |
129 action='store_const', const="short", | |
130 dest='outmaterial', | |
131 help="Output in short form (one per line)") | |
132 p.add_argument("--headers", | |
133 action='store_const', const="headers", | |
134 dest='outmaterial', | |
135 help="Output in default form (headers only)") | |
136 p.add_argument("--full", | |
137 action='store_const', const="full", | |
138 dest='outmaterial', | |
139 help="Output in full form (all material)") | |
140 p.add_argument("--attach", nargs=1, | |
141 type=int, | |
142 action='store', | |
143 dest='attach', | |
144 help="Output specified attachment") | |
145 p.add_argument("--message", nargs=1, | |
146 type=int, | |
147 action='store', | |
148 dest='message', | |
149 help="Output specified message") | |
150 | |
151 p.add_argument("--text", | |
152 action='store_const', const="text", | |
153 dest='outformat', | |
154 help="Output as text (default)") | |
155 p.add_argument("--csv", | |
156 action='store_const', const="csv", | |
157 dest='outformat', | |
158 help="Output as comma-separated values") | |
159 p.add_argument("--xml", | |
160 action='store_const', const="xml", | |
161 dest='outformat', | |
162 help="Output as XML") | |
163 p.add_argument("--json", | |
164 action='store_const', const="json", | |
165 dest='outformat', | |
166 help="Output as JSON") | |
167 p.add_argument("--rdf", | |
168 action='store_const', const="rdf", | |
169 dest='outformat', | |
170 help="Output as RDF") | |
171 p.add_argument("--rdflike", | |
172 action='store_const', const="rdflike", | |
173 dest='outformat', | |
174 help="Output as RDF-like text") | |
175 | |
176 p.add_argument("--closed", | |
177 action='store_true', | |
178 dest='closed', default=False, | |
179 help="Search closed PRs as well as open") | |
180 p.add_argument("--public", | |
181 action='store_true', | |
182 dest='public', default=False, | |
183 help="Search only public information") | |
184 | |
185 p.add_argument("--number", nargs=1, | |
186 type=intrange, | |
187 dest='number', | |
188 help="Restrict search to a range of PRs by number") | |
189 p.add_argument("--synopsis", nargs=1, | |
190 dest='synopsis', | |
191 help="Search PRs by their synopsis field") | |
192 p.add_argument("--confidential", nargs=1, | |
193 type=yesno, choices=["yes", "no"], | |
194 dest='confidential', | |
195 help="Search PRs by their confidentiality setting") | |
196 p.add_argument("--state", nargs=1, | |
197 dest='state', | |
198 help="Search PRs by their state") | |
199 p.add_argument("--locked", nargs=1, | |
200 type=yesno, choices=["yes", "no"], | |
201 dest='locked', | |
202 help="Search PRs by their locked setting") | |
203 p.add_argument("--arrival-schemaversion", nargs=1, | |
204 type=intrange, | |
205 dest='arrival_schemaversion', | |
206 help="Search PRs by their original schema version") | |
207 p.add_argument("--arrival-date", nargs=1, | |
208 type=daterange, | |
209 dest='arrival_date', | |
210 help="Search PRs by arrival date") | |
211 p.add_argument("--closed-date", nargs=1, | |
212 type=daterange, | |
213 dest='closed_date', | |
214 help="Search PRs by the date they were closed") | |
215 p.add_argument("--last-modified", nargs=1, | |
216 type=daterange, | |
217 dest='last_modified', | |
218 help="Search PRs by the date they were last modified") | |
219 p.add_argument("--release", nargs=1, | |
220 dest='release', | |
221 help="Search PRs by their release information") | |
222 p.add_argument("--environment", nargs=1, | |
223 dest='environment', | |
224 help="Search PRs by their environment information") | |
225 | |
226 p.add_argument("--originator-name", nargs=1, | |
227 dest='originator_name', | |
228 help="Search PRs by the name of their originator") | |
229 p.add_argument("--originator-email", nargs=1, | |
230 dest='originator_email', | |
231 help="Search PRs by the email of their originator") | |
232 p.add_argument("--originator-id", nargs=1, | |
233 type=intrange, | |
234 dest='originator_id', | |
235 help="Search PRs by the database ID of their originator") | |
236 | |
237 | |
238 p.add_argument("--responsible", nargs=1, | |
239 dest='responsible', | |
240 help="Search PRs by who's responsible for them") | |
241 p.add_argument("--respondent", nargs=1, | |
242 dest='respondent', | |
243 help="Search PRs by who's a respondent") | |
244 p.add_argument("--subscribed", nargs=1, | |
245 dest='subscribed', | |
246 help="Search PRs by who's subscribed") | |
247 | |
248 p.add_argument("--messages", nargs=1, | |
249 dest='messages', | |
250 help="Search PRs by text in the message log") | |
251 p.add_argument("--adminlog", nargs=1, | |
252 dest='adminlog', | |
253 help="Search PRs by text in the administrative log") | |
254 p.add_argument("--anytext", nargs=1, | |
255 dest='anytext', | |
256 help="Search PRs by text in various fields") | |
257 | |
258 for scheme in classification_schemes: | |
259 p.add_argument("--%s" % scheme, nargs=1, | |
260 dest=scheme, | |
261 help="Search PRs by the %s classification scheme" % | |
262 scheme) | |
263 | |
264 p.add_argument("prs", nargs='*', metavar="PR", | |
265 type=int, | |
266 action='append', | |
267 dest='prs', | |
268 help="Specific PRs to retrieve by number") | |
269 | |
270 args = p.parse_args() | |
271 | |
272 if args.outmaterial is not None: | |
273 outmaterial = args.outmaterial | |
274 if args.outformat is not None: | |
275 outformat = args.outformat | |
276 | 112 |
277 # If we're doing something other than a search, do it now | 113 # If we're doing something other than a search, do it now |
278 if args.attach is not None: | 114 if args.attach is not None: |
279 get_attachment(args.attach) | 115 get_attachment(args.attach) |
280 exit(0) | 116 exit(0) |
429 queries.append(cq) | 265 queries.append(cq) |
430 # end loop | 266 # end loop |
431 | 267 |
432 querytexts = [q.textify() for q in queries] | 268 querytexts = [q.textify() for q in queries] |
433 return "INTERSECT\n".join(querytexts) | 269 return "INTERSECT\n".join(querytexts) |
270 | |
271 | |
272 ############################################################ | |
273 # arg handling | |
274 | |
275 class Invocation: | |
276 class Op: | |
277 # operation codes | |
278 OP_FIELDS = 1 | |
279 OP_SHOW = 2 | |
280 OP_RANGE = 3 | |
281 OP_SEARCH = 4 | |
282 | |
283 def __init__(self, op): | |
284 self.op = op | |
285 | |
286 def dofields(): | |
287 return Op(OP_FIELDS) | |
288 def doshow(field): | |
289 self = Op(OP_SHOW) | |
290 self.field = field | |
291 return self | |
292 def dorange(field): | |
293 self = Op(OP_RANGE) | |
294 self.field = field | |
295 return self | |
296 def doquery( | |
297 | |
298 | |
299 # output formats | |
300 FMT_TXT = 1 | |
301 | |
302 def __init__(self): | |
303 self.op = OP_SEARCH | |
304 self.searchstrings = [] | |
305 self.searchsql = [] | |
306 | |
307 # end Invocation | |
308 | |
309 def getargs(): | |
310 p = argparse.ArgumentParser(program_description) | |
311 | |
312 # note: -h/--help is built in by default | |
313 p.add_argument("-v", "--version", | |
314 action='version', version=program_version, | |
315 help="Print program version and exit") | |
316 | |
317 p.add_argument("--show", nargs=1, | |
318 action = 'store', dest='show', | |
319 help="Show description of field") | |
320 p.add_argument("--range", nargs=1, | |
321 action = 'store', dest='range', | |
322 help="Show range of extant values for field") | |
323 | |
324 p.add_argument("--search", nargs=1, | |
325 action = 'store', dest='search', | |
326 help="Force string to be read as a search string") | |
327 p.add_argument("-s", "--sql", nargs=1, | |
328 action = 'store', dest='sql', | |
329 help="Supply explicit sql query as search") | |
330 | |
331 p.add_argument("--open", | |
332 action = 'store_const', const="True", | |
333 dest = 'openonly', | |
334 help="Exclude closed PRs (default)") | |
335 p.add_argument("--closed", | |
336 action = 'store_const', const="False", | |
337 dest = 'openonly', | |
338 help="Include closed PRs in search") | |
339 p.add_argument("--public", | |
340 action = 'store_const', const="True", | |
341 dest = 'publiconly', | |
342 help="Exclude confidential PRs") | |
343 p.add_argument("--privileged", | |
344 action = 'store_const', const="False", | |
345 dest = 'publiconly', | |
346 help="Allow confidential PRs (default)") | |
347 | |
348 p.add_argument("--oldest", | |
349 action = 'store_const', const="OLDEST", | |
350 dest = 'order', | |
351 help="Sort output with oldest PRs first") | |
352 p.add_argument("--newest", | |
353 action = 'store_const', const="NEWEST", | |
354 dest = 'order', | |
355 help="Sort output with newest PRs first") | |
356 p.add_argument("--staleness", | |
357 action = 'store_const', const="STALENESS", | |
358 dest = 'order', | |
359 help="Sort output by time since last modification") | |
360 p.add_argument("--orderby", nargs=1, | |
361 action = 'store', dest = 'orderfield', | |
362 help="Sort output by specific field") | |
363 p.add_argument("--revorderby", nargs=1, | |
364 action = 'store', dest = 'revorderfield', | |
365 help="Sort output by specific field, reversed") | |
366 | |
367 p.add_argument("-m", "--message", nargs=1, | |
368 action = 'store', dest = 'message', | |
369 help="Print selected message (single PR only)") | |
370 p.add_argument("-a", "--attachment", nargs=1, | |
371 action = 'store', dest = 'attachment', | |
372 help="Print selected attachment (single PR only)") | |
373 | |
374 p.add_argument("-r", "--raw", | |
375 action = 'store_const', const="RAW", | |
376 dest = 'output', | |
377 help="Print exactly what the database returns") | |
378 p.add_argument("-l", "--list", | |
379 action = 'store_const', const="LIST", | |
380 dest = 'output', | |
381 help="Print in list form (default)") | |
382 p.add_argument("--headers", | |
383 action = 'store_const', const="HEADERS", | |
384 dest = 'output', | |
385 help="Print header information only") | |
386 p.add_argument("--meta", | |
387 action = 'store_const', const="META", | |
388 dest = 'output', | |
389 help="Print all metadata") | |
390 p.add_argument("--metadata", | |
391 action = 'store_const', const="META", | |
392 dest = 'output') | |
393 p.add_argument("-f", "--full", | |
394 action = 'store_const', const="FULL", | |
395 dest = 'output', | |
396 help="Print everything") | |
397 | |
398 p.add_argument("--text", | |
399 action = 'store_const', const="TEXT", | |
400 dest = 'outformat', | |
401 help="Print in text format (default)") | |
402 p.add_argument("--csv", | |
403 action = 'store_const', const="CSV", | |
404 dest = 'outformat', | |
405 help="Print a CSV file") | |
406 p.add_argument("--xml", | |
407 action = 'store_const', const="XML", | |
408 dest = 'outformat', | |
409 help="Print in XML") | |
410 p.add_argument("--json", | |
411 action = 'store_const', const="JSON", | |
412 dest = 'outformat', | |
413 help="Print in JSON") | |
414 p.add_argument("--rdf", | |
415 action = 'store_const', const="RDF", | |
416 dest = 'outbformat', | |
417 help="Print in RDF") | |
418 p.add_argument("--rdflike", | |
419 action = 'store_const', const="RDFLIKE", | |
420 dest = 'outformat', | |
421 help="Print RDF-like text") | |
422 | |
423 args = p.parse_args() | |
424 | |
425 p = Invocation() | |
426 | |
427 if args.show is not None: | |
428 do_showfield(args.show) | |
429 exit(0) | |
430 if args.range is not None: | |
431 do_fieldrange(args.range) | |
432 exit(0) | |
433 | |
434 searchstring = args.search | |
435 explicitsql = args.sql | |
436 | |
437 openonly = args.openonly | |
438 if openonly is None: | |
439 openonly = True | |
440 publiconly = args.publiconly | |
441 if publiconly is None: | |
442 publiconly = False | |
443 | |
444 if args.orderfield is not None: | |
445 orderby = args.orderfield | |
446 orderrev = False | |
447 elif args.revorderfield is not None: | |
448 orderby = args.revorderfield | |
449 orderrev = True | |
450 elif args.order == "OLDEST": | |
451 orderby = "number" | |
452 orderrev = False | |
453 elif args.order == "NEWEST": | |
454 orderby = "number" | |
455 orderrev = True | |
456 elif args.order == "STALENESS": | |
457 orderby = "last-modified" | |
458 orderrev = True | |
459 else: | |
460 orderby = "number" | |
461 orderrev = False | |
462 | |
463 if args.message is not None: | |
464 printwhat = "MESSAGE" | |
465 printwhich = args.message | |
466 elif args.attachment is not None: | |
467 printwhat = "ATTACHMENT" | |
468 printwhich = args.attachment | |
469 else: | |
470 printwhat = "PR" | |
471 printwhich = None | |
472 | |
473 output = args.output | |
474 if output is None: | |
475 output = "LIST" | |
476 | |
477 outformat = args.outformat | |
478 if outformat is None: | |
479 outformat = "TEXT" | |
480 | |
481 query = buildquery(searchstring, explicitsql, | |
482 orderby=orderby, orderrev=orderrev, | |
483 openonly=openonly, publiconly=publiconly) | |
484 if printwhat == "PR": | |
485 printer = buildprinter(output, outformat) | |
486 else if printwhat == "MESSAGE": | |
487 printer = getmessage(printwhich) | |
488 else if printwhat == "ATTACHMENT": | |
489 printer = getattachment(printwhich) | |
490 | |
491 return (query, printer) | |
434 # end getargs | 492 # end getargs |
435 | 493 |
436 ############################################################ | 494 ############################################################ |
437 # main | 495 # main |
438 | 496 |
439 def main(): | 497 def main(): |
498 | |
440 opendb() | 499 opendb() |
441 (classification_schemes, classification_schemetypes) = getclassify() | 500 (classification_schemes, classification_schemetypes) = getclassify() |
442 query = getargs(classification_schemes, classification_schemetypes) | 501 query = getargs(classification_schemes, classification_schemetypes) |
443 ids = querydb(query) | 502 ids = querydb(query) |
444 if len(ids) > 0: | 503 if len(ids) > 0: |