write_database

# --coding=utf-8 import traceback from vsoc.bin.common import getLogger from vsoc.utils.sql_utl import DataBase logger = getLogger('write_database.log') import os import ConfigParser # conf_path=os.path.join(os.path.dirname(os.path.dirname(__file__)), 'conf', 'write_data.conf') conf_path='/home/master/ISOP/apps/vsoc/conf/write_data.conf' def write_database(): dir_path =os.path.join('/home/master/ISOP/apps/vsoc/', 'bin', 'upload') pathDir = os.listdir(dir_path) conf = ConfigParser.ConfigParser() conf.read(conf_path) load = conf.items('loads')[0][1] print load if load != '2': print pathDir for dir in pathDir: print os.path.join(dir_path,dir) args="""psql -d nsc --user=master -c"COPY {table} FROM '{csv_path}' with csv header" && rm -rf {csv_path}""" if "vehicle_info" in dir: table="internal_app_vsoc.vehicle_info(vin_id, vehicle_type, protocol, bytes_all, tx_bytes, rx_bytes, tx_pkts, rx_pkts, session_all,start_time, end_time, time_stamp, dport) " else: table="internal_app_vsoc.server_info(dip,protocol, bytes_all, tx_bytes, rx_bytes, tx_pkts, rx_pkts, session_all,start_time, end_time, time_stamp, dport) " conf = ConfigParser.ConfigParser() conf.read(conf_path) conf.set('loads', 'load', '1') with open(conf_path, 'w') as fw: # 循环写入 conf.write(fw) print args.format(table=table,csv_path=os.path.join(dir_path,dir)) rtn = os.popen(args.format(table=table,csv_path=os.path.join(dir_path,dir))) cmd_rtn = rtn.readlines() # os.system(args.format(table=table,csv_path=os.path.join(dir_path,dir))) print cmd_rtn conf = ConfigParser.ConfigParser() conf.read(conf_path) conf.set('loads', 'load', '0') logger.info('%s import succeeded'%dir) else: print traceback.format_exc() logger.error("to be continued") if __name__ == '__main__': try: write_database() except Exception as e: print traceback.format_exc() logger.error(traceback.format_exc())

posted @ 2021-12-23 16:37  枫叶少年  阅读(39)  评论(0编辑  收藏  举报