Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use regex to find variable tokens #239

Merged
merged 2 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions merlin/spec/expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
error_override_vars,
)
from merlin.spec.specification import MerlinSpec
from merlin.utils import contains_token


MAESTRO_RESERVED = {"SPECROOT", "WORKSPACE", "LAUNCHER"}
Expand Down Expand Up @@ -74,7 +75,7 @@ def var_ref(string):
by $(<str>).
"""
string = string.upper()
if "$(" in string:
if contains_token(string):
LOG.warning(f"Bad var_ref usage on string '{string}'.")
return string
return f"$({string})"
Expand Down Expand Up @@ -133,14 +134,14 @@ def determine_user_variables(*user_var_dicts):
f"Cannot reassign value of reserved word '{key}'! Reserved words are: {RESERVED}."
)
new_val = str(val)
if "$(" in new_val: # change to re
if contains_token(new_val):
for determined_key in determined_results.keys():
var_determined_key = var_ref(determined_key)
if var_determined_key in new_val:
new_val = new_val.replace(
var_determined_key, determined_results[determined_key]
)
if "$" in new_val: # change to re
if "$" in new_val:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change this one to contains_shell or some such, "${}"

Copy link
Member

@koning koning Jun 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and make a function like contains_token but for shell variables.

new_val = expandvars(new_val)
determined_results[key.upper()] = new_val
return determined_results
Expand Down
3 changes: 2 additions & 1 deletion merlin/study/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
from merlin.spec.specification import MerlinSpec
from merlin.study.dag import DAG
from merlin.utils import (
contains_token,
get_flux_cmd,
load_array_file,
)
Expand Down Expand Up @@ -353,7 +354,7 @@ def expanded_spec(self):
)

# expand provenance spec filename
if "$(" in self.spec.name:
if contains_token(self.spec.name):
expanded_name = result.description["name"].replace(" ", "_") + ".yaml"
expanded_workspace = os.path.join(
self.output_path,
Expand Down
9 changes: 9 additions & 0 deletions merlin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,12 @@ def check_machines(machines):
return True

return False


def contains_token(string):
"""
Return True if given string contains a token of the form $(STR).
"""
if re.search(r"\$\(\w+\)", string):
return True
return False