def get_drive_list():
"""
Gets a list of drives and metadata by parsing the output of `mount`, and adding additional info from various commands.
Disk size/usage comes from shutil.disk_usage or os.statvfs, and name/type info from dbus (Linux) or diskutil (OSX).
"""
if sys.platform == "darwin":
MOUNT_PARSER = OSX_MOUNT_PARSER
else:
MOUNT_PARSER = LINUX_MOUNT_PARSER
try:
drivelist = subprocess.Popen('mount', shell=True, stdout=subprocess.PIPE)
drivelisto, err = drivelist.communicate()
except OSError: # couldn't run `mount`, let's try reading the /etc/mounts listing directly
with open("/proc/mounts") as f:
drivelisto = f.read()
MOUNT_PARSER = RAW_MOUNT_PARSER
drives = []
for drivematch in MOUNT_PARSER.finditer(drivelisto.decode()):
drive = drivematch.groupdict()
path = drive["path"].replace("\\040", " ").replace("\\011", "\t").replace("\\012", "\n").replace("\\134", "\\")
# skip the drive if the filesystem or path is in a blacklist
if drive["filesystem"] in FILESYSTEM_BLACKLIST or any(path.startswith(p) for p in PATH_PREFIX_BLACKLIST):
logger.debug("Skipping blacklisted drive '{}'".format(path))
continue
# skip if we don't have read access to the drive
if not os.access(path, os.R_OK):
continue
# attempt to get some additional metadata about the drive
usage = _get_drive_usage(path)
dbus_drive_info = _try_to_get_drive_info_from_dbus(drive["device"])
diskutil_info = _try_to_get_drive_info_from_diskutil(drive["device"])
# combine the various metadata sources to construct the overall drive metadata
drives.append({
"path": path,
"name": dbus_drive_info.get("name") or diskutil_info.get("name") or path,
"filesystem": drive["filesystem"],
"freespace": usage["free"],
"totalspace": usage["total"],
"drivetype": dbus_drive_info.get("drivetype") or diskutil_info.get("drivetype") or "",
"guid": dbus_drive_info.get("guid") or diskutil_info.get("guid") or drive["device"],
})
return drives
评论列表
文章目录