悲剧的python脚本
程序员其实有一种悲剧,就是自己写出来的程序还没上线,就被领导否定鸟。这个我最近一个刚刚被否定的脚本
#!/usr/bin/env python
# Filename : ark_drop_op.py
import sys
import string
import cx_Oracle
import logging
import os
import time
LOG_ROOT = "../log"
NOAH_CONN = "noahpf/noahpf@ora10g42"
PROC_PREFIX = "proc_drop_op"
class ark_drop_op:
'''drop result table for ark.'''
def __init__(self):
self.conn = None
self.cursor = None
self.odMap = {}
def ConnectDB(self):
try:
self.conn = cx_Oracle.connect(NOAH_CONN)
self.cursor = self.conn.cursor()
logging.info('Ok! Success connect to Noah.')
except cx_Oracle.DatabaseError ,e:
logging.error(str(e))
sys.exit()
def CloseDB(self):
self.cursor.close()
self.conn.close()
def GetSingleResult(self, sql_):
try:
self.cursor.execute(sql_)
return self.cursor.fetchone()
except cx_Oracle.DatabaseError,e :
logging.error('Error! Access Database Error' + str(e))
sys.exit()
def LoadOpOverDueTable(self):
try:
sql = "select servername, overtime from tb_op_overdue"
logging.debug(sql)
self.cursor.execute(sql)
ods = self.cursor.fetchall()
for od in ods:
self.odMap[od[0]] = od[1]
print self.odMap
except cx_Oracle.DatabaseError,e:
logging.error('Error!' + str(e))
sys.exit()
def GetOverDueOp(self):
t_formate = "%Y%m%d %H:%M%S"
i_oneday = 24*60*60
nowtime = time.strftime(t_formate)
starttime = int(time.mktime(time.strptime(nowtime, t_formate)))
cursorP = None
try:
sql = "select a.opid from tb_op a, tb_op_favorite b where status > 2 and assigntime < :assigntime and a.opid != b.opid and favorite > 0 and a.procname = :procname "
logging.debug(sql)
self.cursor.prepare(sql)
cursorP = self.conn.cursor()
for od in self.odMap:
assigntime = starttime - self.odMap[od]*i_oneday + 1
self.cursor.execute(None,{'assigntime':assigntime, 'procname':od})
OpProc = self.cursor.fetchall()
for ap in OpProc:
cursorP.callproc(PROC_PREFIX, [ap])
except cx_Oracle.DatabaseError,e:
logging.error('Error!' + str(e))
sys.exit()
finally:
if (cursorP != None):
cursorP.close()
def Run(self):
if os.path.exists(LOG_ROOT) != True:
os.makedirs(LOG_ROOT)
os.system('rm -f %s/onekeysearh_checked.log' % (LOG_ROOT))
logging.basicConfig(level = logging.DEBUG, format = "%(asctime)s %(levelname)5s - %(message)s")
logging.getLogger().addHandler(logging.FileHandler("%s/onekeysearch_checked.log" % (LOG_ROOT)))
logging.info('.....................start to check....................')
self.ConnectDB()
self.LoadOpOverDueTable()
self.GetOverDueOp()
self.CloseDB()
logging.info('.....................finish check......................')
task = ark_drop_op()
task.Run()

浙公网安备 33010602011771号