Ai
23 Star 56 Fork 90

openEuler/hpcrunner
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
executeService.py 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
lixiang 提交于 2024-10-10 10:28 +08:00 . 提交修改
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import logging
from asyncio.log import logger
from datetime import datetime
from toolService import ToolService
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"
logging.basicConfig(filename='runner.log', level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
class ExecuteService:
def __init__(self):
self.cur_time = ''
self.end_time = ''
self.tool = ToolService()
self.flags = '*' * 80
self.end_flag = 'END: '
# tools function
def join_cmd(self, arrs):
return " && ".join(arrs)
def print_cmd(self, cmd):
print(self.flags)
self.cur_time = self.tool.get_time_stamp()
print(f"RUNNING at {self.cur_time}:\n{cmd}")
logging.info(cmd)
print(self.flags)
# Execute, get output and don't know whether success or not
def exec_popen(self, cmd, isPrint=True):
if isPrint:
self.print_cmd(cmd)
output = os.popen(f"bash -c '{cmd}'").readlines()
return output
def get_duration(self):
time_1_struct = datetime.strptime(self.cur_time, "%Y-%m-%d %H:%M:%S")
time_2_struct = datetime.strptime(self.end_time, "%Y-%m-%d %H:%M:%S")
seconds = (time_2_struct - time_1_struct).seconds
return seconds
# Execute, get whether success or not
def exec_list(self, cmds):
cmd = self.join_cmd(cmds)
if not cmd.startswith('echo'):
self.print_cmd(cmd)
state = os.system(f"bash -c '{cmd}'")
self.end_time = self.tool.get_time_stamp()
print(f"total time used: {self.get_duration()}s")
logger.info(self.end_flag + cmd)
if state:
print(f"failed at {self.end_time}:{state}".upper())
return False
else:
print(f"successfully executed at {self.end_time}, congratulations!!!".upper())
return True
def exec_raw(self, rows):
return self.exec_list(self.tool.gen_list(rows))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/openeuler/hpcrunner.git
git@gitee.com:openeuler/hpcrunner.git
openeuler
hpcrunner
hpcrunner
master

搜索帮助