Add -f flag for full output
[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 opts, args = getopt.getopt(sys.argv[1:], "1f")
27 for o,a in opts:
28     if o == '-1':
29         single = 1
30     elif o == '-f':
31         full = 1
32     else:
33         print __doc__
34         raise SystemExit
35
36 q = ('SELECT users.uid,email,reply,name,count(log.uid),comment FROM users'
37         ' LEFT JOIN log ON log.uid = users.uid'
38         ' WHERE email like %s GROUP BY email' )
39 for email in args:
40     pat = '%' + email + '%'
41     cursor.execute(q, (pat,))
42     for row in cursor:
43         uid,email,reply,name,count,comment = row
44         if reply == None:
45             reply = '-'
46         if single:
47             print email
48         else:
49             print '%d, %s, %s, %d, "%s"' % (uid, email, reply, count, name)
50             if full:
51                 print '#   ', comment