Skip to content

Instantly share code, notes, and snippets.

@marioevz
Created September 25, 2023 22:33
Show Gist options
  • Save marioevz/0f71feae70aa7e25c9baa2b9c493c850 to your computer and use it in GitHub Desktop.
Save marioevz/0f71feae70aa7e25c9baa2b9c493c850 to your computer and use it in GitHub Desktop.
`hive` script to run hive and sort log outputs
#!/usr/bin/env python3.10
import json
import os
import re
import shutil
import sys
from signal import SIGINT
from subprocess import Popen
HIVE_ROOT = os.environ.get("HIVE_ROOT")
if not HIVE_ROOT:
print("HIVE_ROOT environment variable is not set.")
sys.exit(1)
LATEST_RESULTS_ROOT = os.path.join(HIVE_ROOT, "workspace/latest")
RESULTS_ROOT = os.path.join(HIVE_ROOT, "workspace/logs000")
if os.path.isdir(RESULTS_ROOT):
i = 0
RESULTS_ROOT = os.path.join(HIVE_ROOT, "workspace", "logs" + "{:03d}".format(i))
while os.path.isdir(RESULTS_ROOT):
i += 1
RESULTS_ROOT = os.path.join(HIVE_ROOT, "workspace", "logs" + "{:03d}".format(i))
command = [os.path.join(HIVE_ROOT, "hive")]
command.extend(["--results-root", RESULTS_ROOT])
command.extend(sys.argv[1:])
try:
p = Popen(command)
p.wait()
except KeyboardInterrupt:
p.send_signal(SIGINT)
p.wait()
if not os.path.isdir(RESULTS_ROOT):
print("No results directory")
sys.exit()
simulator_log_pattern = r"\d+-simulator-[0-9a-f]+\.log"
simulator_logs = [
filename for filename in os.listdir(RESULTS_ROOT) if re.match(simulator_log_pattern, filename)
]
if simulator_logs:
shutil.move(
os.path.join(RESULTS_ROOT, simulator_logs[0]), os.path.join(RESULTS_ROOT, "simulator.log")
)
json_file_pattern = r"\d+-[0-9a-f]+\.json"
json_file = [
filename for filename in os.listdir(RESULTS_ROOT) if re.match(json_file_pattern, filename)
]
if json_file:
json_file_path = os.path.join(RESULTS_ROOT, json_file[0])
with open(json_file_path) as f:
data = json.load(f)
test_cases = data.get("testCases", {})
for test_case_id, test_case in test_cases.items():
test_case_folder = os.path.join(RESULTS_ROOT, f"tc_{test_case_id}")
os.makedirs(test_case_folder)
clients = test_case.get("clientInfo", {})
client_indexes = {}
for client_data in sorted(list(clients.values()), key=lambda x: x['instantiatedAt']):
client_type = client_data.get("name", "")
client_log = client_data.get("logFile", "")
if client_type not in client_indexes:
client_indexes[client_type] = 1
else:
client_indexes[client_type] += 1
client_index = client_indexes[client_type]
log_file_name = f"{client_index}_{client_type}.log"
log_file_path = os.path.join(test_case_folder, log_file_name)
full_hash_log_path = os.path.join(RESULTS_ROOT, client_log)
shutil.copy(full_hash_log_path, log_file_path)
if os.path.isdir(LATEST_RESULTS_ROOT):
shutil.rmtree(LATEST_RESULTS_ROOT)
shutil.copytree(RESULTS_ROOT, LATEST_RESULTS_ROOT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment