-
-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
088d12d
commit cb97422
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from collections import OrderedDict | ||
|
||
import pyudev | ||
import psutil | ||
|
||
|
||
IGNORE_DISKS = "sr", "md", "dm-", "loop", "zd", "pmem" | ||
|
||
|
||
def disks_infos(): | ||
context = pyudev.Context() | ||
partitions = {it.device: it for it in psutil.disk_partitions()} | ||
|
||
def _filter_device(dev): | ||
return dev.parent is not None and not any( | ||
map(dev.sys_name.startswith, IGNORE_DISKS) | ||
) | ||
|
||
def _disk_map(dev): | ||
attrs = { | ||
"devname": dev.device_node, | ||
"model": dev.get("ID_MODEL", "<unknown model>"), | ||
"serial": dev.get("ID_SERIAL_SHORT", "<unknown serial>"), | ||
} | ||
|
||
try: | ||
attrs["size"] = dev.attributes.asint("size") | ||
except (AttributeError, UnicodeError, ValueError): | ||
attrs["size"] = "<unknown size>" | ||
|
||
attrs["links"] = list(sorted(it for it in dev.device_links)) | ||
|
||
for child_dev in sorted( | ||
filter(_filter_device, dev.children), key=lambda it: it.device_node | ||
): | ||
attrs.setdefault("partitions", {}) | ||
if child_dev.device_node in partitions: | ||
attrs["partitions"][child_dev.sys_name] = { | ||
"devname": child_dev.device_node, | ||
"filesystem": partitions[child_dev.device_node].fstype, | ||
"mountpoint": partitions[child_dev.device_node].mountpoint, | ||
} | ||
|
||
if "partitions" not in attrs: | ||
attrs["partitions"] = "<no partition>" | ||
|
||
return dev.sys_name, attrs | ||
|
||
return OrderedDict( | ||
sorted( | ||
( | ||
_disk_map(it) | ||
for it in filter( | ||
_filter_device, | ||
context.list_devices(subsystem="block", DEVTYPE="disk"), | ||
) | ||
), | ||
key=lambda d: d[0], | ||
) | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
import json | ||
|
||
print(json.dumps(disks_infos(), indent=4)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def storage_disks_infos(): | ||
from yunohost.disks import disks_infos | ||
|
||
return disks_infos() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def setup_function(function): | ||
... | ||
|
||
def teardown_function(function): | ||
... | ||
|
||
def test_storage_disks_infos(): | ||
... |