74 lines
1.7 KiB
Python
Raw Normal View History

2024-11-28 15:15:05 -03:00
import sys
from pathlib import Path
import typer
from pydantic_core import ValidationError
from benchmarks.core.config import ConfigParser
from benchmarks.deluge.config import DelugeExperimentConfig
parser = ConfigParser()
parser.register(DelugeExperimentConfig)
app = typer.Typer()
def _parse_config(config: Path):
if not config.exists():
print(f'Config file {config} does not exist.')
sys.exit(-1)
2024-11-28 15:52:38 -03:00
with config.open(encoding='utf-8') as infile:
2024-11-28 15:15:05 -03:00
try:
2024-11-28 15:52:38 -03:00
return parser.parse(infile)
2024-11-28 15:15:05 -03:00
except ValidationError as e:
print(f'There were errors parsing the config file.')
for error in e.errors():
print(f' - {error["loc"]}: {error["msg"]} {error["input"]}')
sys.exit(-1)
@app.command()
def list(ctx: typer.Context):
2024-11-28 15:15:05 -03:00
"""
Lists the experiments available in CONFIG.
"""
experiments = ctx.obj
print(f'Available experiments are:')
2024-11-28 15:52:38 -03:00
for experiment in experiments.keys():
2024-11-28 15:15:05 -03:00
print(f' - {experiment}')
@app.command()
def run(ctx: typer.Context, experiment: str):
2024-11-28 15:15:05 -03:00
"""
Runs the experiment with name EXPERIMENT.
"""
experiments = ctx.obj
2024-11-28 15:52:38 -03:00
if experiment not in experiments:
print(f'Experiment {experiment} not found.')
2024-11-28 15:15:05 -03:00
sys.exit(-1)
experiments[experiment].build().run()
def _init_logging():
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
2024-11-28 15:15:05 -03:00
@app.callback()
def main(ctx: typer.Context, config: Path):
if ctx.resilient_parsing:
return
ctx.obj = _parse_config(config)
_init_logging()
2024-12-01 23:12:49 -03:00
if __name__ == '__main__':
app()