Skip to content

Commit

Permalink
fix(parameters): apply with device name pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Jan 24, 2024
1 parent 67c0b58 commit 837f559
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
12 changes: 6 additions & 6 deletions riocli/device/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
import re
import typing
from pathlib import Path
import re

import click
from rapyuta_io import Client
Expand Down Expand Up @@ -102,18 +102,18 @@ def decorated(**kwargs):

return decorated


def fetch_devices(
client: Client,
device_name_or_regex: str,
include_all: bool,
online_devices: bool = False
) -> typing.List[Device]:
devices = client.get_all_devices()
devices = client.get_all_devices(online_device=online_devices)
result = []
for device in devices:
if (include_all or device.name == device_name_or_regex or
(device_name_or_regex not in device.name and
re.search(device_name_or_regex, device.name)) or
device_name_or_regex == device.uuid):
if (include_all or device_name_or_regex == device.uuid or
re.search(device_name_or_regex, device.name)):
result.append(device)

return result
Expand Down
33 changes: 24 additions & 9 deletions riocli/parameter/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from riocli.constants import Colors, Symbols
from riocli.utils import tabulate_data, print_separator
from riocli.parameter.utils import list_trees
from riocli.device.util import fetch_devices


@click.command(
Expand All @@ -35,9 +36,12 @@
help_options_color=Colors.GREEN,
)
@click.option('--devices', type=click.STRING, multiple=True, default=(),
help='Device names to apply configurations')
help='Device names to apply configurations. If --device_name_pattern is'
'provided, this will be ignored.')
@click.option('--device-name-pattern', type=click.STRING, multiple=False,
help='Device name regex pattern to apply configurations. Does not work with --devices.')
@click.option('--tree-names', type=click.STRING, multiple=True, default=None,
help='Tree names to apply')
help='Tree names to apply to the device(s)')
@click.option('--retry-limit', type=click.INT, default=0,
help='Retry limit')
@click.option('-f', '--force', '--silent', 'silent', is_flag=True,
Expand All @@ -47,29 +51,40 @@ def apply_configurations(
devices: typing.List,
tree_names: typing.List[str] = None,
retry_limit: int = 0,
device_name_pattern: str = None,
silent: bool = False,
) -> None:
"""
Apply a set of configurations to a list of devices
"""
try:
client = new_client()
client = new_client()

if tree_names:
validate_trees(tree_names)

if tree_names:
validate_trees(tree_names)
online_devices = client.get_all_devices(online_device=True)

try:
# If device_name_pattern is specified, fetch devices based on the pattern
# else fetch all devices. That means, include_all is False if
# device_name_pattern is specified for the fetch_devices function.
include_all = not len(device_name_pattern or '') > 0
online_devices = fetch_devices(client, device_name_pattern, include_all=include_all, online_devices=True)

online_devices = client.get_all_devices(online_device=True)
device_map = {d.name: d for d in online_devices}

if devices:
# If devices are specified, filter online devices based on the
# list of device names. But if device_name_pattern is specified
# this list of devices will not be honoured.
if devices and not device_name_pattern:
device_ids = {device_map[d].uuid: d for d in devices if
d in device_map}
else:
device_ids = {v.uuid: k for k, v in device_map.items()}

if not device_ids:
click.secho(
"{} Invalid devices or no device is currently online".format(
"{} No device(s) found online. Please check the name or pattern".format(
Symbols.ERROR),
fg=Colors.RED)
raise SystemExit(1)
Expand Down

0 comments on commit 837f559

Please sign in to comment.