Returns: str: returns a formatted string of response.text """ return "".join(i for i in response.text if i not in REGEXES["COMMAND_RESPONSE"]) def _log_to_file(self, log_message, port="M"): """Adds a log message for each command.""" if not log_message.endswith("\n"): log_message += "\n" with open(self.log_file_name, "a") as out_file: logline = log_process._add_log_header(log_message, port=port) out_file.write(logline) def _list_properties_dynamic_powerswitch(self): """Returns a set of dynamic properties. Returns: set: dynamic properties that can be queried during get-prop CLI/API operation. """ return set(["firmware_version"]) deprecation_utils.add_deprecated_attributes( DliPowerSwitch, [("powerswitch_power", "switch_power", False), ("do_and_expect", "switchboard.do_and_expect", True), ("expect", "switchboard.expect", True), ("send", "switchboard.send", True), ("send_and_expect", "switchboard.send_and_expect", True)])
while time.time() < max_disconnect_time: if host_utils.is_pingable(self.ip_address): logger.info("{} online in {}s".format(self.name, int(time.time() - start_time))) break time.sleep(.5) else: raise errors.DeviceNotBootupCompleteError( self.name, "failed to come online {}s".format(int(time.time() - start_time))) # There's a delay between the device being responsive to ping # and being able to open SSH connections time.sleep(self.timeouts["PING_TO_SSH_DELAY"]) # Reopen ssh transport as ssh connection is once more available. self.switchboard.open_all_transports() self.wait_for_bootup_complete(timeout=bootup_timeout) logger.info("{} booted up successfully in {}s.".format( self.name, int(time.time() - start_time))) self._after_boot_hook() deprecation_utils.add_deprecated_attributes( SshDevice, [("send_file_to_device", "file_transfer.send_file_to_device", True), ("recv_file_from_device", "file_transfer.recv_file_from_device", True), ("has_command", "shell_capability.has_command", True)])
Args: serial_port (str): Cambrionix serial port. Returns: str: response. Raises: DeviceError: Device not responding. Note: The command prompt is always this string: ">> ". """ read_data = "" while not read_data.endswith("\n>> "): ready = select.select([serial_port], [], [], 25)[0] if ready: read_data += serial_port.read(serial_port.inWaiting()).decode( "utf-8", "replace") else: raise errors.DeviceError( "Device cambrionix get response failed. " "Read timeout on serial port: {}".format(serial_port)) return read_data.splitlines() deprecation_utils.add_deprecated_attributes( Cambrionix, [("set_mode", "switch_power.set_mode", True), ("power_on", "switch_power.power_on", True), ("power_off", "switch_power.power_off", True)])
Args: command (str): command to issue. regex (str): regular expression with one or more capturing groups. regex_group (int): number of regex group to return. command_name (str): command name to appear in log messages. raise_error (bool): whether or not to raise error if unable to find a match. tries (int): how many times to try executing the command before failing. port (int): which port to send the shell command to. Returns: str: value of the capturing group with index 'regex_group' in the match. Raises: DeviceError: if command execution fails OR couldn't find the requested group in any of the responses. """ return self.command_with_regex(command, regex, self.shell, regex_group=regex_group, raise_error=raise_error, tries=tries, command_name=command_name, port=port) deprecation_utils.add_deprecated_attributes( Yepkit, [("power_on", "switch_power.power_on", True), ("power_off", "switch_power.power_off", True)])
self._hub.switch_power.power_on(self.port_number) # Wait for 'dev/tty' to populate if self._wait_for_connection_func: self._wait_for_connection_func() else: time.sleep(5) switchboard = self._get_switchboard_if_initialized() if switchboard: switchboard.open_all_transports() if self._wait_for_bootup_complete_func: self._wait_for_bootup_complete_func() def _verify_switch_created(self, switch): """Verifies switch is created and has switch_power capability.""" if not (hasattr(switch, "switch_power") and isinstance( switch.switch_power, switch_power_base.SwitchPowerBase)): raise errors.CapabilityNotReadyError( msg="'switch_power' capability is missing in hub device {} ({})," " or is not an instance of SwitchPowerBase".format( self.hub_name, type(switch).__name__), device_name=self._device_name) deprecation_utils.add_deprecated_attributes(CommPowerDefault, [("enable", "on", True), ("disable", "off", True), ("power_cycle", "cycle", True), ("port", "port_number", False), ("address", "hub_name", False)])
Raises: CapabilityNotReadyError: if unable to initiate ethernet_switch. """ if not self.healthy: self.health_check() if not self._ethernet_switch: raise errors.CapabilityNotReadyError( device_name=self._device_name, msg="Not set up for ethernet switching.") self._ethernet_switch.switch_power.power_on(self.ethernet_port_number) def _verify_switch_created(self, switch): """Verifies switch is created and has switch_power capability.""" if not ( hasattr(switch, "switch_power") and isinstance(switch.switch_power, switch_power_base.SwitchPowerBase)): raise errors.CapabilityNotReadyError( msg="'switch_power' capability is missing in hub device {} ({})," " or is not an instance of SwitchPowerBase".format( self.hub_name, type(switch).__name__), device_name=self._device_name) deprecation_utils.add_deprecated_attributes( CommPowerUsbEthernet, [("enable", "ethernet_on", True), ("disable", "ethernet_off", True), ("address", "ethernet_switch_address", False), ("port", "ethernet_port_number", False), ("power_cycle", "cycle", True)])
self._log_object_lifecycle_event("__del__") if hasattr(self, "close"): self.close() deprecation_utils.add_deprecated_attributes( GazooDeviceBase, [("console_port_name", "communication_address", False), ("console_port", "switchboard", False), ("click", "switchboard.click", True), ("click_and_expect", "switchboard.click_and_expect", True), ("close_communication_transports", "switchboard.close_all_transports", True), ("do_and_expect", "switchboard.do_and_expect", True), ("open_communication_transports", "switchboard.open_all_transports", True), ("expect", "switchboard.expect", True), ("press", "switchboard.press", True), ("press_and_expect", "switchboard.press_and_expect", True), ("send", "switchboard.send", True), ("send_and_expect", "switchboard.send_and_expect", True), ("set_max_log_size", "switchboard.set_max_log_size", True), ("release", "switchboard.release", True), ("release_and_expect", "switchboard.release_and_expect", True), ("get_event_label_dict", "event_parser.get_event_label_dict", True), ("get_event_label_list", "event_parser.get_event_labels", True), ("get_last_event", "event_parser.get_last_event", True), ("get_last_event_state", "event_parser.get_last_event_state", True), ("get_unexpected_reboots", "event_parser.get_unexpected_reboots", True), ("get_event_history", "event_parser.get_event_history", True), ("get_event_history_count", "event_parser.get_event_history_count", True), ("wait_for_event_labels", "event_parser.wait_for_event_labels", True)])
else: if close_transports: self._switchboard.close_all_transports() self._hub.switch_power.power_off(self.port_number) @decorators.CapabilityLogDecorator(logger) def on(self, no_wait=False): """Turn on power to the device. Args: no_wait (bool): Return before verifying boot up. """ if not self.healthy: self.health_check() if self._change_triggers_reboot: self._switchboard.add_log_note( f"GDM triggered reboot via {self.hub_type} power change.") self._hub.switch_power.power_on(self.port_number) if not self._change_triggers_reboot: time.sleep(2) # Small delay to give time for 'dev/tty' to populate self._switchboard.open_all_transports() if not no_wait: self._wait_for_bootup_complete_fn() deprecation_utils.add_deprecated_attributes(DevicePowerDefault, [("power_off", "off", True), ("power_on", "on", True), ("power_cycle", "cycle", True)])
else: raise errors.DeviceError("{} failed to become pingable in {}s.".format( self.name, timeout)) output = "Device still offline" while time.time() < max_disconnect_time: try: output, return_code = self.shell( self.commands["TELNET"], include_return_code=True) if return_code == 0: # command executed logger.info("{} online in {}s".format(self.name, int(time.time() - start_time))) return except errors.DeviceError: logger.debug( "{} failed to respond to {!r}.".format(self.name, self.commands["TELNET"]), exc_info=True) time.sleep(.5) raise errors.DeviceError( "Failed to come online and respond to {!r} in {}s. Response: {}".format( self.commands["TELNET"], timeout, output)) deprecation_utils.add_deprecated_attributes( UnifiPoeSwitch, [("do_and_expect", "switchboard.do_and_expect", True), ("expect", "switchboard.expect", True), ("send", "switchboard.send", True), ("send_and_expect", "switchboard.send_and_expect", True)])
def _property_has_setter(self, property_name): """Checks whether a property has a setter. Args: property_name (str): name of the property. If property is a capability property then the string should be <capability_name>.<property_name> Raises: AttributeError: if property doesn't exist Returns: bool: True if property has a setter, else False """ target_class = type(self) # Get the class that holds the property if "." in property_name: capability_name = property_name.split(".")[0] capability_instance = getattr(self, capability_name) target_class = type(capability_instance) property_name = property_name.split(".")[1] # Check whether the property has a setter attr = getattr(target_class, property_name) return attr.fset is not None deprecation_utils.add_deprecated_attributes(AuxiliaryDevice, [ ("console_port_name", "communication_address", False), ])
return self.shell(self.commands["RESET_FAILED"]) except errors.DeviceError: logger.debug( "{} failed to respond to {!r}.".format( self.name, self.commands["BOOTUP_COMPLETE"]), exc_info=True) time.sleep(.5) raise errors.DeviceError( "Failed to come online and respond to {!r} in {}s. Response: {}".format( self.commands["BOOTUP_COMPLETE"], timeout, output)) def _verify_reboot(self): """Verifies reboot actually occurred.""" self._ensure_device_goes_offline() self._ensure_device_is_online() def _list_properties_dynamic_raspbian(self): dyn_list = ["firmware_version", "kernel_version"] return set(dyn_list) deprecation_utils.add_deprecated_attributes( RaspbianDevice, [("send_file_to_device", "file_transfer.send_file_to_device", True), ("recv_file_from_device", "file_transfer.recv_file_from_device", True), ("do_and_expect", "switchboard.do_and_expect", True), ("expect", "switchboard.expect", True), ("send", "switchboard.send", True), ("send_and_expect", "switchboard.send_and_expect", True)])