Skip to content

Commit f341a4c

Browse files
committed
modularize
1 parent bf112a2 commit f341a4c

3 files changed

Lines changed: 36 additions & 35 deletions

File tree

‎blkpy/__init__.py‎

Whitespace-only changes.

‎blkpy/main.py‎

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,5 @@
11
import click
2-
import subprocess
3-
import shlex
4-
import json
5-
6-
7-
# create a function that runs suprocess and returns the output
8-
def run_command(command):
9-
cmd = shlex.split(command)
10-
output = subprocess.check_output(cmd)
11-
return output
12-
13-
def run_lsblk(device):
14-
"""
15-
Runs lsblk command and produces JSON output:
16-
17-
lsblk -J -o NAME,SIZE,TYPE,MOUNTPOINT
18-
{
19-
"blockdevices": [
20-
{"name": "vda", "size": "59.6G", "type": "disk", "mountpoint": null,
21-
"children": [
22-
{"name": "vda1", "size": "59.6G", "type": "part", "mountpoint": "/etc/hosts"}
23-
]
24-
}
25-
]
26-
}
27-
"""
28-
command = f'lsblk -J -o NAME,SIZE,TYPE,MOUNTPOINT'
29-
output = run_command(command)
30-
devices = json.loads(output)['blockdevices']
31-
for parent in devices:
32-
if parent['name'] == device:
33-
return parent
34-
for child in parent.get('children', []):
35-
if child['name'] == device:
36-
return child
2+
from blkpy.util import run_lsblk
373

384
@click.command()
395
@click.option('--verbose', '-v', is_flag=True)

‎blkpy/util.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import subprocess
2+
import shlex
3+
import json
4+
5+
# create a function that runs suprocess and returns the output
6+
def run_command(command):
7+
cmd = shlex.split(command)
8+
output = subprocess.check_output(cmd)
9+
return output
10+
11+
def run_lsblk(device):
12+
"""
13+
Runs lsblk command and produces JSON output:
14+
15+
lsblk -J -o NAME,SIZE,TYPE,MOUNTPOINT
16+
{
17+
"blockdevices": [
18+
{"name": "vda", "size": "59.6G", "type": "disk", "mountpoint": null,
19+
"children": [
20+
{"name": "vda1", "size": "59.6G", "type": "part", "mountpoint": "/etc/hosts"}
21+
]
22+
}
23+
]
24+
}
25+
"""
26+
command = f'lsblk -J -o NAME,SIZE,TYPE,MOUNTPOINT'
27+
output = run_command(command)
28+
devices = json.loads(output)['blockdevices']
29+
for parent in devices:
30+
if parent['name'] == device:
31+
return parent
32+
for child in parent.get('children', []):
33+
if child['name'] == device:
34+
return child
35+

0 commit comments

Comments
 (0)