PushService: Added Schwerkraft Git stuff
[enigma2-plugins.git] / pushservice / src / Plugins / CrashLog.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, 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 CRASHLOG_DIR = '/media/hdd'
32
33 SUBJECT = _("Found CrashLog(s)")
34 BODY    = _("Crashlog(s) are attached")
35
36
37 class CrashLog(PluginBase):
38         
39         ForceSingleInstance = True
40         
41         def __init__(self):
42                 # Is called on instance creation
43                 PluginBase.__init__(self)
44                 self.crashlogs = []
45
46                 # Default configuration
47                 self.setOption( 'delete_logs', NoSave(ConfigYesNo( default = False )), _("Delete crashlog(s)") )
48
49         def run(self, callback, errback):
50                 # At the end a plugin has to call one of the functions: callback or errback
51                 # Callback should return with at least one of the parameter: Header, Body, Attachment
52                 # If empty or none is returned, nothing will be sent
53                 self.crashlogs = []
54                 text = "Found crashlogs, see attachment(s)\n"
55                 for file in os.listdir( CRASHLOG_DIR ):
56                         if file.startswith("enigma2_crash_") and file.endswith(".log"):
57                                 crashlog = os.path.join( CRASHLOG_DIR, file )
58                                 self.crashlogs.append(crashlog)
59                 if self.crashlogs:
60                         callback( SUBJECT, BODY, self.crashlogs )
61                 else:
62                         callback()
63
64         # Callback functions
65         def callback(self):
66                 # Called after all services succeded
67                 if self.getValue('delete_logs'):
68                         # Delete crashlogs
69                         for crashlog in self.crashlogs[:]:
70                                 if os.path.exists( crashlog ):
71                                         os.remove( crashlog )
72                                 self.crashlogs.remove( crashlog )
73                 else:
74                         # Rename crashlogs to avoid resending it
75                         for crashlog in self.crashlogs[:]:
76                                 if os.path.exists( crashlog ):
77                                         # Adapted from autosubmit - instead of .sent we will use .pushed
78                                         currfilename = str(os.path.basename(crashlog))
79                                         newfilename = "/media/hdd/" + currfilename + ".pushed"
80                                         os.rename(crashlog,newfilename)
81                                 self.crashlogs.remove( crashlog )
82
83         def errback(self):
84                 # Called after all services has returned, but at least one has failed
85                 self.crashlogs = []