def cutAllFiles(srcfile, dstfile, key=None, isreplace=False): if key is None: files = tool.getFiles(srcfile) else: files = tool.getFiles(srcfile, key=key) if isreplace: for _file in files: if isFile(_file): _, _, _, name = tool.splitPath(_file) if isExist(_file): delFile(tool.pathJoin(dstfile, name)) tool.cutFile(_file, dstfile) else: _, _, _, name = tool.splitPath(_file) if isExist(_file): delDir(tool.pathJoin(dstfile, name)) shutil.move(_file, dstfile + f'/{name}') print(f'copy {_file} -> dstfile/{name}') else: for _file in files: if isFile(_file): tool.cutFile(_file, dstfile) else: _, _, _, name = tool.splitPath(_file) shutil.move(_file, dstfile + f'/{name}') print(f'copy {_file} -> dstfile/{name}')
def putFile(self, file_path, remote_path, iscut=False): logger = None if self.log: logger = log.init_log(self.log_path) scp = SCPClient(self.ssh.get_transport()) if not os.path.exists(file_path): print(f'{file_path} 文件不存在') if self.log: logger.info(f'{file_path} 文件不存在') _, _, _, file_name = tool.splitPath(file_path) _, _, _, remote_name = tool.splitPath(remote_path) # 可以只是文件夹路径 if remote_name != file_name: remote_path = tool.pathJoin(remote_path, file_name) scp.put(file_path, remote_path) scp.close() if iscut: os.remove(file_path) print(f'{file_path} -----> {self.ip_name}{remote_path}') if self.log: logger.info(f'{file_path} -----> {self.ip_name}{remote_path}')
def alter_log_ini(log_path, n_log=20): """ 替换文件中的字符串 :param file: 文件名 :param old_str: 就字符串 :param new_str: 新字符串 :return: """ ini = """[loggers]\nkeys=root\n[handlers]\nkeys=hand01\n[formatters]\nkeys=form01\n[logger_root]\nlevel=NOTSET\nhandlers=hand01\n[handler_hand01]\nclass=handlers.ConcurrentRotatingFileHandler\nlevel=NOTSET\nformatter=form01\nargs=\n[formatter_form01]\nformat=%(asctime)s %(levelname)s %(message)s\n""" log_ini_path = tool.pathJoin(log_path, 'logging.ini') now_date = str(time.strftime("%Y-%m-%d-%H-%I-%M", time.localtime(time.time()))) log_path = log_path if log_path[-1] == '/': log_path = log_path[:-1] if os.path.exists(log_path) == False: os.makedirs(log_path) with open(log_ini_path, 'w', encoding="utf-8") as f: f.write(ini) old_str = 'args=' new_str = '''args=("{}/{}.log", "a", 1024*2048, {})\n'''.format(log_path, now_date, n_log) file_data = "" with open(log_ini_path, "r", encoding="utf-8") as f: for line in f: if old_str in line: line = new_str file_data += line else: file_data += line with open(log_ini_path, "w", encoding="utf-8") as f: f.write(file_data)
def getFile(self, file_path, remote_path): logger = None if self.log: logger = log.init_log(self.log_path) scp = SCPClient(self.ssh.get_transport()) _, _, _, file_name = tool.splitPath(file_path) _, _, _, remote_name = tool.splitPath(remote_path) # 可以只是文件夹路径 if remote_name != file_name: remote_path = tool.pathJoin(remote_path, file_name) scp.get(file_path, remote_path) scp.close() print(f'{self.ip_name}{file_path} -----> {remote_path}') if self.log: logger.info(f'{self.ip_name}{file_path} -----> {remote_path}')
def init_log(log_path): log_ini_path = tool.pathJoin(log_path, 'logging.ini') logging.config.fileConfig(log_ini_path) log = logging.getLogger() return log