From 803a91c0723533f62148528a81f9d0411b57438e Mon Sep 17 00:00:00 2001 From: Dmitrii Agibov Date: Mon, 20 Feb 2023 15:42:33 +0000 Subject: MLIA-813 Change default output directory - Use directory mlia-output as output directory for MLIA - If parameter --output-dir provided then place directory mlia-output under specified path or otherwise create it in the current working directory Change-Id: I298088c4aa8dbe9f35dee69ecb9ff6e9ea3cac0a --- src/mlia/cli/main.py | 16 ++++++++++------ src/mlia/cli/options.py | 9 +++++---- src/mlia/core/context.py | 38 ++++++++++++++++++-------------------- src/mlia/utils/filesystem.py | 13 +++++++++++++ 4 files changed, 46 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/mlia/cli/main.py b/src/mlia/cli/main.py index cc97494..88258d5 100644 --- a/src/mlia/cli/main.py +++ b/src/mlia/cli/main.py @@ -160,12 +160,16 @@ def setup_context( args: argparse.Namespace, context_var_name: str = "ctx" ) -> tuple[ExecutionContext, dict]: """Set up context and resolve function parameters.""" - ctx = ExecutionContext( - verbose="debug" in args and args.debug, - action_resolver=CLIActionResolver(vars(args)), - output_format=get_output_format(args), - output_dir=args.output_dir if "output_dir" in args else None, - ) + try: + ctx = ExecutionContext( + verbose="debug" in args and args.debug, + action_resolver=CLIActionResolver(vars(args)), + output_format=get_output_format(args), + output_dir=args.output_dir if "output_dir" in args else None, + ) + except Exception as err: # pylint: disable=broad-except + print(f"Error: {err}", file=sys.stderr) + sys.exit(1) setup_logging(ctx.logs_path, ctx.verbose, ctx.output_format) diff --git a/src/mlia/cli/options.py b/src/mlia/cli/options.py index d40aa88..fe177eb 100644 --- a/src/mlia/cli/options.py +++ b/src/mlia/cli/options.py @@ -231,10 +231,11 @@ def add_output_directory(parser: argparse.ArgumentParser) -> None: parser.add_argument( "--output-dir", type=Path, - help="Path to the directory where MLIA " - "stores artifacts, e.g. logs, target profiles and model files. " - "If not specified then MLIA will use " - "directory 'mlia-output' in the current working directory instead.", + help="Path to the directory where MLIA will create " + "output directory 'mlia-output' " + "for storing artifacts, e.g. logs, target profiles and model files. " + "If not specified then 'mlia-output' directory will be created " + "in the current working directory.", ) diff --git a/src/mlia/core/context.py b/src/mlia/core/context.py index e8c8a2c..d3fa606 100644 --- a/src/mlia/core/context.py +++ b/src/mlia/core/context.py @@ -12,18 +12,20 @@ from __future__ import annotations import logging from abc import ABC from abc import abstractmethod -from datetime import datetime from pathlib import Path from typing import Any from typing import Mapping from mlia.core.common import AdviceCategory +from mlia.core.errors import ConfigurationError from mlia.core.events import DefaultEventPublisher from mlia.core.events import EventHandler from mlia.core.events import EventPublisher from mlia.core.helpers import ActionResolver from mlia.core.helpers import APIActionResolver from mlia.core.typing import OutputFormat +from mlia.utils.filesystem import recreate_directory +from mlia.utils.filesystem import USER_ONLY_PERM_MASK logger = logging.getLogger(__name__) @@ -97,18 +99,6 @@ class Context(ABC): self.event_publisher.register_event_handlers(self.event_handlers) -def create_output_dir_with_timestamp() -> Path: - """Generate output directory for the MLIA.""" - base_dir = Path.cwd() / "mlia-output" - base_dir.mkdir(exist_ok=True) - - timestamp = datetime.now().isoformat().replace(":", "") - output_dir = base_dir / f"mlia-output-{timestamp}" - output_dir.mkdir() - - return output_dir - - class ExecutionContext(Context): """Execution context.""" @@ -250,10 +240,18 @@ class ExecutionContext(Context): def _init_output_directory(self, output_dir: str | Path | None) -> None: """Init output directory for the execution.""" - if output_dir: - output_dir_path = Path(output_dir) - output_dir_path.mkdir(exist_ok=True) - else: - output_dir_path = create_output_dir_with_timestamp() - - self._output_dir_path = output_dir_path + try: + if output_dir: + output_dir_location = Path(output_dir) + output_dir_location.mkdir(exist_ok=True, mode=USER_ONLY_PERM_MASK) + else: + output_dir_location = Path.cwd() + + output_dir_path = output_dir_location / "mlia-output" + recreate_directory(output_dir_path) + + self._output_dir_path = output_dir_path + except OSError as err: + raise ConfigurationError( + f"Unable to create output directory: {err}." + ) from err diff --git a/src/mlia/utils/filesystem.py b/src/mlia/utils/filesystem.py index f92629b..f8e8962 100644 --- a/src/mlia/utils/filesystem.py +++ b/src/mlia/utils/filesystem.py @@ -14,6 +14,8 @@ from tempfile import TemporaryDirectory from typing import Generator from typing import Iterable +USER_ONLY_PERM_MASK = 0o700 + def get_mlia_resources() -> Path: """Get the path to the resources directory.""" @@ -96,6 +98,17 @@ def copy_all(*paths: Path, dest: Path) -> None: shutil.copytree(path, dest, dirs_exist_ok=True) +def recreate_directory(dir_path: Path, mode: int = USER_ONLY_PERM_MASK) -> None: + """Recreate directory.""" + if dir_path.exists(): + if not dir_path.is_dir(): + raise ValueError(f"Path {dir_path} is not a directory.") + + shutil.rmtree(dir_path) + + dir_path.mkdir(exist_ok=True, mode=mode) + + @contextmanager def working_directory( working_dir: Path, create_dir: bool = False -- cgit v1.2.1