androidpythonxbmckodi

Calculate Remaining Disk Space On Android Using Python


I'm working on a service add-on for Kodi Media Center that will check the remaining disk space and remind a person, once space gets below 500MB, to use the maintenance tool that I've created. It runs as a separate service. I need a way to determine the remaining disk space using python on Android. I tried using statvfs() but it is apparently only compatible on Unix-like systems, including OS X. That means I can use statvfs for Linux and OSX. I can use wmi or ctypes for Windows but nothing for Android so far. I can create a separate wrapper to check the operating system and use the best method for each - but I can't find a python module for Android that can do this. Any suggestions?

Here is my existing code:

import xbmc, xbmcgui, xbmcaddon
import os, sys, statvfs, time, datetime
from time import mktime

__addon__       = xbmcaddon.Addon(id='plugin.service.maintenancetool')
__addonname__   = __addon__.getAddonInfo('name')
__icon__        = __addon__.getAddonInfo('icon')

thumbnailPath = xbmc.translatePath('special://thumbnails');
cachePath = os.path.join(xbmc.translatePath('special://home'), 'cache')
tempPath = xbmc.translatePath('special://temp')
addonPath = os.path.join(os.path.join(xbmc.translatePath('special://home'), 'addons'),'plugin.service.maintenancetool')
mediaPath = os.path.join(addonPath, 'media')
databasePath = xbmc.translatePath('special://database')


if __name__ == '__main__':
    #check HDD freespace
    st = os.statvfs(xbmc.translatePath('special://home'))

if st.f_frsize:
    freespace = st.f_frsize * st.f_bavail/1024/1024
else:
    freespace = st.f_bsize * st.f_bavail/1024/1024

print "Free Space: %dMB"%(freespace)
if(freespace < 500):
    text = "You have less than 500MB of free space"
    text1 = "Please use the Maintenance tool"
    text2 = "immediately to prevent system issues"

    xbmcgui.Dialog().ok(__addonname__, text, text1, text2)


while not xbmc.abortRequested:    
    xbmc.sleep(500)

And here is the error I get:

Error Type: <type 'exceptions.AttributeError'>
Error Contents: 'module' object has no attribute 'statvfs'
Traceback (most recent call last):
File "/storage/emulated/0/Android/data/org.xbmc.kodi/files/.kodi/addons/plugin.service.maintenancetool/service.py", line 39, in <module>
st = os.statvfs(xbmc.translatePath('special://home))
Attribute Error: 'module' object has no attribute 'statvfs'

Solution

  • I have found an answer in a kodi thread and this should return the remaining bytes left on the android device:

    if xbmc.getCondVisibility('system.platform.android'):
            import subprocess
            df = subprocess.Popen(['df', '/storage/emulated/legacy'], stdout=subprocess.PIPE)
            output = df.communicate()[0]
            info = output.split('\n')[1].split()
            size = float(info[1].replace('G', '').replace('M', '')) * 1000000000.0
            size = size - (size % float(info[-1]))
            available = float(info[3].replace('G', '').replace('M', '')) * 1000000000.0
            available = available - (available % float(info[-1]))
            return int(round(available)), int(round(size))