SPRKLstats/diskusage.py

43 lines
1.1 KiB
Python

# Author: Stefan Weijers https://github.com/Stxfie
import os
from config import bcolors,env
barWidth = 50
# Function to generate usage bar based on percentage of storage in use
def generateBar(percentage):
no_used = int((percentage/100)*barWidth)
bar = '['
if no_used != 0 and no_used != barWidth:
bar = bar + bcolors.EMPTY
elif no_used == barWidth:
bar = bar + bcolors.FULL
bar = bar + '='*no_used
bar = bar + bcolors.UNUSED + '='*(barWidth - no_used)
return bar + bcolors.RESET + ']'
cmd = 'df -h '
# Generate command to be executed
for mount in env.mountpoints:
cmd = cmd + mount + ' '
du = os.popen(cmd).read() # Execute command on system
# Get percentages from each mounting point in order
percinfo = du.split()[11::6]
percs = []
for perc in percinfo:
percs.append(int(perc.split('%')[0]))
# Get full information from df and split it for fancy output
full = du.split('\n')
full = full[:-1] # Get rid of trailing empty string
# Output disk usage along with bar to indicate usage
print(full[0])
for i in range(1, len(full)):
print(full[i])
print(generateBar(percs[i-1]))