PushService: Initial release
[enigma2-plugins.git] / pushservice / src / Plugins / FreeSpace.py
1 # Config
2 from Components.config import ConfigYesNo, ConfigText, ConfigNumber, NoSave
3
4 # Plugin internal
5 from Plugins.Extensions.PushService.__init__ import _
6 from Plugins.Extensions.PushService.PluginBase import PluginBase
7
8 # Plugin specific
9 import os
10
11
12 SUBJECT = _("Free space warning")
13 BODY    = _("Free disk space limit has been reached:\n") \
14                                 + _("Path:  %s\n") \
15                                 + _("Limit: %d GB\n") \
16                                 + _("Left:  %s")
17
18
19 class FreeSpace(PluginBase):
20         
21         ForceSingleInstance = False
22         
23         def __init__(self):
24                 # Is called on instance creation
25                 PluginBase.__init__(self)
26                 
27                 # Default configuration
28                 self.setOption( 'wakehdd',  NoSave(ConfigYesNo(  default = False )),                                  _("Allow HDD wake up") )
29                 self.setOption( 'path',     NoSave(ConfigText(   default = "/media/hdd/movie", fixed_size = False )), _("Where to check free space") )
30                 self.setOption( 'limit',    NoSave(ConfigNumber( default = 100 )),                                    _("Free space limit in GB") )
31         
32         def run(self):
33                 # Return Header, Body, Attachment
34                 # If empty or none is returned, nothing will be sent
35                 path = self.getValue('path')
36                 limit = self.getValue('limit')
37                 
38                 if not self.getValue('wakehdd'):
39                         def mountpoint(path):
40                                 path = os.path.realpath(path)
41                                 if os.path.ismount(path) or len(path)==0: return path
42                                 return mountpoint(os.path.dirname(path))
43                         
44                         # User specified to avoid HDD wakeup if it is sleeping
45                         from Components.Harddisk import harddiskmanager
46                         p = harddiskmanager.getPartitionbyMountpoint( mountpoint(path) )
47                         if p is not None and p.uuid is not None:
48                                 dev = harddiskmanager.getDeviceNamebyUUID(p.uuid)
49                                 if dev is not None:
50                                         hdd = harddiskmanager.getHDD(dev)
51                                         if hdd is not None:
52                                                 if hdd.isSleeping():
53                                                         # Don't wake up HDD
54                                                         print _("[FreeSpace] HDD is idle: ") + str(path)
55                                                         return
56                                                 #TODO TEST
57                                                 else:
58                                                         print _("[FreeSpace] TEST HDD is not idle: ") + str(path)
59                 
60                 # Check free space on path
61                 if os.path.exists( path ):
62                         stat = os.statvfs( path )
63                         free = ( stat.f_bavail if stat.f_bavail!=0 else stat.f_bfree ) * stat.f_bsize / 1024 / 1024 # MB
64                         if limit > (free/1024): #GB
65                                 if free >= 10*1024:     #MB
66                                         free = "%d GB" %(free/1024)
67                                 else:
68                                         free = "%d MB" %(free)
69                                 # Not enough free space
70                                 return SUBJECT, BODY % (path, limit, free)
71                         else:
72                                 # There is enough free space
73                                 return None