PushService: Added Schwerkraft Git stuff
[enigma2-plugins.git] / pushservice / src / Plugins / FreeSpace.py
1 #######################################################################
2 #
3 #    Push Service for Enigma-2
4 #    Coded by betonme (c) 2012 <glaserfrank(at)gmail.com>
5 #    Support: http://www.i-have-a-dreambox.com/wbb2/thread.php?threadid=167779
6 #
7 #    This program is free software; you can redistribute it and/or
8 #    modify it under the terms of the GNU General Public License
9 #    as published by the Free Software Foundation; either version 2
10 #    of the License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #######################################################################
18
19 # Config
20 from Components.config import ConfigYesNo, ConfigText, ConfigNumber, NoSave
21
22 # Plugin internal
23 from Plugins.Extensions.PushService.__init__ import _
24 from Plugins.Extensions.PushService.PluginBase import PluginBase
25
26 # Plugin specific
27 import os
28
29
30 # Constants
31 SUBJECT = _("Free space warning")
32 BODY    = _("Free disk space limit has been reached:\n") \
33                                 + _("Path:  %s\n") \
34                                 + _("Limit: %d GB\n") \
35                                 + _("Left:  %s")
36
37
38 class FreeSpace(PluginBase):
39         
40         ForceSingleInstance = False
41         
42         def __init__(self):
43                 # Is called on instance creation
44                 PluginBase.__init__(self)
45                 
46                 # Default configuration
47                 self.setOption( 'wakehdd',  NoSave(ConfigYesNo(  default = False )),                                  _("Allow HDD wake up") )
48                 self.setOption( 'path',     NoSave(ConfigText(   default = "/media/hdd/movie", fixed_size = False )), _("Where to check free space") )
49                 self.setOption( 'limit',    NoSave(ConfigNumber( default = 100 )),                                    _("Free space limit in GB") )
50         
51         def run(self, callback, errback):
52                 # At the end a plugin has to call one of the functions: callback or errback
53                 # Callback should return with at least one of the parameter: Header, Body, Attachment
54                 # If empty or none is returned, nothing will be sent
55                 path = self.getValue('path')
56                 limit = self.getValue('limit')
57                 
58                 if not self.getValue('wakehdd'):
59                         def mountpoint(path):
60                                 path = os.path.realpath(path)
61                                 if os.path.ismount(path) or len(path)==0: return path
62                                 return mountpoint(os.path.dirname(path))
63                         
64                         # User specified to avoid HDD wakeup if it is sleeping
65                         from Components.Harddisk import harddiskmanager
66                         p = harddiskmanager.getPartitionbyMountpoint( mountpoint(path) )
67                         if p is not None and p.uuid is not None:
68                                 dev = harddiskmanager.getDeviceNamebyUUID(p.uuid)
69                                 if dev is not None:
70                                         hdd = harddiskmanager.getHDD(dev)
71                                         if hdd is not None:
72                                                 if hdd.isSleeping():
73                                                         # Don't wake up HDD
74                                                         print _("[FreeSpace] HDD is idle: ") + str(path)
75                                                         callback()
76                 
77                 # Check free space on path
78                 if os.path.exists( path ):
79                         stat = os.statvfs( path )
80                         free = ( stat.f_bavail if stat.f_bavail!=0 else stat.f_bfree ) * stat.f_bsize / 1024 / 1024 # MB
81                         if limit > (free/1024): #GB
82                                 if free >= 10*1024:     #MB
83                                         free = "%d GB" %(free/1024)
84                                 else:
85                                         free = "%d MB" %(free)
86                                 # Not enough free space
87                                 callback( SUBJECT, BODY % (path, limit, free) )
88                         else:
89                                 # There is enough free space
90                                 callback()