Created
September 13, 2012 02:23
-
-
Save elprup/3711451 to your computer and use it in GitHub Desktop.
syncuser command from svn to reviewboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
''' | |
sync user from svn to review board | |
sample usage: | |
syncuser.py -c root:password@localhost:rbdatabase --update /var/svn/passwd | |
''' | |
import datetime | |
import getopt | |
import MySQLdb as mdb | |
import re | |
import sys | |
def usage(): | |
usagetips = '''syncuser [OPTIONS] password_file | |
-c review board mysql connection string | |
-t test mode | |
--no-insert insert non record | |
--update update old user info | |
-h --help help document | |
-v verbose information display | |
''' | |
print usagetips | |
def verbose_print(message, is_verbose): | |
if is_verbose: | |
print message | |
def get_authlist(file): | |
user_passwd_list = [] | |
fd = open(file, 'r') | |
for line in fd.xreadlines(): | |
user_passwd = re.split('[:=]', line.strip()) | |
if len(user_passwd) != 2: | |
continue | |
user_passwd_list.append(user_passwd) | |
fd.close() | |
return user_passwd_list | |
def get_db_connection(mysqlstring): | |
mysqlinfo = re.split('[:@/]', mysqlstring) | |
if len(mysqlinfo) != 4: | |
return None | |
else: | |
user, passwd, host, database = mysqlinfo | |
conn = mdb.connect(host, user, passwd, database) | |
return conn | |
def get_hexdigest(algorithm, salt, raw_password): | |
if algorithm == 'sha1': | |
import sha | |
return sha.new(salt + raw_password).hexdigest() | |
raise ValueError("Got unknown password algorithm type in password.") | |
def crypt_passwd(raw_password): | |
import random | |
algo = 'sha1' | |
salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5] | |
hsh = get_hexdigest(algo, salt, raw_password) | |
encoded_passwd = '%s$%s$%s' % (algo, salt, hsh) | |
return encoded_passwd | |
def sync_user(passwdfile, mysqlstring, insert, update, test, verbose): | |
authlist = get_authlist(passwdfile) | |
mysql_conn = get_db_connection(mysqlstring) | |
cur = mysql_conn.cursor() | |
for user, passwd in authlist: | |
# check whether exists | |
cur.execute(r'SELECT id FROM auth_user WHERE username=%s LIMIT 1', [user]) | |
verbose_print('Query name:%s' % user, verbose) | |
old_record = cur.fetchone() | |
if old_record != None and update: | |
# existed record | |
if not test: | |
cur.execute(r'UPDATE auth_user SET password=%s WHERE id=%s', | |
[crypt_passwd(passwd), old_record[0]]) | |
verbose_print('UPDATE name:%s, passwd:%s, id:%s' % (user, passwd, old_record[0]) | |
, verbose) | |
elif old_record == None and insert: | |
# new record | |
if not test: | |
now = datetime.datetime.now() | |
cur.execute(r'INSERT INTO auth_user VALUES (NULL,%s,"","",%s,%s,0,1,0,%s,%s)', | |
[user, user+'@papayamobile.com', crypt_passwd(passwd), now, now]) | |
verbose_print('INSERT name:%s, passwd:%s' % (user, passwd), verbose) | |
mysql_conn.close() | |
def main(argv): | |
try: | |
opts, args = getopt.gnu_getopt(argv, 'c:thv', ['no-insert', 'update', 'help']) | |
except: | |
usage() | |
sys.exit(2) | |
mysqlstring = '' | |
testmode = False | |
insertmode = True | |
updatemode = False | |
verbose = False | |
for o, a in opts: | |
if o == '-c': | |
mysqlstring = a | |
elif o == '-t': | |
testmode = True | |
elif o in ('-h', '--help'): | |
usage() | |
sys.exit() | |
elif o == '-v': | |
verbose = True | |
elif o == '--no-insert': | |
insertmode = False | |
elif o == '--update': | |
updatemode = True | |
if len(args) == 1: | |
passwdfile = args[0] | |
else: | |
print 'Oops: password file needed.\n' | |
usage() | |
sys.exit(2) | |
if not testmode and mysqlstring == '': | |
print 'Oops: -c is needed if not test.\n' | |
usage() | |
sys.exit(2) | |
sync_user(passwdfile, mysqlstring, insertmode, updatemode, testmode, verbose) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment