Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cacts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
""" Main entry point for cacts"""

from cacts.cacts import main as cacts_main
from cacts.get_mach_env import print_mach_env

__version__ = "0.1.0"

def main() -> None:
cacts_main()

def get_mach_env() -> None:
print_mach_env()
64 changes: 11 additions & 53 deletions cacts/cacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
import shutil
import psutil
import json
import re
import itertools
import yaml
import argparse

from .project import Project
from .machine import Machine
from .build_type import BuildType
from .utils import expect, run_cmd, get_current_ref, get_current_sha, is_git_repo, \
check_minimum_python_version, GoodFormatter
from .project import Project
from .machine import Machine
from .build_type import BuildType
from .parse_config import parse_project, parse_machine, parse_builds
from .utils import expect, run_cmd, get_current_ref, get_current_sha, is_git_repo, \
check_minimum_python_version, GoodFormatter

check_minimum_python_version(3, 4)

Expand Down Expand Up @@ -60,7 +59,6 @@ def __init__(self, config_file=None,
self._machine = None
self._builds = []
self._config_file = pathlib.Path(config_file or self._root_dir / "cacts.yaml")
self._local = local

# Ensure work dir exists
self._work_dir.mkdir(parents=True,exist_ok=True)
Expand All @@ -71,8 +69,12 @@ def __init__(self, config_file=None,

expect (self._config_file.exists(),
f"Could not find/open config file: {self._config_file}\n")
expect (not (local and machine_name),
"Makes no sense to use -m/--machine and -l/--local at the same time")

self.parse_config_file(machine_name,build_types)
self._project = parse_project(self._config_file,self._root_dir)
self._machine = parse_machine(self._config_file,self._project,'local' if local else machine_name)
self._builds = parse_builds(self._config_file,self._project,self._machine,self._generate,build_types)

###################################
# Sanity Checks #
Expand Down Expand Up @@ -528,50 +530,6 @@ def check_baselines_are_present(self):
expect (len(missing)==0,
f"Re-run with -g to generate missing baselines for builds {missing}")

###############################################################################
def parse_config_file(self,machine_name,builds_types):
###############################################################################
content = yaml.load(open(self._config_file,"r"),Loader=yaml.SafeLoader)
expect (all(k in content.keys() for k in ['project','machines','configurations']),
"Missing section in configuration file\n"
f" - config file: {self._config_file}\n"
f" - requires sections: project, machines, configurations\n"
f" - sections found: {','.join(content.keys())}\n")

proj = content['project']
machs = content['machines']
configs = content['configurations']

if self._local:
local_yaml = pathlib.Path("~/.cime/cacts.yaml").expanduser()
local_content = yaml.load(open(local_yaml,'r'),Loader=yaml.SafeLoader)
machs.update(local_content['machines'])
machine_name = 'local'

# Build Project
self._project = Project(proj,self._root_dir)

# Build Machine
self._machine = Machine(machine_name,self._project,machs)

# Get builds
if builds_types:
for name in builds_types:
build = BuildType(name,self._project,self._machine,configs)
# Skip non-baselines builds when generating baselines
if not self._generate or build.uses_baselines:
self._builds.append(build)
else:
# Add all build types that are on by default
for name in configs.keys():
if name=='default':
continue
build = BuildType(name,self._project,self._machine,configs)

# Skip non-baselines builds when generating baselines
if (not self._generate or build.uses_baselines) and build.on_by_default:
self._builds.append(build)

###############################################################################
def parse_command_line(args, description, version):
###############################################################################
Expand Down
49 changes: 49 additions & 0 deletions cacts/get_mach_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import sys
import pathlib
import argparse

from .parse_config import parse_project, parse_machine
from .utils import check_minimum_python_version, GoodFormatter

check_minimum_python_version(3, 4)

###############################################################################
def print_mach_env():
###############################################################################
from . import __version__ # Import __version__ here to avoid circular import

args = vars(parse_command_line(sys.argv, __doc__, __version__))

project = parse_project(args['config_file'],args['root_dir'])
machine = parse_machine(args['config_file'],project,args['machine_name'])

print(" && ".join(machine.env_setup))

sys.exit(0)

###############################################################################
def parse_command_line(args, description, version):
###############################################################################
parser = argparse.ArgumentParser(
usage="""\n{0} <ARGS> [--verbose]
OR
{0} --help

\033[1mEXAMPLES:\033[0m
\033[1;32m# Get the env setup command for machine 'foo' using config file my_config.yaml \033[0m
> ./{0} foo -f my_config.yaml
""".format(pathlib.Path(args[0]).name),
description=description,
formatter_class=GoodFormatter
)

parser.add_argument("-f","--config-file", default=f"{os.getcwd()}/cacts.yaml",
help="YAML file containing valid project/machine settings")

parser.add_argument("-r", "--root-dir", default=f"{os.getcwd()}",
help="The root directory of the project, where the main CMakeLists.txt file is located")

parser.add_argument("machine_name", help="The machine name for which you want the scream env")

return parser.parse_args(args[1:])
74 changes: 74 additions & 0 deletions cacts/parse_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pathlib
import yaml

from .project import Project
from .machine import Machine
from .build_type import BuildType
from .utils import expect, check_minimum_python_version

check_minimum_python_version(3, 4)

###############################################################################
def parse_project(config_file,root_dir):
###############################################################################
content = yaml.load(open(config_file,"r"),Loader=yaml.SafeLoader)

expect ('project' in content.keys(),
"Missing 'project' section in configuration file\n"
f" - config file: {config_file}\n"
f" - sections found: {','.join(content.keys())}\n")

# Build Project
return Project(content['project'],root_dir)

###############################################################################
def parse_machine(config_file,project,machine_name):
###############################################################################
content = yaml.load(open(config_file,"r"),Loader=yaml.SafeLoader)

expect ('machines' in content.keys(),
"Missing 'machines' section in configuration file\n"
f" - config file: {config_file}\n"
f" - sections found: {','.join(content.keys())}\n")

# Special handling of 'local' machine
machs = content['machines']
if machine_name=="local":
local_yaml = pathlib.Path("~/.cime/cacts.yaml").expanduser()
local_content = yaml.load(open(local_yaml,'r'),Loader=yaml.SafeLoader)
machs.update(local_content['machines'])
machine_name = 'local'

# Build Machine
return Machine(machine_name,project,machs)

###############################################################################
def parse_builds(config_file,project,machine,generate,build_types=None):
###############################################################################
content = yaml.load(open(config_file,"r"),Loader=yaml.SafeLoader)

expect ('configurations' in content.keys(),
"Missing 'configurations' section in configuration file\n"
f" - config file: {config_file}\n"
f" - sections found: {','.join(content.keys())}\n")

# Get builds
builds = []
if build_types:
for name in build_types:
build = BuildType(name,project,machine,content['configurations'])
# Skip non-baselines builds when generating baselines
if not generate or build.uses_baselines:
builds.append(build)
else:
# Add all build types that are on by default
for name in configs.keys():
if name=='default':
continue
build = BuildType(name,project,machine,content['configurations'])

# Skip non-baselines builds when generating baselines
if (not generate or build.uses_baselines) and build.on_by_default:
builds.append(build)

return builds
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dev = [

[project.scripts]
cacts = "cacts:main"
get-mach-env = "cacts:get_mach_env"

[tool.hatch.version]
path = "cacts/__init__.py"
Expand Down
Loading