def _parse_mounts(self, content): self.rows = [] self.mounts = {} for line in get_active_lines(content): mount = {} mount['mount_clause'] = line # Get the mounted filesystem by checking the ' on ' line_sp = _customized_split(line, line, sep=' on ') mount['filesystem'] = line_sp[0] # Get the mounted point by checking the last ' type ' before the last '(' mnt_pt_sp = _customized_split(raw=line, l=line_sp[1], sep=' (', reverse=True) line_sp = _customized_split(raw=line, l=mnt_pt_sp[0], sep=' type ', reverse=True) mount['mount_point'] = line_sp[0] mount['mount_type'] = line_sp[1].split()[0] line_sp = _customized_split(raw=line, l=mnt_pt_sp[1], sep=None, check=False) mount['mount_options'] = MountOpts( optlist_to_dict(line_sp[0].strip('()'))) if len(line_sp) == 2: mount['mount_label'] = line_sp[1] entry = MountEntry(mount) self.rows.append(entry) self.mounts[mount['mount_point']] = entry
def parse_content(self, content): self._bond_mode = None self._partner_mac_address = None self._active_slaves = None self.xmit_hash_policy = None self._slave_interface = [] self._aggregator_id = [] for line in get_active_lines(content): if line.startswith("Bonding Mode: "): raw_mode = line.split(":", 1)[1].strip() self._bond_mode = raw_mode if raw_mode in BOND_PREFIX_MAP: self._bond_mode = BOND_PREFIX_MAP[raw_mode] else: raise ParseException( "Unrecognised bonding mode '{b}'".format(b=raw_mode)) elif line.startswith("Partner Mac Address: "): self._partner_mac_address = line.split(":", 1)[1].strip() elif line.startswith("Slave Interface: "): self._slave_interface.append(line.split(":", 1)[1].strip()) elif line.strip().startswith("Aggregator ID: "): self._aggregator_id.append(line.strip().split(':', 1)[1].strip()) elif line.strip().startswith("Transmit Hash Policy"): # No need of values in bracket: # Integer notification (0), (1), (2) of layer2, layer3+4, layer2+3 resp self.xmit_hash_policy = line.split(":", 1)[1].split()[0] elif line.strip().startswith("Currently Active Slave"): self._active_slave = line.split(":", 1)[1].split()[0]
def parse_content(self, content): self.global_lines = [] self.host_lines = {} _content = get_active_lines(content) if not _content: raise SkipException("Empty content.") index_list = [i for i, l in enumerate(_content) if l.startswith('Host ')] index = index_list[0] if index_list else len(_content) for line in _content[:index]: line_splits = [s.strip() for s in line.split(None, 1)] kw, val = line_splits[0], line_splits[1] if len(line_splits) == 2 else '' self.global_lines.append(self.KeyValue(kw, val, line)) hostbit = '' for line in _content[index:]: line_splits = [s.strip() for s in line.split(None, 1)] kw, val = line_splits[0], line_splits[1] if len(line_splits) == 2 else '' if kw == 'Host': hostbit = kw + '_' + val self.host_lines[hostbit] = [] else: self.host_lines[hostbit].append(self.KeyValue(kw, val, line)) if not (self.host_lines or self.global_lines): raise SkipException("Nothing parsed.")
def parse_content(self, content): """ Parse each line in the file ``/etc/fstab``. """ fstab_output = parse_delimited_table([FS_HEADINGS] + get_active_lines(content)) self.data = [] for line in fstab_output: line['fs_spec'] = line.get('fs_spec', '') line['fs_vfstype'] = line.get('fs_vfstype', '') # Decode fs_file to transfer the '\040' to ' '. # Encode first and then decode works for both Python2 and Python3. line['fs_file'] = line.get('fs_file', '').encode().decode("unicode-escape") line['fs_freq'] = int(line.get('fs_freq', '0')) line['fs_passno'] = int(line.get('fs_passno', '0')) # if there is no mntops, it is defaults. # (/dev/foo /foo somefs defaults 0 0) and (/dev/foo /foo somefs) are same line['raw_fs_mntops'] = line.get('raw_fs_mntops', 'defaults') # optlist_to_dict converts 'key=value' to key: value and 'key' to key: True line['fs_mntops'] = MountOpts( optlist_to_dict(line.get('raw_fs_mntops'))) # add `raw` here for displaying convenience on front-end line['raw'] = [ l for l in content if l.strip().startswith(line['fs_spec']) ][0] self.data.append(FSTabEntry(line)) # assert: all mount points of valid entries are unique by definition self.mounted_on = dict((row.fs_file, row) for row in self.data)
def parse_content(self, content): # Use all the defined scanners to search the log file, setting the # properties defined in the scanner. self.lines = [l for l in content if len(l) > 0 and l[0].isdigit()] for scanner in self.scanners: scanner(self) # Parse kernel driver lines self.data = {} slot = None slot_re = re.compile(r'^[0-9a-f]+:[0-9a-f]+.[0-9a-f]+') fields = ["Subsystem", "Kernel driver in use", "Kernel modules"] for line in get_active_lines(content): parts = line.split() if slot_re.match(parts[0]): slot = parts[0] device_details = line.split(None, 1)[-1] # keep the raw line self.data[slot] = { 'Slot': slot, 'Dev_Details': device_details.lstrip() } elif slot and (line.split(":")[0].strip() in fields): parts = line.split(':') self.data[slot][parts[0]] = parts[1].lstrip()
def parse_content(self, content): # No content found or file is empty if not content: raise SkipException("Empty file") data = {} self.raw = [] key, value = "", "" for line in get_active_lines(content): self.raw.append(line.strip()) if "=" in line: key, value = line.split("=", 1) elif key: if value.endswith("'") or value.endswith("\""): data[key] = value[1:-1].split() key, value = "", "" value = value + " " + line # No useful parsed data if not data: raise SkipException( "Input content is not empty but there is no useful parsed data." ) self.update(data)
def parse_content(self, content): if not content: raise SkipException('Nothing to parse.') self._netns_list = [] for line in get_active_lines(content): self._netns_list.extend(line.split())
def parse_content(self, content): up2date_info = {} for line in get_active_lines(content): if "[comment]" not in line and '=' in line: key, val = line.split('=') up2date_info[key.strip()] = val.strip() self.data = up2date_info
def _parse_mounts(self, content): self.rows = [] self.mounts = {} for line in get_active_lines(content): mount = {} mount['mount_clause'] = line # Handle the '\040' in `mount_point`, e.g. "VMware\040Tools" line_sp = line.encode().decode("unicode-escape") line_sp = _customized_split(raw=line, l=line_sp) mount['filesystem'] = mount['mounted_device'] = line_sp[0] line_sp = _customized_split(raw=line, l=line_sp[1], num=3, reverse=True) mount['mount_label'] = line_sp[-2:] line_sp = _customized_split(raw=line, l=line_sp[0], reverse=True) mount['mount_options'] = MountOpts(optlist_to_dict(line_sp[1])) line_sp = _customized_split(raw=line, l=line_sp[0], reverse=True) mount['mount_type'] = mount['filesystem_type'] = line_sp[1] mount['mount_point'] = line_sp[0] entry = MountEntry(mount) self.rows.append(entry) self.mounts[mount['mount_point']] = entry
def parse_content(self, content): lines = get_active_lines(content) if len(lines) != 1: raise ParseException("Unexpected file content") try: self.runtime_us = int(lines[0]) except: raise ParseException("Unexpected file content")
def parse_content(self, content): """ Parse grub configuration file to create a dict with this structure:: { "configs": [ (name, value), (name, value) ...], "title": [ [(title_name, name), (cmd, opt), (cmd, opt) ...], [(title_name, another_name), ...] ], "menuentry": [ [(menuentry_name, its name), (cmd, opt), (cmd, opt) ...], [(menuentry_name, another_name), ...] ], } """ line_iter = iter(get_active_lines(content)) conf = {"configs": [], "title": [], "menuentry": []} line = None while (True): try: if line is None: line = next(line_iter) if line.startswith('title '): last_line = _parse_title(line_iter, line, conf) line = last_line elif line.startswith('menuentry '): _parse_menu_entry(line_iter, line, conf) line = None else: conf["configs"].append(_parse_config(line)) line = None except StopIteration: self.data = conf break if not self.data.get('title'): self.data.pop('title') if not self.data.get('menuentry'): self.data.pop('menuentry') if not self.data.get('configs'): self.data.pop('configs') for line_full in self.data.get('title', []) + self.data.get( 'menuentry', []): for name, line in line_full: if name == 'menuentry_name' or name == 'title_name': entry = {} entry['name'] = line elif entry and name.startswith(('kernel', 'linux')): entry['cmdline'] = line self._boot_entries.append(BootEntry(entry))
def parse_content(self, content): self._bond_mode = None self._partner_mac_address = None self._active_slave = None self.xmit_hash_policy = None self._arp_polling_interval = None self._arp_ip_target = None self._slave_interface = [] self._aggregator_id = [] self._mii_status = [] self._slave_link_failure_count = [] self._slave_speed = [] self._slave_duplex = [] self._primary_slave = None for line in get_active_lines(content): if line.startswith("Bonding Mode: "): raw_mode = line.split(":", 1)[1].strip() self._bond_mode = raw_mode if raw_mode in BOND_PREFIX_MAP: self._bond_mode = BOND_PREFIX_MAP[raw_mode] else: raise ParseException( "Unrecognised bonding mode '{b}'".format(b=raw_mode)) elif line.startswith("Partner Mac Address: "): self._partner_mac_address = line.split(":", 1)[1].strip() elif line.startswith("Slave Interface: "): self._slave_interface.append(line.split(":", 1)[1].strip()) elif line.strip().startswith("Aggregator ID: "): self._aggregator_id.append(line.strip().split(':', 1)[1].strip()) elif line.strip().startswith("Transmit Hash Policy"): # No need of values in bracket: # Integer notification (0), (1), (2) of layer2, layer3+4, layer2+3 resp self.xmit_hash_policy = line.split(":", 1)[1].split()[0] elif line.strip().startswith("Currently Active Slave"): self._active_slave = line.split(":", 1)[1].split()[0] elif line.strip().startswith("MII Status: "): self._mii_status.append(line.strip().split(':', 1)[1].strip()) elif line.strip().startswith("Link Failure Count: "): self._slave_link_failure_count.append(line.strip().split( ':', 1)[1].strip()) elif line.strip().startswith("Speed: "): self._slave_speed.append(line.strip().split(':', 1)[1].strip()) elif line.strip().startswith("Duplex: "): self._slave_duplex.append(line.strip().split(':', 1)[1].strip()) elif line.strip().startswith("ARP Polling Interval (ms):"): self._arp_polling_interval = line.strip().split(':', 1)[1].strip() elif line.strip().startswith("ARP IP target/s (n.n.n.n form):"): self._arp_ip_target = line.strip().split(':', 1)[1].strip() elif line.strip().startswith("Primary Slave"): self._primary_slave = line.split(":", 1)[1].strip()
def parse_content(self, content): # parse the output of `ipcs -s -i ##` command pids = set() self._semid = None for line in get_active_lines(content): line = line.strip() if line.startswith('Semaphore'): self._semid = line.split('=')[-1] elif self._semid and line[0].isdigit(): pids.add(line.split()[-1]) self._pids = sorted(list(pids))
def parse_content(self, content): self.hosts = [] for line in get_active_lines(content): # remove inline comments line = line.partition("#")[0].strip() # break the line into parts parts = line.split() ip, host = parts[:2] aliases = parts[2:] self.hosts.append(HostParser.Host(ip, host, aliases))
def parse_content(self, content): """ Method to parse the contents of file ``/etc/hosts`` This method must be implemented by each parser. Arguments: content (list): List of strings that are the contents of the /etc/hosts file. """ self.hosts = [] for line in get_active_lines(content): # remove inline comments line = line.partition("#")[0].strip() # break the line into parts parts = line.split() ip, host = parts[:2] aliases = parts[2:] self.hosts.append(HostParser.Host(ip, host, aliases))
def parse_content(self, content): if not content: raise SkipException("Empty output.") data = dict() for line in get_active_lines(content): if "=" not in line: continue key, value = line.split("=", 1) # Some keys can have empty values, so just skip them. if not value: continue data[key] = value if not data: raise SkipException("No parsed data.") self.update(data)
def parse_content(self, content): if not content: raise SkipException() data = {} per_device = {} current_dev = "" for line in get_active_lines(content): if (not ("not found" in line or "Error" in line or "No such file" in line or "Warning" in line)): if len(line.split(": ")) >= 2: key, val = line.split(": ") if "IP" in key: proto = re.sub(r'\[|\]', r'', key.split('.')[1]) key = key.split('.')[0] + "_" + proto else: key = key.split('.')[1] val = re.sub(r'\d+\s|\(|\)', r'', val.strip()) # Device configuration details starts here if key == "DEVICE" and not current_dev: current_dev = val continue elif key == "DEVICE" and current_dev: data[current_dev] = per_device current_dev = val per_device = {} continue per_device.update({key: val}) if current_dev and per_device: # Last device configuration details data[current_dev] = per_device if not data: raise SkipException() self.update(data) self._con_dev = [ k for k, v in data.items() if 'STATE' in v and v['STATE'] == 'connected' ]
def parse_content(self, content): """ Details of all the bridges are extracted and stored in a list as dictionary elements. Each dictionary element contains the information of a specific bridge. """ # No content found or file is empty if not content: raise SkipException("Empty file") self.data = [] bridge_details = {} for line in get_active_lines(content): key, value = [i.strip() for i in line.split(":", 1)] parsed_value = value.strip('"') if value.startswith("{") and value.endswith("}"): parsed_value = {} value = value.strip("{}") if value: parsed_value = optlist_to_dict(value, opt_sep=", ", strip_quotes=True) elif value.startswith("[") and value.endswith("]"): parsed_value = [] value = value.strip("[]") if value: parsed_value = [ i.strip(' \t\"\'') for i in value.split(",") ] if key not in bridge_details: bridge_details[key] = parsed_value else: # A new bridge comes self.data.append(bridge_details) bridge_details = {key: parsed_value} # Add the last bridge self.data.append(bridge_details)
def parse_content(self, content, allow_no_value=True): content = get_active_lines(content) super(TunedConfIni, self).parse_content(content, allow_no_value)
def parse_content(self, content): self.lines = [] for line in get_active_lines(content): kw, val = (w.strip() for w in line.split(None, 1)) self.lines.append(self.KeyValue(kw, val, kw.lower())) self.keywords = set([k.kw_lower for k in self.lines])
def parse_content(self, content): """ Parse grub configuration file to create a dict with this structure:: { "configs": { name1: [val1, val2, ...] name2: [val], ... }, "title": [ {title: name1, kernel: [val], ...}, {title: name2, module: [val1, val2], ...}, ], "menuentry": [ {menuentry: name1, insmod: [val1, val2], ...}, {menuentry: name2, linux16: [val], ...}, ], } """ def _skip_script(line): # Skip the script in Grub2 if line.startswith('if'): _skip_script.if_idx += 1 elif line.startswith('fi'): _skip_script.if_idx -= 1 return _skip_script.if_idx def _parser_line(line): sp = [i.strip() for i in line.split(None, 1)] val = sp[1] if len(sp) > 1 else '' if sp[0] not in entry: entry[sp[0]] = [] entry[sp[0]].append(val) # Handle the cmdline if sp[0].startswith(('kernel', 'linux')): b_entry['cmdline'] = val def _parser_entry_line(line): if line.startswith('title '): name = line.split('title', 1)[1].strip() b_entry['name'] = entry['title'] = name else: sp = line.split('menuentry', 1)[1].split('{', 1) name = sp[0].strip() if not name: raise ParseException( "Cannot parse menuentry line: {0}".format(line_raw)) b_entry['name'] = entry['menuentry'] = name # More things after the { if len(sp) > 1 and sp[1]: _parser_line(sp[1]) self.configs = {} self._boot_entries = [] self.entries = [] b_entry = {} entry = {} _skip_script.if_idx = 0 for line_raw in get_active_lines(content): # Handle of lines { # Remove the heading and trailing whitespace and curly brackets line = line_raw.strip('{} \t') if_idx = _skip_script(line) if if_idx == 0 and line and not line.startswith('fi'): # Handle the title / menuentry { if line.startswith(('title ', 'menuentry')): self.entries.append(entry) if entry else None self._boot_entries.append( BootEntry(b_entry)) if b_entry else None b_entry = {} entry = {} _parser_entry_line(line) # } End of title / menuentry handling # Lines inside of an entry elif entry: _parser_line(line) # Lines outside of entries else: sep = '=' if '=' in line else None sp = [i.strip() for i in line.split(sep, 1)] if sp[0] not in self.configs: self.configs[sp[0]] = [] self.configs[sp[0]].append(sp[1] if len(sp) > 1 else '') # } End of outside of entry # } End of lines handling # Store the last entry self.entries.append(entry) if entry else None self.boot_entries.append(BootEntry(b_entry)) if b_entry else None self.update({'configs': self.configs}) if self.configs else None self._is_kdump_iommu_enabled_ = self._is_kdump_iommu_enabled() self._kernel_initrds = get_kernel_initrds(self.entries)
def parse_content(self, content): for line in get_active_lines(content): self.data[self.scsi_host] = [ mode.strip() for mode in line.split(',') ]
def parse_content(self, content): self.features = [] for line in get_active_lines(content): self.features.extend(line.split())
def _check_content(self, content): self._content = get_active_lines(content, '=======') if len(self._content) <= 1: raise ParseException( "Input content is empty or there is no useful parsed data.")
def parse_content(self, content): for line in get_active_lines(content): self.data[self.scsi_host] = line