Beispiel #1
0
class Ls(GenericUnixCommand):
    _re_files_list = re.compile(r"\S{2,}")
    _re_total = re.compile(r"total\s+(\d+\S*)")
    _re_long = re.compile(
        r"([\w-]{10})\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*\S)\s+(\S+)\s*$")
    _re_long_links = re.compile(
        r"([\w-]{10})\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*\S)\s+(\S+)\s+->\s+(\S+)\s*$"
    )

    def __init__(self,
                 connection,
                 prompt=None,
                 newline_chars=None,
                 options=None,
                 runner=None):
        super(Ls, self).__init__(connection=connection,
                                 prompt=prompt,
                                 newline_chars=newline_chars,
                                 runner=runner)
        self._converter_helper = ConverterHelper()
        # Parameters defined by calling the command
        self.options = options
        self.matched = 0

    def build_command_string(self):
        cmd = "ls"
        if self.options:
            cmd = cmd + " " + self.options
        return cmd

    def on_new_line(self, line, is_full_line):
        if not is_full_line:
            return super(Ls, self).on_new_line(line, is_full_line)
        if self._regex_helper.search_compiled(Ls._re_total, line):
            if "total" not in self.current_ret:
                self.current_ret["total"] = dict()
            self.current_ret["total"]["raw"] = self._regex_helper.group(1)
            self.current_ret["total"][
                "bytes"] = self._converter_helper.to_bytes(
                    self._regex_helper.group(1))[0]
        elif self._regex_helper.search_compiled(Ls._re_long_links, line):
            self._add_new_file_long(True)
        elif self._regex_helper.search_compiled(Ls._re_long, line):
            self._add_new_file_long(False)
        elif self._regex_helper.search_compiled(Ls._re_files_list, line):
            files = line.split()
            if "files" not in self.current_ret:
                self.current_ret["files"] = dict()
            for filename in files:
                self.current_ret["files"][filename] = dict()
                self.current_ret["files"][filename]["name"] = filename
        return super(Ls, self).on_new_line(line, is_full_line)

    def _add_new_file_long(self, islink):
        filename = self._regex_helper.group(7)
        if "files" not in self.current_ret:
            self.current_ret["files"] = dict()
        self.current_ret["files"][filename] = dict()
        self.current_ret["files"][filename][
            "permissions"] = self._regex_helper.group(1)
        self.current_ret["files"][filename]["hard_links_count"] = int(
            self._regex_helper.group(2))
        self.current_ret["files"][filename][
            "owner"] = self._regex_helper.group(3)
        self.current_ret["files"][filename][
            "group"] = self._regex_helper.group(4)
        self.current_ret["files"][filename][
            "size_raw"] = self._regex_helper.group(5)
        self.current_ret["files"][filename]["size_bytes"] = \
            self._converter_helper.to_bytes(self._regex_helper.group(5))[0]
        self.current_ret["files"][filename]["date"] = self._regex_helper.group(
            6)
        self.current_ret["files"][filename]["name"] = self._regex_helper.group(
            7)
        if islink:
            self.current_ret["files"][filename][
                "link"] = self._regex_helper.group(8)

    def _get_types(self, requested_type):
        if not self.done():
            raise ResultNotAvailableYet("Command not executed already")
        requested_type = requested_type.lower()
        ret = dict()
        result = self.result()
        if 'files' in result:
            for file_name in result["files"]:
                file_dict = result["files"][file_name]
                permissions = file_dict["permissions"]
                current_type = permissions[0]
                if requested_type == current_type:
                    ret[file_name] = file_dict
        return ret

    def get_dirs(self):
        return self._get_types('d')

    def get_links(self):
        return self._get_types('l')

    def get_files(self):
        return self._get_types('-')
Beispiel #2
0
class Iperf(GenericUnixCommand):
    def __init__(self,
                 connection,
                 options,
                 prompt=None,
                 newline_chars=None,
                 runner=None):
        super(Iperf, self).__init__(connection=connection,
                                    prompt=prompt,
                                    newline_chars=newline_chars,
                                    runner=runner)
        self.options = options
        self.current_ret['CONNECTIONS'] = dict()
        self.current_ret['INFO'] = list()

        # private values
        self._connection_dict = dict()
        self._list_of_connections = dict()
        self._converter_helper = ConverterHelper()

    def build_command_string(self):
        cmd = 'iperf ' + str(self.options)
        return cmd

    def on_new_line(self, line, is_full_line):
        if is_full_line:
            try:
                self._command_failure(line)
                self._parse_connection_name_and_id(line)
                self._parse_headers(line)
                self._parse_connection_info(line)
                self._parse_connection_headers(line)
            except ParsingDone:
                pass
        return super(Iperf, self).on_new_line(line, is_full_line)

    _re_command_failure = re.compile(
        r"(?P<FAILURE_MSG>.*failed.*|.*error.*|.*command not found.*|.*iperf:.*)"
    )

    def _command_failure(self, line):
        if self._regex_helper.search_compiled(Iperf._re_command_failure, line):
            self.set_exception(
                CommandFailure(
                    self, "ERROR: {}".format(
                        self._regex_helper.group("FAILURE_MSG"))))
            raise ParsingDone

    _re_connection_name_and_id = re.compile(
        r"(?P<ID>\[\s*\d*\])\s*(?P<ID_NAME>.*port\s*\d*\s*connected with.*)")

    def _parse_connection_name_and_id(self, line):
        if self._regex_helper.search_compiled(Iperf._re_connection_name_and_id,
                                              line):
            connection_id = self._regex_helper.group("ID")
            connection_name = self._regex_helper.group("ID_NAME")
            connection_dict = {connection_id: connection_name}
            self._connection_dict.update(connection_dict)
            raise ParsingDone

    _re_headers = re.compile(r"(?P<HEADERS>\[\s+ID\].*)")

    def _parse_headers(self, line):
        if self._regex_helper.search_compiled(Iperf._re_headers, line):
            matched = line.split()[2:]
            self._list_of_connections = [header.strip() for header in matched]
            raise ParsingDone

    _re_connection_info = re.compile(
        r"(?P<CONNECTION_ID>\[\s*\d*\])\s*(?P<CONNECTION_REPORT>.*)")

    def _parse_connection_info(self, line):
        if self._regex_helper.search_compiled(Iperf._re_connection_info, line):
            connection_id = self._regex_helper.group("CONNECTION_ID")
            connection_report = self._regex_helper.group(
                "CONNECTION_REPORT").split('  ')
            connection_report = [
                report.strip() for report in connection_report
            ]
            connection_name = self._connection_dict[connection_id]
            info_dict = dict(zip(self._list_of_connections, connection_report))
            self._normalise_units(connection_report, info_dict)
            self._update_current_ret(connection_name, info_dict)
            raise ParsingDone

    def _update_current_ret(self, connection_name, info_dict):
        if connection_name in self.current_ret['CONNECTIONS']:
            self.current_ret['CONNECTIONS'][connection_name].append(info_dict)
        else:
            connection_dict = {connection_name: [info_dict]}
            self.current_ret['CONNECTIONS'].update(connection_dict)

    _re_ornaments = re.compile(r"(?P<ORNAMENTS>----*|\[\s*ID\].*)",
                               re.IGNORECASE)

    def _parse_connection_headers(self, line):
        if not self._regex_helper.search_compiled(Iperf._re_ornaments, line):
            self.current_ret['INFO'].append(line.strip())
            raise ParsingDone

    def _normalise_units(self, report, dictionary_to_update):
        for (index, item) in enumerate(report):
            if 'Bytes' in item or 'bits' in item:
                header = self._list_of_connections[index]
                raw_bites = self._converter_helper.to_bytes(item)[0]
                new_column_title = header + " Raw"
                read_bites = dictionary_to_update[header]
                dictionary_to_update[header] = raw_bites
                dictionary_to_update.update({new_column_title: read_bites})
Beispiel #3
0
class Du(GenericUnixCommand):
    """Unix command du."""
    def __init__(self,
                 connection,
                 options=None,
                 prompt=None,
                 newline_chars=None,
                 runner=None):
        """
        Unix command du.

        :param connection: moler connection to device, terminal when command is executed
        :param options: Options of unix du command
        :param prompt: expected prompt sending by device after command execution
        :param newline_chars: Characters to split lines
        :param runner: Runner to run command
        """
        super(Du, self).__init__(connection=connection,
                                 prompt=prompt,
                                 newline_chars=newline_chars,
                                 runner=runner)
        self.options = options
        self._converter_helper = ConverterHelper()

    def build_command_string(self):
        """
        Build command string from parameters passed to object.

        :return: String representation of command to send over connection to device.
        """
        cmd = "du"
        if self.options:
            cmd = '{} {}'.format(cmd, self.options)
        return cmd

    def on_new_line(self, line, is_full_line):
        """
        Put your parsing code here.

        :param line: Line to process, can be only part of line. New line chars are removed from line.
        :param is_full_line: True if line had new line chars, False otherwise
        :return: Nothing
        """
        if is_full_line:
            try:
                self._parse_du(line)
                self._parse_du_bytes(line)
            except ParsingDone:
                pass
        return super(Du, self).on_new_line(line, is_full_line)

    # 4       ./directory/directory2
    _re_du = re.compile(r"(?P<NUMBER>^\d+)\s+(?P<DIRECTORY>\S*$)")

    def _parse_du(self, line):
        if self._regex_helper.search_compiled(Du._re_du, line):
            self.current_ret[self._regex_helper.group('DIRECTORY')] = dict()
            self.current_ret[self._regex_helper.group(
                'DIRECTORY')]["size_bytes"] = 1024 * int(
                    self._regex_helper.group('NUMBER'))
            self.current_ret[self._regex_helper.group(
                'DIRECTORY')]["size_raw"] = self._regex_helper.group('NUMBER')
            raise ParsingDone

    # 4.0K    ./directory2/directory3
    _re_du_bytes = re.compile(
        r"(?P<NUMBER>^\d+\.?\d*\w)\s+(?P<DIRECTORY>\S*$)")

    def _parse_du_bytes(self, line):
        if self._regex_helper.search_compiled(Du._re_du_bytes, line):
            self.current_ret[self._regex_helper.group('DIRECTORY')] = dict()
            self.current_ret[self._regex_helper.group('DIRECTORY')]["size_bytes"] = \
                self._converter_helper.to_bytes(self._regex_helper.group('NUMBER'))[0]
            self.current_ret[self._regex_helper.group(
                'DIRECTORY')]["size_raw"] = self._regex_helper.group('NUMBER')
            raise ParsingDone
Beispiel #4
0
class Ls(GenericUnixCommand):
    """Unix command ls"""

    _re_files_list = re.compile(r"\S{2,}")
    _re_total = re.compile(r"total\s+(\d+\S*)")
    _re_long = re.compile(
        r"([\w-]{10}[\.\+]?)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*\S)\s+(\S+)\s*$"
    )
    _re_long_links = re.compile(
        r"([\w-]{10}[\.\+]?)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*\S)\s+(\S+)\s+->\s+(\S+)\s*$"
    )

    def __init__(self,
                 connection,
                 prompt=None,
                 newline_chars=None,
                 options=None,
                 path=None,
                 runner=None):
        """
        Unix command ls

        :param connection: Moler connection to device, terminal when command is executed.
        :param prompt: prompt (on system where command runs).
        :param newline_chars: Characters to split lines - list.
        :param options: Options of unix ls command
        :param path: path to list
        :param runner: Runner to run command.
        """
        super(Ls, self).__init__(connection=connection,
                                 prompt=prompt,
                                 newline_chars=newline_chars,
                                 runner=runner)
        self._converter_helper = ConverterHelper()
        self.current_ret["files"] = dict()
        # Parameters defined by calling the command
        self.options = options
        self.path = path

    def get_dirs(self):
        """
        Returns only directories (folders) from command output

        :return: Dict, key is item, value is parsed information about item
        """
        return self._get_types('d')

    def get_links(self):
        """
        Returns only links from command output

        :return: Dict, key is item, value is parsed information about item
        """
        return self._get_types('l')

    def get_files(self):
        """
        Returns only files from command output

        :return: Dict, key is item, value is parsed information about item
        """
        return self._get_types('-')

    def build_command_string(self):
        """
        Builds command string from parameters passed to object.

        :return: String representation of command to send over connection to device.
        """
        cmd = "ls"
        if self.options:
            cmd = "{} {}".format(cmd, self.options)
        if self.path:
            cmd = "{} {}".format(cmd, self.path)
        return cmd

    def on_new_line(self, line, is_full_line):
        """
        Processes line from output form connection/device.

        :param line: Line to process, can be only part of line. New line chars are removed from line.
        :param is_full_line: True if line had new line chars, False otherwise
        :return: None
        """
        if is_full_line:
            try:
                self._parse_total(line)
                self._parse_long_links(line)
                self._parse_long_file(line)
                self._parse_files_list(line)
            except ParsingDone:
                pass
        return super(Ls, self).on_new_line(line, is_full_line)

    def _parse_files_list(self, line):
        """
        Parses list of files.

        :param line: Line from device.
        :return: None but raises ParsingDone if matches success.
        """
        if self._regex_helper.search_compiled(Ls._re_files_list, line):
            files = line.split()
            for filename in files:
                self.current_ret["files"][filename] = dict()
                self.current_ret["files"][filename]["name"] = filename
            raise ParsingDone()

    def _parse_long_file(self, line):
        """
        Parses line with long information with file or directory.

        :param line: Line from device.
        :return: None but raises ParsingDone if matches success.
        """
        if self._regex_helper.search_compiled(Ls._re_long, line):
            self._add_new_file_long(False)
            raise ParsingDone()

    def _parse_long_links(self, line):
        """
        Parses line with long information with link.

        :param line: Line from device.
        :return: None but raises ParsingDone if matches success.
        """
        if self._regex_helper.search_compiled(Ls._re_long_links, line):
            self._add_new_file_long(True)
            raise ParsingDone()

    def _parse_total(self, line):
        """
        Parses information about total in ls output.

        :param line: Line from device.
        :return: None but raises ParsingDone if matches success.
        """
        if self._regex_helper.search_compiled(Ls._re_total, line):
            if "total" not in self.current_ret:
                self.current_ret["total"] = dict()
            self.current_ret["total"]["raw"] = self._regex_helper.group(1)
            self.current_ret["total"][
                "bytes"] = self._converter_helper.to_bytes(
                    self._regex_helper.group(1))[0]
            raise ParsingDone()

    def _add_new_file_long(self, islink):
        """
        Adds parsed output to command ret.

        :param islink: True if parsed output is link or False otherwise.
        :return: None.
        """
        filename = self._regex_helper.group(7)
        self.current_ret["files"][filename] = dict()
        self.current_ret["files"][filename][
            "permissions"] = self._regex_helper.group(1)
        self.current_ret["files"][filename]["hard_links_count"] = int(
            self._regex_helper.group(2))
        self.current_ret["files"][filename][
            "owner"] = self._regex_helper.group(3)
        self.current_ret["files"][filename][
            "group"] = self._regex_helper.group(4)
        self.current_ret["files"][filename][
            "size_raw"] = self._regex_helper.group(5)
        self.current_ret["files"][filename]["size_bytes"] = \
            self._converter_helper.to_bytes(self._regex_helper.group(5))[0]
        self.current_ret["files"][filename]["date"] = self._regex_helper.group(
            6)
        self.current_ret["files"][filename]["name"] = self._regex_helper.group(
            7)
        if islink:
            self.current_ret["files"][filename][
                "link"] = self._regex_helper.group(8)

    def _get_types(self, requested_type):
        """
        Method to return only object of specific type.
        :param requested_type: Type of object. Available values: 'd', 'l', '-'
        :return: Dict of files
        """
        if not self.done():
            raise ResultNotAvailableYet("Command not executed already")
        requested_type = requested_type.lower()
        ret = dict()
        result = self.result()
        if 'files' in result:
            for file_name in result["files"]:
                file_dict = result["files"][file_name]
                permissions = file_dict["permissions"]
                current_type = permissions[0]
                if requested_type == current_type:
                    ret[file_name] = file_dict
        return ret