python处理sql文件_使用Python将sql文件刷入DB

Python学习第二弹#coding = UTF-8

import os, sys, time, shutil

class NdbFlush:

def __init__(self):

self._ROOT_PATH_ = None

self._TNS_LIST_ = {}

self._FILE_LIST_ = {} # {SCHEMA:{'TAB':[], 'SEQ':[], 'PKGH':[]}, SCHEMA:{...}, ...}

self._ORA_CFG_FILE_ = None

def _UnInit_(self):

self._ROOT_PATH_ = None

self._TNS_LIST_ = None

self._FILE_LIST_ = None

self._ORA_CFG_FILE_ = None

#######################################

# 设置路径

#######################################

def GetFilePath(self):

expath = os.getcwd()

if os.path.isdir(expath):

return expath

elif os.path.isfile(expath):

return os.path.dirname(expath)

#######################################

# 获取连接串列表

#######################################

def GetOraInfo(self):

ora_file = self._ROOT_PATH_ + '/' + self._ORA_CFG_FILE_

tns_list = {}

fh = open(ora_file, 'r')

for ora in fh.readlines():

ora = ora.replace('\n', '')

if ora and len(ora) > 5:

ora_list = []

schema = ora.split('/')[0].upper()

ora_list = tns_list.get(schema)

if (ora_list and len(ora_list) > 0):

ora_list.append(ora)

else:

ora_list = [ora]

tns_list[schema] = ora_list

return tns_list

#######################################

# 将SQL文件名称加载到列表中

#######################################

def LoadNdbList(self, _list_):

#print('[LoadNdbList] _list_ =', _list_, '\tlen(_list_)', len(_list_))

lst = {}

try:

for itm in _list_:

#print('\tITM =', itm)

file_split = itm.split('_')

#print('\tfile_split[0] =', file_split[0], '\tfile_split[1] =', file_split[1])

#schema = {}

files = []

schema = lst.get(file_split[1].upper())

#print('\tschema =', schema)

if schema and len(schema) > 0:

files = schema.get(file_split[0])

#print('\t\tfiles =', files)

if files and len(files) > 0:

files.append(itm)

schema[file_split[0]] = files

#print('\t\t\tfiles 1 =', files)

#print('\t\t\tschema =', schema)

else:

files = [itm]

schema[file_split[0]] = files

#print('\t\t\tfiles 2 =', files)

else:

#print('\t', schema, file_split[0], itm)

schema = {}

files =[itm]

schema[file_split[0]] = files

#print('\tschema =', schema)

lst[file_split[1].upper()] = schema

#print('\tLST =', lst)

#print('-' * 80)

except Exception as e:

#print('请传入数组类参数(如:元组[])\n')

print(e)

lst = {}

#print(lst)

return lst

#######################################

# 获取SQL文件列表

#######################################

def GetSqlFileList(self):

filelist = []

sqlPath = self._ROOT_PATH_ + '/files/'

for file in os.listdir(sqlPath):

if file[-4:].upper() == '.SQL':

if filelist:

filelist.append(file)

else:

filelist = [file]

#print('\n\n', filelist, '\n\n')

print('在目录[' + sqlPath + ']下找到[%d]个SQL文件' % len(filelist))

return filelist

#######################################

# 将SQL刷入DB

#######################################

def SqlFlushDB(self, _runMode_ = 'M'):

# schema loop

file_list = self._FILE_LIST_

ora_list = self._TNS_LIST_

filePath = self._ROOT_PATH_ + '/files/'

for im in file_list:

#print('schema =', im)#, lst.get(im))

# file type loop begin

# SEQ

for xm in file_list.get(im):

#print('\ttype =', xm, '\n\tlist =', file_list.get(im).get(xm))

if 'SEQ' == xm.upper():

self.InitBat(im, file_list, ora_list, filePath, xm, _runMode_)

# TAB

for xm in file_list.get(im):

#print('\ttype =', xm, '\n\tlist =', file_list.get(im).get(xm))

if 'TAB' == xm.upper():

self.InitBat(im, file_list, ora_list, filePath, xm, _runMode_)

# TAB

for xm in file_list.get(im):

#print('\ttype =', xm, '\n\tlist =', file_list.get(im).get(xm))

if 'PKGH' == xm.upper():

self.InitBat(im, file_list, ora_list, filePath, xm, _runMode_)

# not in (TAB, SEQ)

for xm in file_list.get(im):

#print('\ttype =', xm, '\n\tlist =', file_list.get(im).get(xm))

if 'TAB' != xm.upper() and 'SEQ' != xm.upper() and 'PKGH' != xm.upper():

self.InitBat(im, file_list, ora_list, filePath, xm, _runMode_)

# file type loop end

def InitBat(self, _schema_, _fileList_, _oraList_, _filePath_, _fileType_, _runMode_ = 'M'):

# file name loop

for file in _fileList_.get(_schema_).get(_fileType_):

#print('\t\t', file)

filePath = _filePath_

sqlpath = filePath + file

fh = open(sqlpath, 'a+')

fh.write('\nexit')

fh.close()

tnslst = ''

# ora conf loop

fht = open(sqlpath + '.bat', 'a+')

fht.write('title [' + file + ']\necho off\n')

fht.write('cd ' + filePath + '\n')

fht.write('cls\n\n\n')

# tns loop

for tns in _oraList_.get(_schema_):

#print('\t\t\t', tns)

tnslst += tns + ', '

fht.write(('@echo "[ %s ]' %file) + (' -> [ %s]"' %tns) + '\n\n')

fht.write('@echo 刷库中...\n\n')

fht.write('sqlplus ' + tns + ' @' + file + ' >> ' + file + '.log\n\n\n')

fht.flush()

#fht.write('@pause\n')

fht.write('@echo FINISH>' + file + '.ok')

fht.write('\n\nexit')

fht.close()

print(('[ %s ]' %file) + (' -> [ %s]' %tnslst))

if _runMode_ == 'M':

self.RunBat(sqlpath, _runMode_)

else:

os.system(r'' + sqlpath + '.bat')

#time.sleep(1)

try:

fhl = open(r'' + sqlpath + '.log', 'r')

lines = fhl.readlines()

lineidx = 0

errFlag = False

fhl.close()

for line in lines:

lineU = line.upper()

if lineU.find('ERROR') >= 0:

errFlag = True

break

lineidx += 1

if errFlag:

print('\t>>[Status] Failed..')

print('\t  [ ' + lines[lineidx].replace('\n', '') + ' ]')

print('\t  [ ' + lines[lineidx + 1].replace('\n', '') + ' ]')

else:

print('\t>>[Status] Success..')

if os.path.isfile(r'' + sqlpath + '.log'):

os.remove(r'' + sqlpath + '.log')

shutil.move(sqlpath, sqlpath.replace('/files/', '/finish/'))

except Exception as e:

print('\t程序异常:', e)

print('-' * 70)

def RunBat(self, _fileName_, _runMode_):

state = 'START'

while True:

#print(runnext)

if state == 'START':

os.system('start ' + r'' + _fileName_ + '.bat')

state = 'RUNNING'

#print(1)

elif state == 'FINISH':

#print(9)

break

elif state == 'RUNNING':

time.sleep(1)

#print(2)

try:

fh = open(r'' + _fileName_ + '.ok', 'r')

state = fh.read().replace('\n', '')

except:

state = 'RUNNING'

else:

break

def CleanFile(self, _mode_ = 'Finish'):

tmpPath = self._ROOT_PATH_ + '/files/'

for file in os.listdir(tmpPath):

ffff = file.upper()

delFlag = False

if _mode_ == 'Finish':

if ffff[-4:] == '.BAT' or ffff[-7:] == '.SQL.OK':

delFlag = True

else:

if ffff[-4:] == '.LOG' or ffff[-4:] == '.BAT' or ffff[-7:] == '.SQL.OK':

delFlag = True

if delFlag:

tmpFile = os.path.join(tmpPath,  file)

if os.path.isfile(tmpFile):

os.remove(tmpFile)

def Launcher(self):

#l = ['tab_lpms_xxx.sql', 'tab_lpms_xx1x.sql', 'tab_lpms_xxxxx.sql', 'tab_wlt_xxx.sql', 'seq_lpms_xxx.sql', 'pkgh_jone_xxx.sql', 'pkgb_jone_xxx.sql', 'pubk_jone_xxx.sql']

#self._FILE_LIST_ = self.LoadNdbList(l)

# 清理历史bat文件

self._ROOT_PATH_ = self.GetFilePath().replace('\\', '/')

print('工作目录:', self._ROOT_PATH_)

_clean_ = True

_show_list_ = False

_dosMode_ = 'M'

#workp = 'D:/NdbFlush'

ipt = input('>>')

ipt = ipt.upper()

if ipt == 'V':

_show_list_ = True

elif ipt == 'D1':

_dosMode_ = 'S'

elif ipt == 'D2':

_dosMode_ = 'M'

elif ipt == 'Q':

exit()

elif ipt == 'C':

_clean_ = True

else:

print('将以默认方式执行...')

print('=' * 70)

print('\n\n')

self.CleanFile('Begin')

self._FILE_LIST_ = self.LoadNdbList(self.GetSqlFileList())

# 显示文件清单 BEGIN

if _show_list_:

lst = self._FILE_LIST_

for im in lst:

# schema

print('schema =', im)#, lst.get(im))

for xm in lst.get(im):

# file type

print('\ttype =', xm, '\n\tlist =', lst.get(im).get(xm))

# 显示文件清单 END

self._ORA_CFG_FILE_ = 'ora_tns_info.conf'

self._TNS_LIST_ = self.GetOraInfo()

self.SqlFlushDB(_dosMode_)

if _clean_:

self.CleanFile()

self._UnInit_()

return self._FILE_LIST_, self._TNS_LIST_

def usage():

print('=' * 70)

print('=\t[ NDB 刷库工具 ] v1.0')

print('=\t2015-07-05 by L')

print('-' * 60)

print('=\t[ Q: 退出; ]')

print('=\t[ V: 显示录库的SQL列表; ]')

print('=\t[ D1: 单窗口执行; D2:多窗口执行[默认]; ]')

print('=\t[ \t执行前请确保: ')

print('=\t \t\t1)SQL:[$WorkPath$/files/]')

print('=\t \t\t2)TNS:[$WorkPath$/ora_tns_info.conf]')

print('=\t[ 回车继续; ]')

print('=' * 70)

if __name__ == '__main__':

usage()

ndb = NdbFlush()

lst, ora = ndb.Launcher()

print('\n\n')

print('=' * 70)

print('\n')

print('刷库动作完毕,执行结果详见上面日志,失败信息已写入对应log文件$WorkPath$/file/*.log...\n[Enter]')

input('')

'''

print('[MAIN] lst =', lst)

print('[MAIN] ora =', ora)

print('\n\n')

print('\n\n\n\n')

print('-' * 100)

print('\n\n\n\n')

for im in lst:

# schema

print('schema =', im)#, lst.get(im))

for xm in lst.get(im):

# file type

print('\ttype =', xm, '\n\tlist =', lst.get(im).get(xm))

'''

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值