#!/usr/bin/python
#-*- coding: utf-8 -*-
import sys
import os
import re
import chardet
import codecs
import MySQLdb
path="/Users/zhangyupeng/SVN/10_UAT_DEPLOY/SCFS/"
#connect the database
db = MySQLdb.connect(host="localhost" , user="root" , passwd="XXXX" , db="test_python" , charset="utf8")
cursor=db.cursor()
for i in os.walk(path):
file= str(i[2]).strip().split(',')
for j in file:
if j.lower().find('.sql')>0:
#take the SVN file path
#kill the symbol
j=j.strip().replace('[','').replace(']','').replace('\'','',10).replace('/','')
path=str(i[0])+'/'+j
#pass the udf & 10_TEMP & FXDS
if path.find('10_TEMP')>0:
continue;
if path.find('UDF')>0:
continue
if path.find('FXDS')>0:
continue;
#open the SVN file and take the info
with open(path,"rb") as f:
#open the file with code
data = f.read()
code=(chardet.detect(data))
code_p=str(code)[str(code)[:-2].rfind('\'')+1:].replace('\'','').replace('}','').replace(': ','')
mysql=codecs.open(path, 'rb',code_p)
#take the infomation
for line in mysql:
p1=line.lower().strip().encode('utf-8')
a3=''
a4=''
p=str(p1)
#kill the comment
if str(p)[0:2]=='--':
continue;
#search the database & table
if str(p).find('create')>=0 and str(p).find('table')>=0:
#take the database
aa1=str(p)[0:str(p).find('.')]
a1=aa1[aa1.rfind(' ')+1:]
#take the table
a2=str(p)[str(p).rfind('.')+1:].replace('(','')
#search the field & comment
if str(p).find('comment')>0:
#take the field
a3=str(p)[:str(p).find(' ')]
#take the comment
a4=str(p)[str(p).rfind('comment',10)+8:].replace(' ','',20).replace('\'','',10).replace(',','',20)
#kill the null
if a3=='' or a3==')':
continue;
#print a1+','+a2+','+a3+','+a4
#print 'database:',a1,'|','table:',a2,'|','field:',a3,'|','comment:',a4
cursor.execute('insert into svn_field_comment_test values("%s", "%s", "%s", "%s")' % \
(a1,a2,a3,a4))
db.commit()
db.close