2023-06-07 22:40:16 +01:00
|
|
|
# Crown Copyright (C) Dstl 2022. DEFCON 703. Shared in confidence.
|
|
|
|
|
"""Provides a CLI using Typer as an entry point."""
|
2023-06-09 22:23:45 +01:00
|
|
|
import logging
|
2023-06-07 22:40:16 +01:00
|
|
|
import os
|
2023-06-09 15:49:48 +01:00
|
|
|
import shutil
|
2023-06-09 22:23:45 +01:00
|
|
|
from enum import Enum
|
2023-06-27 13:06:10 +01:00
|
|
|
from pathlib import Path
|
2023-06-09 22:23:45 +01:00
|
|
|
from typing import Optional
|
2023-06-07 22:40:16 +01:00
|
|
|
|
2023-06-09 15:49:48 +01:00
|
|
|
import pkg_resources
|
2023-06-07 22:40:16 +01:00
|
|
|
import typer
|
2023-06-09 22:23:45 +01:00
|
|
|
import yaml
|
2023-06-09 15:49:48 +01:00
|
|
|
from platformdirs import PlatformDirs
|
2023-06-09 16:44:49 +01:00
|
|
|
from typing_extensions import Annotated
|
2023-06-07 22:40:16 +01:00
|
|
|
|
|
|
|
|
app = typer.Typer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
|
|
|
|
def build_dirs():
|
|
|
|
|
"""Build the PrimAITE app directories."""
|
|
|
|
|
from primaite.setup import setup_app_dirs
|
|
|
|
|
|
|
|
|
|
setup_app_dirs.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
|
|
|
|
def reset_notebooks(overwrite: bool = True):
|
|
|
|
|
"""
|
|
|
|
|
Force a reset of the demo notebooks in the users notebooks directory.
|
|
|
|
|
|
|
|
|
|
:param overwrite: If True, will overwrite existing demo notebooks.
|
|
|
|
|
"""
|
|
|
|
|
from primaite.setup import reset_demo_notebooks
|
|
|
|
|
|
|
|
|
|
reset_demo_notebooks.run(overwrite)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
2023-06-09 16:44:49 +01:00
|
|
|
def logs(last_n: Annotated[int, typer.Option("-n")]):
|
2023-06-07 22:40:16 +01:00
|
|
|
"""
|
|
|
|
|
Print the PrimAITE log file.
|
|
|
|
|
|
|
|
|
|
:param last_n: The number of lines to print. Default value is 10.
|
|
|
|
|
"""
|
|
|
|
|
import re
|
2023-06-27 13:06:10 +01:00
|
|
|
|
2023-06-09 16:44:49 +01:00
|
|
|
from primaite import LOG_PATH
|
2023-06-07 22:40:16 +01:00
|
|
|
|
2023-06-09 16:44:49 +01:00
|
|
|
if os.path.isfile(LOG_PATH):
|
|
|
|
|
with open(LOG_PATH) as file:
|
2023-06-07 22:40:16 +01:00
|
|
|
lines = file.readlines()
|
|
|
|
|
for line in lines[-last_n:]:
|
|
|
|
|
print(re.sub(r"\n*", "", line))
|
|
|
|
|
|
|
|
|
|
|
2023-06-27 13:06:10 +01:00
|
|
|
_LogLevel = Enum("LogLevel", {k: k for k in logging._levelToName.values()}) # noqa
|
2023-06-09 22:23:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
|
|
|
|
def log_level(level: Annotated[Optional[_LogLevel], typer.Argument()] = None):
|
|
|
|
|
"""
|
|
|
|
|
View or set the PrimAITE Log Level.
|
|
|
|
|
|
|
|
|
|
To View, simply call: primaite log-level
|
|
|
|
|
|
|
|
|
|
To set, call: primaite log-level <desired log level>
|
|
|
|
|
|
|
|
|
|
For example, to set the to debug, call: primaite log-level DEBUG
|
|
|
|
|
"""
|
|
|
|
|
app_dirs = PlatformDirs(appname="primaite")
|
|
|
|
|
app_dirs.user_config_path.mkdir(exist_ok=True, parents=True)
|
|
|
|
|
user_config_path = app_dirs.user_config_path / "primaite_config.yaml"
|
|
|
|
|
if user_config_path.exists():
|
|
|
|
|
with open(user_config_path, "r") as file:
|
|
|
|
|
primaite_config = yaml.safe_load(file)
|
|
|
|
|
|
|
|
|
|
if level:
|
|
|
|
|
primaite_config["log_level"] = level.value
|
|
|
|
|
with open(user_config_path, "w") as file:
|
|
|
|
|
yaml.dump(primaite_config, file)
|
|
|
|
|
else:
|
|
|
|
|
level = primaite_config["log_level"]
|
|
|
|
|
print(f"PrimAITE Log Level: {level}")
|
|
|
|
|
|
|
|
|
|
|
2023-06-07 22:40:16 +01:00
|
|
|
@app.command()
|
|
|
|
|
def notebooks():
|
|
|
|
|
"""Start Jupyter Lab in the users PrimAITE notebooks directory."""
|
|
|
|
|
from primaite.notebooks import start_jupyter_session
|
|
|
|
|
|
|
|
|
|
start_jupyter_session()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
|
|
|
|
def version():
|
|
|
|
|
"""Get the installed PrimAITE version number."""
|
|
|
|
|
import primaite
|
|
|
|
|
|
|
|
|
|
print(primaite.__version__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
|
|
|
|
def clean_up():
|
|
|
|
|
"""Cleans up left over files from previous version installations."""
|
|
|
|
|
from primaite.setup import old_installation_clean_up
|
|
|
|
|
|
|
|
|
|
old_installation_clean_up.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
2023-06-09 15:49:48 +01:00
|
|
|
def setup(overwrite_existing: bool = True):
|
2023-06-07 22:40:16 +01:00
|
|
|
"""
|
|
|
|
|
Perform the PrimAITE first-time setup.
|
|
|
|
|
|
|
|
|
|
WARNING: All user-data will be lost.
|
|
|
|
|
"""
|
2023-06-09 16:44:49 +01:00
|
|
|
# Does this way to avoid using PrimAITE package before config is loaded
|
2023-06-09 15:49:48 +01:00
|
|
|
app_dirs = PlatformDirs(appname="primaite")
|
2023-06-09 16:04:56 +01:00
|
|
|
app_dirs.user_config_path.mkdir(exist_ok=True, parents=True)
|
2023-06-09 15:49:48 +01:00
|
|
|
user_config_path = app_dirs.user_config_path / "primaite_config.yaml"
|
|
|
|
|
build_config = overwrite_existing or (not user_config_path.exists())
|
|
|
|
|
if build_config:
|
|
|
|
|
pkg_config_path = Path(
|
|
|
|
|
pkg_resources.resource_filename(
|
|
|
|
|
"primaite", "setup/_package_data/primaite_config.yaml"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
shutil.copy2(pkg_config_path, user_config_path)
|
|
|
|
|
|
2023-06-07 22:40:16 +01:00
|
|
|
from primaite import getLogger
|
|
|
|
|
from primaite.setup import (
|
|
|
|
|
old_installation_clean_up,
|
|
|
|
|
reset_demo_notebooks,
|
|
|
|
|
reset_example_configs,
|
|
|
|
|
setup_app_dirs,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
_LOGGER = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
_LOGGER.info("Performing the PrimAITE first-time setup...")
|
|
|
|
|
|
2023-06-09 15:49:48 +01:00
|
|
|
if build_config:
|
|
|
|
|
_LOGGER.info("Building primaite_config.yaml...")
|
|
|
|
|
|
2023-06-07 22:40:16 +01:00
|
|
|
_LOGGER.info("Building the PrimAITE app directories...")
|
|
|
|
|
setup_app_dirs.run()
|
|
|
|
|
|
|
|
|
|
_LOGGER.info("Rebuilding the demo notebooks...")
|
|
|
|
|
reset_demo_notebooks.run(overwrite_existing=True)
|
|
|
|
|
|
|
|
|
|
_LOGGER.info("Rebuilding the example notebooks...")
|
|
|
|
|
reset_example_configs.run(overwrite_existing=True)
|
|
|
|
|
|
|
|
|
|
_LOGGER.info("Performing a clean-up of previous PrimAITE installations...")
|
|
|
|
|
old_installation_clean_up.run()
|
|
|
|
|
|
|
|
|
|
_LOGGER.info("PrimAITE setup complete!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.command()
|
2023-06-28 12:01:01 +01:00
|
|
|
def session(tc: Optional[str] = None, ldc: Optional[str] = None):
|
2023-06-07 22:40:16 +01:00
|
|
|
"""
|
|
|
|
|
Run a PrimAITE session.
|
|
|
|
|
|
2023-06-28 12:01:01 +01:00
|
|
|
tc: The training config filepath. Optional. If no value is passed then
|
|
|
|
|
example default training config is used from:
|
|
|
|
|
~/primaite/config/example_config/training/training_config_main.yaml.
|
|
|
|
|
|
|
|
|
|
ldc: The lay down config file path. Optional. If no value is passed then
|
|
|
|
|
example default lay down config is used from:
|
|
|
|
|
~/primaite/config/example_config/lay_down/lay_down_config_5_data_manipulation.yaml.
|
2023-06-07 22:40:16 +01:00
|
|
|
"""
|
|
|
|
|
from primaite.main import run
|
2023-06-28 12:01:01 +01:00
|
|
|
from primaite.config.training_config import main_training_config_path
|
|
|
|
|
from primaite.config.lay_down_config import data_manipulation_config_path
|
|
|
|
|
|
|
|
|
|
if not tc:
|
|
|
|
|
tc = main_training_config_path()
|
|
|
|
|
|
|
|
|
|
if not ldc:
|
|
|
|
|
ldc = data_manipulation_config_path()
|
2023-06-07 22:40:16 +01:00
|
|
|
|
|
|
|
|
run(training_config_path=tc, lay_down_config_path=ldc)
|