文件删除
#!/usr/bin/python #Filename:smelter_delete.py # -*- coding:utf-8 -*- import cx_Oracle import os import datetime import os.path import shutil import sys import getopt,string class Filedelete_module(object): def __init__(self,user_name,user_password,dns): self.user_name=user_name self.user_password=user_password self.dns=dns def conn_db(self): db=cx_Oracle.connect(self.user_name,self.user_password,self.dns) return db def c_process_id(self,pid): db=cx_Oracle.connect(self.user_name,self.user_password,self.dns) 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_delete.py [options] Options: -h, --help show this help message and exit -i, --interid assign to interid -d, --dayid assign to file date -n, --process assign to process id -s, --sourcedir assign to source directory Example: smelter_delete.py -i 30 -d 20110502 -n 0 -s /tmp/test0604 ''' def find_file(self,table_id,data_date,process_id,current_directory): #find file db=self.conn_db() cursor=db.cursor() cursor.execute("select type_id from dm_type where type_code=\'FILE_DELETE\'") b=cursor.fetchall() #print b[0][0] tp_id = b[0][0] if len(data_date)==8: data_ymd=data_date data_h='000000' elif len(data_date)==14: data_ymd=data_date[0:8] data_h=data_date[8:14] cursor.execute('select t1.if_file_name,t1.file_type,t1.file_id from log_file t1 join dm_type t2 on t1.type_id=t2.type_id where t1.table_id=:tid and t1.data_date=:dt_dt and t1.current_directory=:crt and t2.type_code<>\'FILE_COPY\' and t2.type_code<>\'FILE_MOVE\' and t2.type_code<>\'FILE_DELETE\' and t1.status=\'S\'',tid=table_id,dt_dt=data_ymd,crt=current_directory ) 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,table_id,source_file[i][2],tp_id,source_file[i][1]]) cursor.callproc("common.Log_file_start",[process_id,table_id,source_file[i][2],tp_id,source_file[i][1],data_ymd,data_h,source_file[i][0],current_directory]) i+=1 return i,source_file,data_ymd,data_h,tp_id else: return i,0,0,0,0 db.close() def fun_delete(self,table_id,data_date,process_id,current_directory): #delete file try: db=self.conn_db() cursor=db.cursor() p_process_id=self.c_process_id(process_id) i,source_file,data_ymd,data_h,tp_id = self.find_file(table_id,data_date,p_process_id,current_directory) if i==0: meg='no file' return -1,meg else: j=0 while j < i: filename = os.path.join(current_directory,source_file[j][0]) #print filename if os.path.isfile(filename): os.remove(filename) cursor.callproc("common.Log_file_success",[p_process_id,table_id,source_file[j][2],tp_id,source_file[j][1],0]) else: meg='%s no exsit'%filename cursor.callproc("common.Log_file_fail",[p_process_id,table_id,source_file[j][2],tp_id,source_file[j][1],meg,0,0]) break j+=1 cursor.execute('select count(*) from log_file t1,dm_type t2 where t1.table_id=:tid and t1.data_date=:dt_dt and t1.current_directory=:crt and t1.process_id=:p_process_id and t1.status=\'F\' and t1.type_id=t2.type_id',tid=table_id,dt_dt=data_ymd,crt=current_directory,p_process_id=p_process_id) a=cursor.fetchall() if a[0][0]==0: return 0,j else: return -1,meg db.close() except Exception as e: return -1,e def main(self): #main INTERID=0 DAYID=0 PROCESS=5 SOURCEDIR=0 TARGETDIR=0 if len(sys.argv) > 1: try: opts, args = getopt.getopt(sys.argv[1:], "i:d:n:s:h", ["interid=","dayid=","process=","sourcedir=","help"]) for a,o in opts: if a in ('-i', '--interid'): INTERID=o #print o elif a in ('-d','--dayid'): #print o DAYID=o elif a in ('-n','--process'): PROCESS=o #print o elif a in ('-s', '--sourcedir'): SOURCEDIR=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() #print INTERID,DAYID,PROCESS,SOURCEDIR f_delete=self.fun_delete(INTERID,DAYID,PROCESS,SOURCEDIR) print f_delete except Exception as e: return 'input parameter error!',e if __name__ == '__main__': job_set=Filedelete_module('','','') job_set.main() #smelter_delete.py -i 30 -d 20110502 -w 0 -s /tmp/test0604