Skip to content

Commit

Permalink
Improve branch boundary updates in workflows.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Dec 30, 2022
1 parent a2a8e49 commit 5ba9d74
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
6 changes: 3 additions & 3 deletions law/sandbox/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,11 @@ def is_sandboxed(self):

def is_root_task(self):
is_root = super(SandboxTask, self).is_root_task()
if _sandbox_switched:
return is_root and _sandbox_is_root_task
else:
if not _sandbox_switched:
return is_root

return is_root and _sandbox_is_root_task

def _staged_input(self):
if not _sandbox_stagein_dir:
raise Exception("LAW_SANDBOX_STAGEIN_DIR must not be empty in a sandbox when target "
Expand Down
22 changes: 18 additions & 4 deletions law/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,17 @@ def range_expand(s, include_end=False, min_value=None, max_value=None, sep=":"):
"""
Takes a string, or a sequence of strings in the format ``"1:3"``, or a tuple or a sequence of
tuples containing start and stop values of a range and returns a list of all intermediate
values. When *include_end* is *True*, the end value is included. One sided range expressions
such as ``":4"`` or ``"4:"`` for strings and ``(None, 4)`` or ``(4, None)`` for tuples are also
expanded but they require *min_value* and *max_value* to be set (an exception is raised
otherwise), with *max_value* being either included or not, depending on *include_end*. Example:
values. When *include_end* is *True*, the end value is included.
One sided range expressions such as ``":4"`` or ``"4:"`` for strings and ``(None, 4)`` or
``(4, None)`` for tuples are also expanded but they require *min_value* and *max_value* to be
set (an exception is raised otherwise), with *max_value* being either included or not, depending
on *include_end*.
Also, when a *min_value* (*max_value*) is set, the minimum (maximum) of expanded range is
limited at this value.
Example:
.. code-block:: python
Expand Down Expand Up @@ -634,6 +641,13 @@ def to_int(v, s=None):
# remove duplicates preserving the order
numbers = make_unique(numbers)

# apply limits
if min_value is not None:
numbers = [num for num in numbers if num >= min_value]
if max_value is not None:
py_max_value = (max_value + 1) if include_end else max_value
numbers = [num for num in numbers if num < py_max_value]

return numbers


Expand Down
21 changes: 14 additions & 7 deletions law/workflow/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,19 +549,26 @@ def create_branch_map(self):
"""
return

def _reset_branch_boundaries(self, branch_map):
def _reset_branch_boundaries(self, full_branch_map):
if self.is_branch():
raise Exception("calls to _reset_branch_boundaries are forbidden for branch tasks")

# rejoin branch ranges when given
if self.branches:
# get minimum and maximum branches
min_branch = min(branch_map.keys())
max_branch = max(branch_map.keys())
min_branch = min(full_branch_map.keys())
max_branch = max(full_branch_map.keys())

branches = range_expand(list(self.branches), min_value=min_branch,
max_value=max_branch + 1)
self.branches = tuple(range_join(branches))
# get expanded branch values
branches = range_expand(list(self.branches), min_value=min_branch, max_value=max_branch,
include_end=True)

# assign back to branches attribute, use an empty tuple in case all branches are used
use_all = (
len(branches) == len(full_branch_map) and
set(branches) == set(full_branch_map)
)
self.branches = () if use_all else tuple(range_join(branches))

def _reduce_branch_map(self, branch_map):
if self.is_branch():
Expand All @@ -577,7 +584,7 @@ def _reduce_branch_map(self, branch_map):
max_branch = max(branches)

requested = range_expand(list(self.branches), min_value=min_branch,
max_value=max_branch + 1)
max_value=max_branch, include_end=True)
remove_branches |= branches - set(requested)

# remove from branch map
Expand Down

0 comments on commit 5ba9d74

Please sign in to comment.