diff --git a/riocli/device/util.py b/riocli/device/util.py index 58a16776..2422bdb5 100644 --- a/riocli/device/util.py +++ b/riocli/device/util.py @@ -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 @@ -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 diff --git a/riocli/parameter/apply.py b/riocli/parameter/apply.py index 08771dcb..452da9b1 100644 --- a/riocli/parameter/apply.py +++ b/riocli/parameter/apply.py @@ -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( @@ -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, @@ -47,21 +51,32 @@ 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: @@ -69,7 +84,7 @@ def apply_configurations( 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)