zoukankan      html  css  js  c++  java
  • 文件移动

    #!/usr/bin/python
    #Filename :smelter_move.py
    # -*- coding:utf-8 -*-
    
    
    import cx_Oracle
    import os
    import datetime
    import os.path
    import shutil
    import sys
    import getopt,string
    
    class Filemove_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_move.py [options]
            Options:     
            -h, --help            show this help message and exit
            -i, --interid         assign to interface id
            -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_move.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_MOVE\'")
            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_move(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.move(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()
                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_move=self.fun_move(INTERID,DAYID,PROCESS,SOURCEDIR,TARGETDIR)
                    print f_move
            except Exception as e:
                return 'input parameter error!',e
    
    if __name__ == '__main__':
        job_set=Filemove_module('','','')
        job_set.main()
    
  • 相关阅读:
    【Henu ACM Round#17 A】Simple Game
    【Henu ACM Round #12 E】Thief in a Shop
    【Henu ACM Round#16 D】Bear and Two Paths
    【Henu ACM Round#16 A】 Bear and Game
    P4824 [USACO15FEB]Censoring (Silver) 审查(银)
    P4001 [BJOI2006]狼抓兔子
    P2444 [POI2000]病毒
    P3966 [TJOI2013]单词
    P3796 【模板】AC自动机
    P4574 [CQOI2013]二进制A+B
  • 原文地址:https://www.cnblogs.com/yuchunju/p/2570376.html
Copyright © 2011-2022 走看看