import multiprocessing
from lib import expand, cal_metrics
from datetime import datetime
import os, shutil, json, csv, psutil, sys
from multiprocessing import Process
M = 1024*1024
def addtimeline(weightmethod, weight_input):
current_datetime = datetime.now()
timestamp = current_datetime.strftime("%Y%m%d%H%M%S%f")
new_folder_path = os.path.join('result/history/', timestamp)
os.makedirs(new_folder_path)
os.makedirs(new_folder_path+'/single_res')
os.makedirs(new_folder_path+'/inputs')
# 要复制的文件列表
file_list = ['graph_stat.json', 'weight.json', 'path_metrics.csv', 'rank.json']
for file_name in file_list:
source_file = os.path.join('result/', file_name)
shutil.copy2(source_file, new_folder_path+'/single_res')
shutil.copytree(os.path.join('result/', 'tmp'), os.path.join(new_folder_path, 'tmp'))
file_list = ['input.json', 'MainInput.json', weightmethod+'_input/'+weight_input]
for file_name in file_list:
source_file = os.path.join('input/', file_name)
shutil.copy2(source_file, new_folder_path+'/inputs')
return current_datetime
def save(addnum, linknum, multi):
with open('input/MainInput.json', 'r') as f:
input = json.loads(f.read())
f.close()
weightmethod = input['weight']; rankmethod = input['rank']
weight_input = input['weight_input']
jounal1_path = 'result/stat_all.csv'; jounal2_path = 'result/perform_log.csv'
# 添加时间戳并备份
timestamp = addtimeline(weightmethod, weight_input)
# stat_all.csv
jounal1 = []
with open("result/graph_stat.json", 'r') as f:
stat = json.loads(f.read())
f.close()
hostsnum=stat["hostsnum"]; edgesnum=stat["edgesnum"]; pathsnum=stat["pathsnum"]
with open("result/tmp/time.log", 'r') as f:
log = f.read()
f.close()
ls1 = log.split()
err = []; ls = []; j=0; flag=0
for i in range(len(ls1)):
if flag==1:
flag-=1
continue
try:
ls.append(float(ls1[i]))
j+=1
except:
if j+1 == i or j+2 == i:
ls.append('None')
err.append(ls1[i])
if ls1[i] == 'signal' or ls1[i] == 'code':
err.append(ls1[i+1])
flag += 1
exception = ' '.join(err)
print(ls)
with open('result/tmp/exception.txt', 'w') as f:
f.write(exception)
expandtime=ls[0]; expandmem=ls[1]
weighttime=ls[2]; weightmem=ls[3]
metricstime=ls[4]; metricsmem=ls[5]
ranktime=ls[6]; rankmem=ls[7]
jounal1.append([timestamp, weightmethod, rankmethod, hostsnum, edgesnum, pathsnum, expandtime, expandmem, weighttime, weightmem, metricstime, metricsmem, ranktime, rankmem, exception])
with open(jounal1_path, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(jounal1[0])
# perform_log.csv
jounal2 = []
jounal2.append([timestamp, weightmethod, rankmethod, addnum, linknum, multi])
with open(jounal2_path, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(jounal2[0])
def save_risk(addnum, linknum, multi):
with open('input/MainInput.json', 'r') as f:
input = json.loads(f.read())
f.close()
weightmethod = input['weight']; rankmethod = input['rank']
weight_input = input['weight_input']
method = input["rrisk_cal"]["method"]
if method=='s':
riskmethod='static'
elif method=='d':
riskmethod='dynamic'
jounal1_path = 'result/stat_all.csv'; jounal2_path = 'result/perform_log.csv'
# 添加时间戳并备份
timestamp = addtimeline(weightmethod, weight_input)
# stat_all.csv
jounal1 = []
with open("result/graph_stat.json", 'r') as f:
stat = json.loads(f.read())
f.close()
hostsnum=stat["hostsnum"]; edgesnum=stat["edgesnum"]; pathsnum=stat["pathsnum"]
with open("result/tmp/time.log", 'r') as f:
log = f.read()
f.close()
ls1 = log.split()
err = []; ls = []; j=0; flag=0
for i in range(len(ls1)):
if flag==1:
flag-=1
continue
try:
ls.append(float(ls1[i]))
j+=1
except:
if j+1 == i or j+2 == i:
ls.append('None')
err.append(ls1[i])
if ls1[i] == 'signal' or ls1[i] == 'code':
err.append(ls1[i+1])
flag += 1
with open("result/tmp/risk_time.log", 'r') as f:
log = f.read()
f.close()
ls2 = log.split()
err = []; ls_risk = []; j=0; flag=0
for i in range(len(ls2)):
if flag==1:
flag-=1
continue
try:
ls_risk.append(float(ls2[i]))
j+=1
except:
if j+1 == i or j+2 == i:
ls_risk.append('None')
err.append(ls2[i])
if ls2[i] == 'signal' or ls2[i] == 'code':
err.append(ls1[i+1])
flag += 1
for lr in ls_risk:
ls.append(lr)
print(lr)
print(ls)
exception = ' '.join(err)
with open('result/tmp/exception.txt', 'w') as f:
f.write(exception)
expandtime=ls[0]; expandmem=ls[1]
weighttime=ls[2]; weightmem=ls[3]
metricstime=ls[4]; metricsmem=ls[5]
ranktime=ls[6]; rankmem=ls[7]
risktime=ls[8]; riskmem=ls[9]
jounal1.append([timestamp, weightmethod, rankmethod,riskmethod, hostsnum, edgesnum, pathsnum, expandtime, expandmem, weighttime, weightmem, metricstime, metricsmem, ranktime, rankmem,risktime,riskmem, exception])
with open(jounal1_path, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(jounal1[0])
# perform_log.csv
jounal2 = []
jounal2.append([timestamp, weightmethod, rankmethod,riskmethod, addnum, linknum, multi])
with open(jounal2_path, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(jounal2[0])
解析一下函数用途与代码逻辑,不做修改优化,仅仅解释,包括传输入参数以及用途,流程,详细清晰
最新发布