文件替换
#!/usr/bin/python #Filename :smelter_repalce.py # -*- coding:utf-8 -*- import cx_Oracle import sys import getopt,string import re import os import datetime import os.path import configuration from Common import * class replace(object): def __init__(self): pass def conn_db(self): try: job_config=configuration.configuration() user_name=job_config.loadConfigFile('config_test.xml','user_name') user_password=job_config.loadConfigFile('config_test.xml','user_password') dns=job_config.loadConfigFile('config_test.xml','dns') db=cx_Oracle.connect(user_name,user_password,dns) return db except Exception as e: print (-1,e) def c_process_id(self,pid): db=self.conn_db() cursor=db.cursor() pro_pid=cursor.var(cx_Oracle.NUMBER) cursor.callproc("common.process_id",[pid,pro_pid]) return pro_pid.getvalue() db.close() def prhelp(self): print ''' Usage: smelter_colmerge.py [options] Options: -h, --help show this help message and exit -i, --interid input inter id -d, --dayid input file date -b, --before input before type code -n, --processid input process id -o, --origin input original string -r, --new input new string -m, --mode input mode,value f(full) or v(vague) Example: python smelter_replace.py -n 100008000 -i 25 -d 20120531 -b FILE_COLLECT -o women -r nimen -m f ''' #Parameters processing def fun_param_input(self): try: if len(sys.argv) > 14: opts, args = getopt.getopt(sys.argv[1:],"i:d:b:n:o:r:m:h", ["interid=","dayid=","before=","processid=","origin=","new=","mode=","help"]) #print(sys.argv) #print(opts) #print(args) for a,o in opts: if a in ('-i', '--interid'): INTERID=o #print INTERID elif a in ('-n','--processid'): PROCESSID=o #print PROCESSID elif a in ('-d','--dayid'): #print o DAYID=o elif a in ('-b','--before'): TYPCD=o #print o elif a in ('-o','--origin'): orig_str=o #print o elif a in ('-r','--new'): new_str=o #print o elif a in ('-m','--mode'): mode=o #print o elif a in ('-h', '--help'): self.prhelp() sys.exit() if len(DAYID)==8: date_ymd=DAYID date_h='000000' elif len(DAYID)==14: date_ymd=DAYID[0:8] date_h=DAYID[8:14] else: print 'Please input right day' self.prhelp() sys.exit() if mode =='f' or mode =='v': #print INTERID,PROCESSID,date_ymd,date_h,TYPCD,orig_str,new_str,mode return INTERID,PROCESSID,date_ymd,date_h,TYPCD,orig_str,new_str,mode else: print 'Please input right mode value' self.prhelp() sys.exit() else: print 'Please input right parameters' self.prhelp() sys.exit() except Exception as e: return (-1,e) def find_file(self): INTERID,PROCESSID,date_ymd,date_h,tp_code,orig_str,new_str,mode=self.fun_param_input() #print INTERID,PROCESSID,date_ymd,date_h,tp_code,orig_str,new_str,mode db=self.conn_db() cursor=db.cursor() dm_t=myexec_type() tp_id=dm_t.code_to_id('FILE_REPLACE') bf_id =dm_t.code_to_id(tp_code) process_id=self.c_process_id(PROCESSID) #print "111111111111111111111111111111111111111111" #print tp_id,bf_id,process_id cursor.execute('select max(process_id) from log_file where table_id=:tid and data_date=:dt_dt and type_id=:tpp_id and status=\'S\'',tid=INTERID,dt_dt=date_ymd,tpp_id=bf_id) c=cursor.fetchall() pcsid=c[0][0] #print pcsid cursor.execute('select if_file_name,file_type,file_id,current_directory from log_file where table_id=:tid and data_date=:dt_dt and process_id=:ppid and type_id=:tpp_id and status=\'S\'',tid=INTERID,dt_dt=date_ymd,ppid=pcsid,tpp_id=bf_id) a=cursor.fetchall() source_file=[] i=0 if len(a)>0: while i<len(a): source_file.append(a[i]) cursor.callproc("common.Log_file_initial",[process_id,INTERID,source_file[i][2],tp_id,source_file[i][1]]) cursor.callproc("common.Log_file_start",[process_id,INTERID,source_file[i][2],tp_id,source_file[i][1],date_ymd,date_h,source_file[i][0],source_file[i][3]]) i+=1 #print i,date_ymd,date_h,tp_id,process_id,mode,INTERID return i,source_file,date_ymd,date_h,tp_id,process_id,orig_str,new_str,mode,INTERID else: return 0,0,0,0,0,0,0 db.close def fun_replace(self): try: db=self.conn_db() cursor=db.cursor() i,source_file,date_ymd,date_h,tp_id,process_id,orig_str,new_str,mode,INTERID=self.find_file() if i==0: meg='no file' return (-1,meg) else: mg=[] j=0 if mode=='v': orig_str=re.escape(orig_str) #print orig_str v_orig = re.compile(orig_str) while j < i: s_name = os.path.join(source_file[j][3],source_file[j][0]) t_name = os.path.join(source_file[j][3],source_file[j][0]) t_name=t_name+"_replace" if os.path.exists(s_name): if os.path.exists(t_name): os.remove(t_name) s_f=open(s_name,'rb') t_f=open(t_name,'wb') for line in s_f.readlines(): n = v_orig.sub(new_str,line) t_f.write(n) cursor.callproc("common.Log_file_success",[process_id,INTERID,source_file[j][2],tp_id,source_file[j][1],0]) s_f.close() t_f.close() else: s_f=open(s_name,'rb') t_f=open(t_name,'wb') for line in s_f.readlines(): n = v_orig.sub(new_str,line) t_f.write(n) cursor.callproc("common.Log_file_success",[process_id,INTERID,source_file[j][2],tp_id,source_file[j][1],0]) s_f.close() t_f.close() else: meg='%s not exists'%source_file[j][0] mg.append(meg) cursor.callproc("common.Log_file_fail",[process_id,INTERID,source_file[j][2],tp_id,source_file[j][1],meg,0,0]) j+=1 else: mg=[] orig_str=re.escape(orig_str) b=orig_str+'{1}' c=b+'\W' #print "22222222222" #print c f_orig = re.compile(c) while j < i: s_name = os.path.join(source_file[j][3],source_file[j][0]) t_name = os.path.join(source_file[j][3],source_file[j][0]) t_name=t_name+"_replace" if os.path.exists(s_name): if os.path.exists(t_name): os.remove(t_name) s_f=open(s_name,'rb') t_f=open(t_name,'wb') for line in s_f.readlines(): n = f_orig.sub(new_str,line) t_f.write(n) cursor.callproc("common.Log_file_success",[process_id,INTERID,source_file[j][2],tp_id,source_file[j][1],0]) s_f.close() t_f.close() else: s_f=open(s_name,'rb') t_f=open(t_name,'wb') for line in s_f.readlines(): n= f_orig.sub(new_str,line) t_f.write(n) cursor.callproc("common.Log_file_success",[process_id,INTERID,source_file[j][2],tp_id,source_file[j][1],0]) s_f.close() t_f.close() else: meg='%s not exists'%source_file[j][0] mg.append(meg) cursor.callproc("common.Log_file_fail",[process_id,INTERID,source_file[j][2],tp_id,source_file[j][1],meg,0,0]) j+=1 if len(mg) > 1: print (-1,mg) else: print (0,i) db.close() except Exception as e: return (-1,e) if __name__ == '__main__': a=replace() a.fun_replace()