def preStart(tmpDir): try: if os.path.exists(tmpDir): shutil.rmtree(tmpDir) os.makedirs(tmpDir) except Exception as e: logger.error("临时文件创建失败:{}".format(e)) return False return True
def read(self, filePath): try: fd = open(filePath, 'rb') except Exception as ex: print(ex) msg = traceback.format_exc() # print(msg) logger.error("读取文件失败:[{}][{}] ".format(filePath, ex)) sys.exit(1) self.str = fd.read() # print "conf_content:", self.str return True
def preStart(fun): """ 创建临时目录(函数运行结束,自动删除临时目录) Args: fun: 目录路径 Returns:boolean """ try: with tempfile.TemporaryDirectory() as tmpdir: fun(tmpdir) except Exception as e: logger.error("临时文件创建失败:{}".format(e)) return False return True
def select(self, sqlstr): """ 查询数据 Args: sqlstr: Returns: """ try: self.cursor.execute(sqlstr) except Exception as e: logger.error("error: 查询操作出错") logger.error("error:[{}]".format(e)) return "" return self.cursor.fetchall()
def insert(self, sqlStr): """ 插入数据 Args: sqlStr: sql语句 Returns:boolean """ try: self.cursor.execute(sqlStr) self.dbhandle.commit() except Exception as e: logger.error("error: 插入操作失败:[{}]".format(sqlStr)) logger.error("error reason:{}".format(e)) self.dbhandle.rollback() return False return True
def checkDir(targetPath): """ 判断目录是否存在,不存在则创建 Args: targetPath: 路径 Returns:成功:0 失败:-1 """ Flag = 0 if os.path.isdir(targetPath): pass else: try: os.makedirs(targetPath) except Exception as e: logger.error("文件夹创建失败{}".format(targetPath)) logger.error("error:{}".format(e)) Flag = -1 Flag = 1 return Flag
def createDataBase(configPath, dbname=""): """ 创建数据库 Args: configPath: 配置文件位置 dbname: 数据库名 Returns:结果 """ try: odb = OperateDB(configPath, isUsedDB=False) if len(dbname) == 0: dbname = odb.dbconf.db val = odb.insert("create database IF NOT EXISTS " + dbname + " default charset utf8 collate utf8_general_ci;") except Exception as e: val = False logger.error("数据库创建失败:[{}]".format(dbname)) logger.error("error:{}".format(e)) print("创建数据库:", dbname) return val
def parseInputParameter(filePath): rc = ReadConfigure() if not rc.read(filePath): logger.error("error 文件不可访问:[{}]".format(filePath)) sys.exit(1) return json.loads(rc.getReadData())