Skip to content

Commit

Permalink
Add structured parsing to lsblk
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrivastavv committed Nov 26, 2021
1 parent ab414c5 commit 9564d60
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lisa/tools/lsblk.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import re
from dataclasses import dataclass
from typing import List

from lisa.executable import Tool
from lisa.util import find_patterns_in_lines


@dataclass
class PartitionInfo(Tool):
name: str = ""
size: int = 0
type: str = ""
mountpoint: str = ""

def __init__(self, name: str, size: int, type: str, mountpoint: str):
self.name = name
self.size = size
self.type = type
self.mountpoint = mountpoint


class Lsblk(Tool):
# NAME="loop2" SIZE="34017280" TYPE="loop" MOUNTPOINT="/snap/snapd/13640"
_LSBLK_ENTRY_REGEX = re.compile(
r'NAME="(?P<name>\S+)"\s+SIZE="(?P<size>\d+)"\s+'
r'TYPE="(?P<type>\S+)"\s+MOUNTPOINT="(?P<mountpoint>\S+)"'
)

@property
def command(self) -> str:
return "lsblk"

def get_partition_information(self) -> List[PartitionInfo]:
# parse output of lsblk
output = self.run("-b -P -o NAME,SIZE,TYPE,MOUNTPOINT", sudo=True).stdout
partition_info = []
lsblk_entries = find_patterns_in_lines(output, [self._LSBLK_ENTRY_REGEX])[0]
for lsblk_entry in lsblk_entries:
partition_info.append(
PartitionInfo(
name=lsblk_entry[0],
size=int(lsblk_entry[1]),
type=lsblk_entry[2],
mountpoint=lsblk_entry[3],
)
)
return partition_info

0 comments on commit 9564d60

Please sign in to comment.