-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathframework.py
More file actions
45 lines (39 loc) · 1.17 KB
/
framework.py
File metadata and controls
45 lines (39 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import subprocess
import shlex
import json
import click
# create a function that runs suprocess and returns the output
def run_command(command):
cmd = shlex.split(command)
output = subprocess.check_output(cmd)
return output
def run_lsblk(device):
"""
Runs lsblk command and produces JSON output:
lsblk -J -o NAME,SIZE,TYPE,MOUNTPOINT
{
"blockdevices": [
{"name": "vda", "size": "59.6G", "type": "disk", "mountpoint": null,
"children": [
{"name": "vda1", "size": "59.6G", "type": "part", "mountpoint": "/etc/hosts"}
]
}
]
}
"""
command = f'lsblk -J -o NAME,SIZE,TYPE,MOUNTPOINT'
output = run_command(command)
devices = json.loads(output)['blockdevices']
for parent in devices:
if parent['name'] == device:
return parent
for child in parent.get('children', []):
if child['name'] == device:
return child
@click.command()
@click.option('--verbose', '-v', is_flag=True)
@click.argument('device')
def main(device, verbose):
print(f"Device: {device}")
print(f"Verbose: {verbose}")
print(f"{run_lsblk(device)}")