1- import os .path
2- from cn_s3_api .api import CNS3Api
1+ import sys
2+ import os
3+ sys .path .insert (1 , os .getcwd ())
4+
35import argparse
4- import configparser
56import logging
6-
7-
8- __CONFIG__ = configparser .RawConfigParser ()
9- __SECTION__ = ''
10- def read_config (file ):
11- with open (file ) as f :
12- file_content = f .read ()
13- __CONFIG__ .read_string (file_content )
14- return __CONFIG__
15-
16- def config (section ,key ,default = '' ):
17- c = __CONFIG__ [section ]
18- return c .get (key ,default )
19-
20- def get_config ():
21- items = __CONFIG__ .items ('config' )
22- data = {}
23- for k , v in items :
24- data [k ] = v
25- return data
26-
27- def connect_s3 ():
28- s3_api = CNS3Api (dict (
29- aws_access_key_id = config (__SECTION__ , 'access_key' ),
30- aws_secret_access_key = config (__SECTION__ , 'secret_key' ),
31- endpoint_url = config (__SECTION__ , 'end_point' ),
32- region_name = config (__SECTION__ , 'region' ),
33- ))
34- return s3_api
35- def copy_to_s3 (file ):
36- s3_api = connect_s3 ()
37- filename = os .path .basename (file )
38- s3path = config (__SECTION__ ,'dir_dest' ) + "/" + filename
39- try :
40- s3_api .upload_file (config (__SECTION__ ,'bucket' ), file , s3path )
41- except Error :
42- print ("Erreur" )
43-
44- def delete_to_s3 (file ):
45- s3_api = connect_s3 ()
46- filename = os .path .basename (file )
47- s3path = config (__SECTION__ , 'dir_dest' ) + "/" + filename
48- try :
49- s3_api .remove (config (__SECTION__ ,'bucket' ),s3path )
50- except Error :
51- print ("Erreur" )
52-
53- def list_s3 (dir ):
54- s3_api = connect_s3 ()
55- return s3_api .list (config (__SECTION__ ,'bucket' ),dir )
56- def search_list (key ,s3dir_list ):
57- dirname = config (__SECTION__ , 'dir_dest' )
58- for i in s3dir_list :
7+ from utils import config
8+ from utils import s3_utils
9+ from utils import sftp_utils
10+ def search_list (key ,dir_list ):
11+ dirname = config .get_key (config .get_section (), 'dir_dest' )
12+ for i in sdir_list :
5913 if (i ['Key' ] == dirname + "/" + key ):
6014 return i
6115 return False
16+ def copy_file (file ):
17+ type = config .get_key (config .get_section (),'type' ,'config' )
18+ if type == 's3' :
19+ s3_utils .copy_to_s3 (file )
20+ elif type == 'sftp' :
21+ sftp_utils .copy_to_sftp (file )
22+
23+ def list_dest ():
24+ dest = config .get_key (config .get_section (), 'dir_dest' )
25+ type = config .get_key (config .get_section (), 'type' ,"config" )
26+ listdest = []
27+ if type == "s3" :
28+ listdest = s3_utils .list_s3 (dest )
29+ elif type == "sftp" :
30+ list = sftp_utils .list (dest )
31+ for f in list :
32+ listdest .append ({'Key' : f .filename ,'Size' : f .st_size })
33+ return listdest
6234def main ():
63- global __SECTION__
6435 parser = argparse .ArgumentParser ()
65- parser .add_argument ('--config' , help = 'configFile' ,default = 'config' )
66- parser .add_argument ('--section' , help = 'a section in the configfile' , default = 's3 ' )
36+ parser .add_argument ('--config' , help = 'configFile' ,default = './ config.conf ' )
37+ parser .add_argument ('--section' , help = 'a section in the configfile' , default = 'config ' )
6738 args = parser .parse_args ()
68- __SECTION__ = args .section
69- read_config (args .config )
39+ config . set_section ( args .section )
40+ config . read_config (args .config )
7041 format = '%(asctime)s %(message)s'
71- logging .basicConfig (filename = config (args .section ,'logfile' ), level = logging .INFO , format = format )
72- path = config (args .section , 'dir_source' )
42+ logging .basicConfig (filename = config . get_key (args .section ,'logfile' ), level = logging .INFO , format = format )
43+ path = config . get_key (args .section , 'dir_source' )
7344 #Load s3 list
74- listdest = list_s3 ( config ( __SECTION__ , 'dir_dest' ) + "/" )
45+ listdest = list_dest ( )
7546 #list source directory
7647 for f in os .listdir (path ):
7748 s = search_list (f ,listdest )
@@ -82,12 +53,12 @@ def main():
8253 print (f + " OK" )
8354 else :
8455 print (f + " KO BAD SIZE" + "s3-size=" + str (s ['Size' ]) + " /size=" + str (size ))
85- copy_to_s3 (path + "/" + f )
56+ copy_file (path + "/" + f )
8657 print (f + " COPIED" )
8758 else :
8859 print (f + " KO NOT FOUND" )
8960 #copy
90- copy_to_s3 (path + "/" + f )
61+ copy_file (path + "/" + f )
9162 print (f + " COPIED" )
9263
9364
0 commit comments