• 文件复制


    #!/usr/bin/python
    #Filename :smelter_copy.py
    # -*- coding:utf-8 -*-
    
    import cx_Oracle
    import os
    import datetime
    import os.path
    import shutil
    import sys
    import getopt,string
    
    
    class Filecopy_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_copy.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
            -p, --targetdir       assign to target directory
    
            Example: smelter_copy.py -i 30 -d 20110502 -n 0 -s /tmp/test0604 -p /hadoop/pigtmp/sj0109/sj
            '''
    
        
        def find_file(self,table_id,data_date,process_id,current_directory):
            db=self.conn_db()
            cursor=db.cursor()
            cursor.execute("select type_id from dm_type where type_code=\'FILE_COPY\'")
            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()
            #print a
            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_copy(self,table_id,data_date,process_id,current_directory,target_directory):
            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])
                        targetname = os.path.join(target_directory,source_file[j][0])
                        #print filename
                        #print targetname
                        if os.path.exists(filename):
                            if not os.path.exists(targetname):
                                shutil.copy(filename,target_directory)
                                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 already exists'%targetname
                                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
                        else:
                            meg='%s not  exists'%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()
                #print a[0][0]
                if a[0][0]==0:
                    return 0,j 
                else:
                    return -1,meg
                db.close()
            except Exception as e:
                return -1,e
    
    
    
        def main(self):
            INTERID=0
            DAYID=0
            PROCESS=5
            SOURCEDIR=0
            TARGETDIR=0
            try:
                if len(sys.argv) > 1:
                    opts, args = getopt.getopt(sys.argv[1:], "i:d:n:s:p:h", ["interid=","dayid=","process=","sourcedir=","targetdir","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','--work'):
                            PROCESS=o
                            #print o
                        elif a in ('-s', '--sourcedir'):
                            SOURCEDIR=o
                            #print o 
                        elif a in ('-p', '--targetdir'):
                            TARGETDIR=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,TARGETDIR
                    f_copy=self.fun_copy(INTERID,DAYID,PROCESS,SOURCEDIR,TARGETDIR)
                    print f_copy
            except Exception as e:
                return 'input parameter error!',e
    
    
    
    if __name__ == '__main__':
        job_set=Filecopy_module('','','')
        job_set.main()
        #smelter_copy.py -i 30 -d 20110502 -w 0 -s /tmp/test0604 -p /hadoop/
    
  • 相关阅读:
    [BTS][收藏]如何通过BizTalk Server 2006 Exam (70235)
    [BTS]BizTalk调用WebService
    [WWF]我的第一个Workflow项目
    谈谈如何学习BizTalk
    [BTS06]BizTalk2006 SDK阅读笔记(五) 管道Pipeline
    [BTS06]BizTalk2006 SDK阅读笔记(三) 工作过程
    如何实现模块间通信[DNN 3.2.2]
    印度是如何做软件开发的
    实施ERP100问
    C#分析数据库结构,使用XSL模板自动生成代码
  • 原文地址:https://www.cnblogs.com/yuchunju/p/2570377.html
Copyright © 2020-2023  润新知