Skip to content

Commit

Permalink
Added function tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
AcerP-py committed Nov 15, 2024
1 parent a593779 commit a0fd1e6
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 54 deletions.
80 changes: 39 additions & 41 deletions src/velocity/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"-v", "--version", action="version", version=f"%(prog)s {version('olcf-velocity')}", help="program version"
)
parser.add_argument(
"-D",
"--debug",
"-L",
"--logging-level",
choices=["TRACE", "DEBUG", "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL"],
help="set debug output level",
)
Expand Down Expand Up @@ -63,42 +63,44 @@
args = parser.parse_args()

# parse arguments & variables
for argument in args.arguments:
try:
constructed_arg: dict = dict()
a_split: list = argument.split(";")
for a_item in a_split:
parts: dict = re_fullmatch(r"^\s*(?P<key>\S+)\s*:\s*(?P<value>\S+)\s*$", a_item).groupdict()
constructed_arg[parts["key"]] = parts["value"]
prev_arguments: list | None = config.get("constraints:arguments", warn_on_miss=False)
if prev_arguments is None:
config.set("constraints:arguments", [constructed_arg, ])
else:
prev_arguments.append(constructed_arg)
config.set("constraints:arguments", prev_arguments)
except AttributeError:
raise InvalidCLIArgumentFormat("Invalid format in '{}'".format(argument))
for variable in args.variables:
try:
constructed_var: dict = dict()
v_split: list = variable.split(";")
for v_item in v_split:
parts: dict = re_fullmatch(r"^\s*(?P<key>\S+)\s*:\s*(?P<value>\S+)\s*$", v_item).groupdict()
constructed_var[parts["key"]] = parts["value"]
prev_variables: list | None = config.get("constraints:variables", warn_on_miss=False)
if prev_variables is None:
config.set("constraints:variables", [constructed_var, ])
else:
prev_variables.append(constructed_var)
config.set("constraints:variables", prev_variables)
except AttributeError:
raise InvalidCLIArgumentFormat("Invalid format in '{}'".format(variable))
if "argument" in args:
for argument in args.arguments:
try:
constructed_arg: dict = dict()
a_split: list = argument.split(";")
for a_item in a_split:
parts: dict = re_fullmatch(r"^\s*(?P<key>\S+)\s*:\s*(?P<value>\S+)\s*$", a_item).groupdict()
constructed_arg[parts["key"]] = parts["value"]
prev_arguments: list | None = config.get("constraints:arguments", warn_on_miss=False)
if prev_arguments is None:
config.set("constraints:arguments", [constructed_arg, ])
else:
prev_arguments.append(constructed_arg)
config.set("constraints:arguments", prev_arguments)
except AttributeError:
raise InvalidCLIArgumentFormat("Invalid format in '{}'".format(argument))
if "variables" in args:
for variable in args.variables:
try:
constructed_var: dict = dict()
v_split: list = variable.split(";")
for v_item in v_split:
parts: dict = re_fullmatch(r"^\s*(?P<key>\S+)\s*:\s*(?P<value>\S+)\s*$", v_item).groupdict()
constructed_var[parts["key"]] = parts["value"]
prev_variables: list | None = config.get("constraints:variables", warn_on_miss=False)
if prev_variables is None:
config.set("constraints:variables", [constructed_var, ])
else:
prev_variables.append(constructed_var)
config.set("constraints:variables", prev_variables)
except AttributeError:
raise InvalidCLIArgumentFormat("Invalid format in '{}'".format(variable))

############################################################
# apply user run time arguments over settings
############################################################
if args.debug is not None:
config.set("velocity:debug", args.debug)
if args.logging_level is not None:
config.set("velocity:logging:level", args.logging_level)
if args.system is not None:
config.set("velocity:system", args.system)
if args.backend is not None:
Expand All @@ -108,13 +110,9 @@

# setup logging and log startup
logger.enable("velocity")
logger.configure(handlers=[{"sink": sys.stdout, "level": config.get("velocity:debug")}])
logger.debug(
"Starting velocity {},{},{}.".format(
config.get("velocity:system"), config.get("velocity:backend"), config.get("velocity:distro")
)
)
logger.trace(config.get(""))
logger.configure(handlers=[{"sink": sys.stdout, "level": config.get("velocity:logging:level")}])
logger.debug("Starting velocity.")
logger.debug(config.get(""))

############################################################
# Load images
Expand Down
10 changes: 8 additions & 2 deletions src/velocity/_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from loguru import logger
from shutil import which as shutil_which
from pathlib import Path
from abc import ABC, abstractmethod
from abc import abstractmethod
from ._exceptions import (
RepeatedSection,
LineOutsideOfSection,
Expand All @@ -14,8 +14,10 @@
)
from ._config import config
from ._graph import Image
from ._tools import OurABCMeta, trace_function


@trace_function
def _substitute(text: str, variables: dict[str, str], regex: str) -> str:
"""Substitute a variables in a string by a regex."""

Expand All @@ -28,7 +30,7 @@ def _replace(m: re_Match):
return re_sub(regex, _replace, text)


class Backend(ABC):
class Backend(metaclass=OurABCMeta):
"""Abstract class for velocity backend."""

template_sections: list[str] = [
Expand All @@ -43,6 +45,7 @@ class Backend(ABC):
]

@classmethod
@trace_function
def _get_sections(cls, template: list[str]) -> dict[str, list[str]]:
"""Retrieve the sections from a VTMP."""

Expand Down Expand Up @@ -83,6 +86,7 @@ def _get_sections(cls, template: list[str]) -> dict[str, list[str]]:
return sections

@classmethod
@trace_function
def _filter_content(cls, image: Image, text: str) -> str:
"""Filter conditionals and white space from a template line."""
# handle conditionals
Expand All @@ -101,6 +105,7 @@ def _filter_content(cls, image: Image, text: str) -> str:
return text

@classmethod
@trace_function
def _load_template(cls, image: Image, variables: dict[str, str]) -> list[str]:
"""Load a template and parse it."""
template: list[str] = list()
Expand Down Expand Up @@ -681,6 +686,7 @@ def generate_final_image_cmd(self, src: str, dest: str) -> str:
return "cp {} {}".format(src, dest)


@trace_function
def get_backend() -> Backend:
backend = config.get("velocity:backend")
if backend == "apptainer":
Expand Down
5 changes: 4 additions & 1 deletion src/velocity/_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
from ._graph import Image
from ._print import header_print, indent_print, TextBlock
from ._backends import get_backend, Backend
from ._tools import OurMeta, trace_function


@trace_function
def read_pipe(pipe: PIPE, topic: SimpleQueue, prefix: str, log: SimpleQueue) -> None:
"""Read a subprocess PIPE and place lines on topic queue and log queue."""
while True:
Expand All @@ -27,6 +29,7 @@ def read_pipe(pipe: PIPE, topic: SimpleQueue, prefix: str, log: SimpleQueue) ->
log.put("{} {}".format(prefix, ln.strip("\n")))


@trace_function
def run(cmd: str, log_file: Path = None, verbose: bool = False) -> None:
"""Run a system command logging all output to a file and print if verbose."""
# open log file (set to False if none is provided)
Expand Down Expand Up @@ -73,7 +76,7 @@ def run(cmd: str, log_file: Path = None, verbose: bool = False) -> None:
exit(process.poll())


class ImageBuilder:
class ImageBuilder(metaclass=OurMeta):
"""Image building class."""

def __init__(
Expand Down
8 changes: 4 additions & 4 deletions src/velocity/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from os import getlogin as get_username, getenv
from yaml import safe_load as yaml_safe_load
from ._exceptions import InvalidConfigIdentifier
from ._tools import OurMeta


class Config:
class Config(metaclass=OurMeta):
"""Configuration class. Stores configuration as a dictionary."""

def __init__(self) -> None:
Expand Down Expand Up @@ -109,8 +109,8 @@ def __str__(self) -> str:
if _config.get("velocity:distro", warn_on_miss=False) is None:
_config.set("velocity:distro", "ubuntu")

if _config.get("velocity:debug", warn_on_miss=False) is None:
_config.set("velocity:debug", "WARNING")
if _config.get("velocity:logging:level", warn_on_miss=False) is None:
_config.set("velocity:logging:level", "WARNING")

if _config.get("velocity:image_path", warn_on_miss=False) is None:
image_dir = Path.home().joinpath(".velocity", "images")
Expand Down
12 changes: 7 additions & 5 deletions src/velocity/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
)
from ._config import config
from ._exceptions import InvalidImageVersionError, CannotFindDependency, EdgeViolatesDAG, NoAvailableBuild
from ._tools import OurMeta, trace_function


@trace_function
def get_permutations(idx: int, sets: list[list]):
"""
Get all the possible permutations from a list of lists
Expand All @@ -40,7 +42,7 @@ def get_permutations(idx: int, sets: list[list]):
return permutations


class Version:
class Version(metaclass=OurMeta):
"""Version class."""

def __init__(self, version_specifier: str) -> None:
Expand Down Expand Up @@ -147,7 +149,7 @@ def __str__(self) -> str:
return self.vs


class Image:
class Image(metaclass=OurMeta):
"""Velocity container image."""

def __init__(self, name: str, version: str, system: str, backend: str, distro: str, path: str) -> None:
Expand Down Expand Up @@ -335,7 +337,7 @@ class DepOp(Enum):
UN = None


class Target:
class Target(metaclass=OurMeta):
"""Build targets."""

def __init__(self, node: Image, op: DepOp):
Expand All @@ -346,7 +348,7 @@ def __str__(self):
return "Target: {} -> {}".format(self.op, self.node)


class ImageGraph(nx_DiGraph):
class ImageGraph(nx_DiGraph, metaclass=OurMeta):
"""Image dependency graph."""

def __init__(self, **kwargs) -> None:
Expand Down Expand Up @@ -521,7 +523,7 @@ def create_build_recipe(self, targets: list[Target]) -> tuple:
raise NoAvailableBuild("No Available build!")


class ImageRepo:
class ImageRepo(metaclass=OurMeta):
"""Image repository."""

def __init__(self) -> None:
Expand Down
6 changes: 5 additions & 1 deletion src/velocity/_print.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Printing formatter."""

from colorama import Fore, Back, Style
from ._tools import OurMeta, trace_function


class TextBlock:
class TextBlock(metaclass=OurMeta):
"""Defines a block of text and its styling."""

def __init__(
Expand All @@ -19,6 +20,7 @@ def __init__(
self.style = style


@trace_function
def bare_print(tb: list[TextBlock]) -> None:
"""Print a list of TextBlocks."""
for text_block in tb:
Expand All @@ -36,13 +38,15 @@ def bare_print(tb: list[TextBlock]) -> None:
print()


@trace_function
def header_print(tb: list[TextBlock]) -> None:
"""Print a list of TextBlocks with a preceding '==> ' block."""
text_blocks = [TextBlock("==> ", fore=Fore.GREEN, style=Style.BRIGHT)]
text_blocks.extend(tb)
bare_print(text_blocks)


@trace_function
def indent_print(tb: list[TextBlock]) -> None:
"""Print a list of TextBlocks with a preceding 4 spaces."""
text_blocks = [TextBlock(" ", fore=Fore.GREEN, style=Style.BRIGHT)]
Expand Down
Loading

0 comments on commit a0fd1e6

Please sign in to comment.