Add help option to finduser
[omc-tools.git] / finduser
1 #! /usr/bin/env python
2 """finduser [flags] pattern...
3
4 Flags:
5     -1   Print just email
6     -f   Also print the comment
7
8 Arguments is a list of SQL paterns (will get wrapped in wildcards, %),
9 by default display full information as CSV.
10 """
11
12 import mysql.connector
13 import datetime, os, re, subprocess, sys, string, random
14 import getopt
15
16 dbconfig = {
17         'user': 'licensereader',
18         'password': open('ropass.txt').read().strip(),
19         'database': 'license'
20         }
21 conn = mysql.connector.connect(**dbconfig)
22 cursor = conn.cursor()
23
24 single = 0
25 full = 0
26 try:
27     opts, args = getopt.getopt(sys.argv[1:], "1f")
28 except:
29     print __doc__
30     raise SystemExit
31
32 for o,a in opts:
33     if o == '-1':
34         single = 1
35     elif o == '-f':
36         full = 1
37
38 q = ('SELECT users.uid,email,reply,name,count(log.uid),comment FROM users'
39         ' LEFT JOIN log ON log.uid = users.uid'
40         ' WHERE email like %s GROUP BY email' )
41 for email in args:
42     pat = '%' + email + '%'
43     cursor.execute(q, (pat,))
44     for row in cursor:
45         uid,email,reply,name,count,comment = row
46         if reply == None:
47             reply = '-'
48         if single:
49             print email
50         else:
51             print '%d, %s, %s, %d, "%s"' % (uid, email, reply, count, name)
52             if full:
53                 print '#   ', comment