1#!/usr/bin/python
2
3import common
4import MySQLdb
5import sys
6from autotest_lib.client.common_lib import global_config
7
8if (len(sys.argv) < 2 or
9    [arg for arg in sys.argv[1:] if arg.startswith('-')]):
10    print "Usage: %s username [username ...]" %sys.argv[0]
11    sys.exit(1)
12
13config = global_config.global_config
14section = 'AUTOTEST_WEB'
15host = config.get_config_value(section, "host")
16db_name = config.get_config_value(section, "database")
17user = config.get_config_value(section, "user")
18password = config.get_config_value(section, "password")
19
20con = MySQLdb.connect(host=host, user=user,
21                      passwd=password, db=db_name)
22cur = con.cursor()
23
24for username in sys.argv[1:]:
25    cur.execute("""
26        SELECT access_level
27        FROM afe_users
28        WHERE login = %s""", username)
29    row = cur.fetchone()
30
31    if row is None:
32        print "User %s does not exist. Creating..." % username
33        cur.execute("""
34            INSERT INTO afe_users (login, access_level)
35            VALUES (%s, 100)""", username)
36        print "    Done"
37    else:
38        print "Updating user %s..." % username
39        cur.execute("""
40            UPDATE afe_users
41            SET access_level = 100
42            WHERE login = %s""", username)
43        if (cur.rowcount == 1):
44            print "    Done"
45        else:
46            print "    %s is already a superuser!" % username
47
48cur.close()
49con.commit()
50con.close()
51