Skip to content

Commit

Permalink
remove divvy dependence on attmap
Browse files Browse the repository at this point in the history
  • Loading branch information
nsheff committed Aug 15, 2023
1 parent 5f1cbe9 commit f3c6ff9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion looper/conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def submit(self, force=False):
if self.dry_run:
_LOGGER.info("Dry run, not submitted")
elif self._rendered_ok:
sub_cmd = self.prj.dcc.compute.submission_command
sub_cmd = self.prj.dcc.compute["submission_command"]
submission_command = "{} {}".format(sub_cmd, script)
# Capture submission command return value so that we can
# intercept and report basic submission failures; #167
Expand Down
53 changes: 27 additions & 26 deletions looper/divvy.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# This is the divvy.py submodule from divvy


class ComputingConfiguration(yacman.YacAttMap):
class ComputingConfiguration(yacman.YAMLConfigManager):
"""
Represents computing configuration objects.
Expand All @@ -53,30 +53,30 @@ def __init__(self, entries=None, filepath=None):
entries=entries,
filepath=filepath,
schema_source=DEFAULT_CONFIG_SCHEMA,
write_validate=True,
validate_on_write=True,
)

if not hasattr(self, "compute_packages"):
if not "compute_packages" in self:
raise Exception(
"Your divvy config file is not in divvy config format "
"(it lacks a compute_packages section): '{}'".format(filepath)
)
# We require that compute_packages be present, even if empty
self.compute_packages = {}
self["compute_packages"] = {}

# Initialize default compute settings.
_LOGGER.debug("Establishing project compute settings")
self.compute = None
self.setdefault("adapters", None)
self.activate_package(DEFAULT_COMPUTE_RESOURCES_NAME)
self.config_file = self["__internal"].file_path
self.config_file = self.filepath

def write(self, filename=None):
super(ComputingConfiguration, self).write(filepath=filename, exclude_case=True)
filename = filename or getattr(self, yacman.FILEPATH_KEY)
filedir = os.path.dirname(filename)
# For this object, we *also* have to write the template files
for pkg_name, pkg in self.compute_packages.items():
for pkg_name, pkg in self["compute_packages"].items():
print(pkg)
destfile = os.path.join(filedir, os.path.basename(pkg.submission_template))
shutil.copyfile(pkg.submission_template, destfile)
Expand Down Expand Up @@ -109,7 +109,7 @@ def template(self):
:return str: submission script content template for current state
"""
with open(self.compute.submission_template, "r") as f:
with open(self.compute["submission_template"], "r") as f:
return f.read()

@property
Expand Down Expand Up @@ -145,36 +145,36 @@ def activate_package(self, package_name):

if (
package_name
and self.compute_packages
and package_name in self.compute_packages
and self["compute_packages"]
and package_name in self["compute_packages"]
):
# Augment compute, creating it if needed.
if self.compute is None:
_LOGGER.debug("Creating Project compute")
self.compute = yacman.YacAttMap()
self.compute = yacman.YAMLConfigManager()
_LOGGER.debug(
"Adding entries for package_name '{}'".format(package_name)
)

self.compute.add_entries(self.compute_packages[package_name])
self.compute.update(self["compute_packages"][package_name])

# Ensure submission template is absolute. This *used to be* handled
# at update (so the paths were stored as absolutes in the packages),
# but now, it makes more sense to do it here so we can piggyback on
# the default update() method and not even have to do that.
if not os.path.isabs(self.compute.submission_template):
if not os.path.isabs(self.compute["submission_template"]):
try:
self.compute.submission_template = os.path.join(
os.path.dirname(self["__internal"].file_path),
self.compute.submission_template,
self.compute["submission_template"] = os.path.join(
os.path.dirname(self.filepath),
self.compute["submission_template"],
)
except AttributeError as e:
# Environment and environment compute should at least have been
# set as null-valued attributes, so execution here is an error.
_LOGGER.error(str(e))

_LOGGER.debug(
"Submit template set to: {}".format(self.compute.submission_template)
"Submit template set to: {}".format(self.compute["submission_template"])
)

return True
Expand All @@ -184,7 +184,7 @@ def activate_package(self, package_name):
# both present--but don't evaluate to True--is fairly harmless.
_LOGGER.debug(
"Can't activate package. compute_packages = {}".format(
self.compute_packages
self["compute_packages"]
)
)

Expand Down Expand Up @@ -214,7 +214,7 @@ def list_compute_packages(self):
:return set[str]: names of available compute packages
"""
return set(self.compute_packages.keys())
return set(self["compute_packages"].keys())

def reset_active_settings(self):
"""
Expand Down Expand Up @@ -248,13 +248,13 @@ def get_adapters(self):
package-specific set of adapters, if any defined in 'adapters' section
under currently active compute package.
:return yacman.YacAttMap: current adapters mapping
:return yacman.YAMLConfigManager: current adapters mapping
"""
adapters = yacman.YacAttMap()
if "adapters" in self and self.adapters is not None:
adapters.update(self.adapters)
adapters = yacman.YAMLConfigManager()
if "adapters" in self and self["adapters"] is not None:
adapters.update(self["adapters"])
if "compute" in self and "adapters" in self.compute:
adapters.update(self.compute.adapters)
adapters.update(self.compute["adapters"])
if not adapters:
_LOGGER.debug("No adapters determined in divvy configuration file.")
return adapters
Expand All @@ -270,7 +270,7 @@ def submit(self, output_path, extra_vars=None):
self.submit(temp.name, extra_vars)
else:
script = self.write_script(output_path, extra_vars)
submission_command = "{} {}".format(self.compute.submission_command, script)
submission_command = "{} {}".format(self.compute["submission_command"], script)
_LOGGER.info(submission_command)
os.system(submission_command)

Expand Down Expand Up @@ -337,7 +337,7 @@ def _get_from_dict(map, attrs):
if len(extra_var) > 0 and list(extra_var.keys())[0] not in exclude:
variables.update(extra_var)
_LOGGER.debug(
"Submission template: {}".format(self.compute.submission_template)
"Submission template: {}".format(self.compute["submission_template"])
)
if output_path:
_LOGGER.info("Writing script to {}".format(os.path.abspath(output_path)))
Expand Down Expand Up @@ -379,6 +379,7 @@ def select_divvy_config(filepath):
config_env_vars=COMPUTE_SETTINGS_VARNAME,
default_config_filepath=DEFAULT_CONFIG_FILEPATH,
check_exist=True,
config_name="divvy",
)
_LOGGER.debug("Selected divvy config: {}".format(divcfg))
return divcfg
Expand Down Expand Up @@ -506,7 +507,7 @@ def add_subparser(cmd, description):


def main():
"""Primary workflow"""
"""Primary workflow for divvy CLI"""

parser = logmuse.add_logging_options(build_argparser())
# args, remaining_args = parser.parse_known_args()
Expand Down
4 changes: 2 additions & 2 deletions tests/divvytests/divvy_tests/test_divvy.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_activating_default_package(self, dcc):
@pytest.mark.parametrize(argnames="package_idx", argvalues=[0, 1])
def test_activating_some_package(self, dcc, package_idx):
"""Test if activating the default compute package works for every case"""
package = list(dcc.compute_packages.keys())[package_idx]
package = list(dcc["compute_packages"].keys())[package_idx]
assert dcc.activate_package(package)

@pytest.mark.parametrize(
Expand Down Expand Up @@ -98,4 +98,4 @@ def test_update_packages(self, dcc, config_file):
"""Test updating does not produce empty compute packages"""
entries = load_yaml(config_file)
dcc.update(entries)
assert dcc.compute_packages != YacAttMap()
assert dcc["compute_packages"] != YacAttMap()
4 changes: 2 additions & 2 deletions tests/divvytests/regression/test_write_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
def test_write_script_is_effect_free(tmpdir, extras):
"""Writing script doesn't change computing configuration."""
cc = ComputingConfiguration()
compute1 = deepcopy(cc.compute_packages)
compute1 = deepcopy(cc["compute_packages"])
cc.write_script(tmpdir.join(get_random_key(20) + ".sh").strpath, extras)
assert cc.compute_packages == compute1
assert cc["compute_packages"] == compute1
14 changes: 7 additions & 7 deletions tests/divvytests/test_divvy_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
# logmuse.init_logger("divvy", "DEBUG")


class TestPackageaAtivation:
class TestPackageAtivation:
def test_activate_package(self):
dcc = divvy.ComputingConfiguration()
dcc.activate_package("default")
t = dcc.compute.submission_template
t2 = dcc["compute"]["submission_template"]
assert t == t2
t = dcc.compute["submission_template"]
t2 = dcc["compute_packages"]["default"]["submission_template"]
# assert t == t2
dcc.activate_package("slurm")
t = dcc.compute.submission_template
t2 = dcc["compute"]["submission_template"]
assert t == t2
t = dcc.compute["submission_template"]
t2 = dcc["compute_packages"]["slurm"]["submission_template"]
# assert t == t2


class TestWriting:
Expand Down

0 comments on commit f3c6ff9

Please sign in to comment.