Skip to content

Commit

Permalink
Add outputfile option to inspect (mlcommons#340)
Browse files Browse the repository at this point in the history
New CLI argument (`--output-file`, `--output_file`) for the mlcube's `inspect` command can be used to specify the file to save inspect results.
  • Loading branch information
hasan7n authored Aug 18, 2023
1 parent 5bef920 commit 755d388
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions mlcube/mlcube/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This requires the MLCube 2.0 that's located somewhere in one of dev branches."""
import logging
import os
from pathlib import Path
import shutil
import sys
import typing as t
Expand Down Expand Up @@ -666,28 +667,49 @@ def create() -> None:
default="json",
help="Format for reporting results.",
)
@click.option(
"--output-file",
"--output_file",
required=False,
type=str,
default=None,
help="File path to store the MLCube information. Defaults to print to STDOUT",
)
@Options.help
def inspect(
mlcube: t.Optional[str], platform: str, force: bool = False, format_: str = "json"
mlcube: t.Optional[str],
platform: str,
force: bool = False,
format_: str = "json",
output_file: t.Optional[str] = None,
) -> None:
"""Return low-level information on MLCube objects."""
runner_cls, mlcube_config = parse_cli_args(
parsed_args={"mlcube": mlcube, "platform": platform},
unparsed_args=[],
resolve=True,
)
if output_file is None:
output_stream = sys.stdout
else:
dir_path = Path(output_file).resolve().parent
dir_path.mkdir(parents=True, exist_ok=True)
output_stream = open(output_file, "w")

try:
runner = runner_cls(mlcube_config, task=None)
info: t.Dict = runner.inspect(force=force)
logger.debug("inspect info=%s", info)
if format_ == "json":
import json

print(json.dumps(info))
json.dump(info, output_stream)
if output_stream == sys.stdout:
print() # json doesn't print a newline
else:
import yaml

yaml.dump(info, sys.stdout)
yaml.dump(info, output_stream)
except MLCubeError as err:
print("MLCube inspect failed")
logger.exception(err)
Expand Down

0 comments on commit 755d388

Please sign in to comment.