SVN与ReviewBoard联动(2)

接上一篇。

1、脚本写了没多久,但郁闷的是测通上面的脚本我花了很久,后来发现主要是字符集的问题。

我用的是Debian默认字符集为UTF8,python2.7默认字符集为ASCII。
后面,设置了python的字符集默认为UTF8,第一篇的脚本才测通了。
/usr/lib/python2.7/sitecustomize.py

# install the apport exception handler if available
import sys

sys.setdefaultencoding('UTF8')

try:
    import apport_python_hook
except ImportError:
    pass
else:
    apport_python_hook.install()

2、然后发现RBT貌似不支持UTF8的中文参数,反正我没有测通,好吧,自己写了一个脚本将SVN上的中文注释更新到mysql中。
说明一下,RBT的这个问题,我已经向社区提交Bug了,亲测补丁有效,大家等待下一版本就好了。

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on 2016-11-24
@author: Hansen
用于更新rb的说明
'''

import sys
import datetime
import MySQLdb
import nsvnutils

#记录日志
def nlog(logpath,msg):
    fo = open(logpath,'a+')
    try:
        fo.write(msg+'\n')
    finally:
        fo.close()

#更新rb的reviewrequest说明
def update_rb_comment():
    #获取连接
    try:
        connection=MySQLdb.connect(user='user',passwd="passwd",host='127.0.0.1',port=3306,db='db',charset='utf8')
        subconnection=MySQLdb.connect(user='user',passwd="passwd",host='127.0.0.1',port=3306,db='db',charset='utf8')
        #print('mysql connection ok')
        nlog('/mnt/rb/sql.log','mysql connection ok')
    except Exception as ex:
        #print(str(ex))
        nlog('/mnt/rb/sql.log',str(ex))
        return

    #获取需要处理的数据
    try:
        cursor=connection.cursor()
	subcursor=subconnection.cursor()
        sql="select id,summary from reviews_reviewrequest r where r.description='' or r.description is null"
        cursor.execute(sql)
        #print('mysql select ok')
        nlog('/mnt/rb/sql.log','mysql select ok')
        for row in cursor.fetchall():
            rid=str(row[0])
            ss=row[1].split('_Rev')
	    if len(ss)==2:
                nsvnutils.get_repo_name(ss[0])
                comment=nsvnutils.get_svn_log(ss[1])
		commiter=nsvnutils.get_svn_commiter(ss[1])
                subsql="update reviews_reviewrequest r set r.description='"+commiter+"_"+comment+"' where r.id="+rid
	        #print(subsql)
                nlog('/mnt/rb/sql.log',subsql)
		subcursor.execute(subsql)
            else:
                #print('skiping id='+rid+' summary='+row[1])
                nlog('/mnt/rb/sql.log','skiping id='+rid+' summary='+row[1])
        subconnection.commit()
    except Exception as ex:
        #print(str(ex))
        nlog('/mnt/rb/sql.log',str(ex))
	return
    finally:
        cursor.close()
	subcursor.close()

    connection.close()
    subconnection.close()

#start here
nlog("/mnt/rb/sql.log", 'Starting job on '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
update_rb_comment()
nlog("/mnt/rb/sql.log", 'Ending job on '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

3、建立定时任务,执行脚本

crontab -l

#10分钟运行一次
*/10 * * * * /home/neohope/scripts/updatedb.py

Comments are closed.