-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
97 lines (74 loc) · 2.98 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import logging
import argparse
import json
import os
import socket
import sys
import re
from datetime import datetime
from colorlog import ColoredFormatter
class ColorFilter(logging.Filter):
def filter(self, record):
# Define a regex pattern to match ANSI color escape sequences
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
# Remove ANSI color escape sequences from record.msg
record.msg = ansi_escape.sub('', record.msg)
return True
class SuffixFilter(logging.Filter):
def filter(self, record):
if '.' in record.name:
record.name_last = record.name.rsplit('.', 1)[-1]
else:
record.name_last = record.name
return True
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", help="path to config file")
parser.add_argument("-l", "--log", help="path to log file", default="./logs")
args = parser.parse_args()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = ColoredFormatter(
"%(log_color)s%(asctime)s %(bold)s%(levelname)s%(reset)s %(log_color)s%(thin)s%(name_last)-16s %(reset)s %(message)s",
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
},
secondary_log_colors={},
style='%'
)
os.makedirs(args.log, exist_ok=True)
log_filename = os.path.join(args.log, f'{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.log')
file_handler = logging.FileHandler(log_filename)
file_formatter = logging.Formatter(
'%(asctime)s %(levelname)s %(name_last)-16s %(message)s', datefmt='%Y-%m-%d %H:%M:%S'
)
file_handler.setFormatter(file_formatter)
file_handler.addFilter(SuffixFilter())
file_handler.addFilter(ColorFilter())
logger.addHandler(file_handler)
logger.info(f'Loading config from {args.config}')
config = json.load(open(args.config, 'r'))
if 'run_name' not in config:
config['run_name'] = os.path.split(args.config)[-1].replace('.', '_')
if 'log_path' not in config:
config['log_path'] = log_filename
# log hostname, user, python environment variables, ... to log file
logger.info(f'Config: \n{json.dumps(config, indent=2, ensure_ascii=False)}')
logger.info(f'Hostname: {socket.gethostname()}')
logger.info(f'Python Version: {sys.version}')
logger.info(f'Python Environment Variables: {os.environ}')
logger.info(f'Command: {sys.argv}')
handler.setFormatter(formatter)
logger.addHandler(handler)
handler.addFilter(SuffixFilter())
logger.info(f'Running SimpleEvaluationPipeline, run_name: {config["run_name"]}')
from freeeval.core.pipelines import EvaluationPipeline
pipeline = EvaluationPipeline(config)
pipeline.run()