V 2.0b1-20140219: Check if refresh is still running to prevent multiple
[enigma2-plugins.git] / epgrefresh / src / EPGBackupSupport.py
1 # -*- coding: UTF-8 -*-
2
3 from Screens.ChoiceBox import ChoiceBox
4 from Screens.MessageBox import MessageBox
5 from Screens.Standby import TryQuitMainloop
6
7 from . import _
8
9 # Error-print
10 from traceback import print_exc
11 from sys import stdout
12
13 # SH-Script
14 from Tools.Directories import SCOPE_PLUGINS, resolveFilename
15 import os
16 SH_EXEC_FILE = resolveFilename(SCOPE_PLUGINS, "Extensions/EPGRefresh/EPGBackup.sh")
17 SH_TMP_OUTPUT="/tmp/.EPGBackup.sh.output"
18 BOOTCOUNTERFILE="/tmp/.EPGBackup.boot.counter"
19
20 FORCEBACKUPNOFILES="FORCEBACKUP_NOFILES"
21 FORCEBACKUPCANCEL="FORCEBACKUP_CANCEL"
22 EPGBACKUP_SHELL_CONSTANTS = { 'INSTALL': 'install',
23                 'UNINSTALL': 'uninstall',
24                 'SETFORCERESTORE': 'setforcefile',
25                 'EPGINFO': 'epginfo',
26                 'EPGINFOSORTSIZE': 'bySize',
27                 'ISPATCHED': 'ispatched',
28                 'STRINGBIGGEST': 'biggest',
29                 'STRINGYOUNGEST': 'youngest',
30                 'STRINGFORCED': 'force',
31 }
32 class EPGBackupSupport:
33         """Class for the Backup-Functionality"""
34
35         def __init__(self, session):
36                 # Initialize
37                 self.session = session
38                 
39                 # We have succesfully booted, so delete the counter-File
40                 if os.path.exists(BOOTCOUNTERFILE):
41                         os.remove(BOOTCOUNTERFILE)
42
43         def forceBackup(self):
44                 self._forceBackup()
45                 
46         def forceBackupBySize(self):
47                 self._forceBackup(EPGBACKUP_SHELL_CONSTANTS["EPGINFOSORTSIZE"])
48                 
49         def _forceBackup(self, sortMode = ""):
50                 backupList = self._getBackupFiles(sortMode)
51                 if len(backupList) == 0:
52                         backupList.append((_("No Backupfiles found"), FORCEBACKUPNOFILES))
53                 backupList.insert(0, (_("Cancel"), FORCEBACKUPCANCEL))
54                 self.session.openWithCallback(self._forceBackupCB,
55                         ChoiceBox, _("Select a file to force a Backup"), backupList)
56         
57         def _forceBackupCB(self, backupinfo):
58                 if backupinfo is None:
59                         return
60                 else:
61                         backupfile = backupinfo [1].rstrip()
62                         if backupfile[1] and FORCEBACKUPCANCEL != backupfile and FORCEBACKUPNOFILES != backupfile:
63                                 self.executeShScript(EPGBACKUP_SHELL_CONSTANTS["SETFORCERESTORE"], backupinfo[1].rstrip())
64                                 self.session.openWithCallback(self._restartGUICB, MessageBox, \
65                                         _("To load the Backupfile the GUI has to be restarted.\nDo you want to restart the GUI now?"), \
66                                         MessageBox.TYPE_YESNO, timeout =  30, default = False)
67
68         def _restartGUICB(self, answer):
69                 if answer is True:
70                         self.session.open(TryQuitMainloop, 3)
71         
72         def _getBackupFiles(self, sortMode):
73                 try:
74                         backupList = []
75                         backupStrList = self.executeShScript(EPGBACKUP_SHELL_CONSTANTS["EPGINFO"], sortMode, True)
76                         if backupStrList:
77                                 backupfiles = backupStrList.split("\n")
78                         if backupfiles:
79                                 for file in backupfiles:
80                                         if file:
81                                                 file = file.replace(EPGBACKUP_SHELL_CONSTANTS["STRINGBIGGEST"], _("FILELIST_BIGGEST"))
82                                                 file = file.replace(EPGBACKUP_SHELL_CONSTANTS["STRINGYOUNGEST"], _("FILELIST_YOUNGEST"))
83                                                 file = file.replace(EPGBACKUP_SHELL_CONSTANTS["STRINGFORCED"], _("FILELIST_FORCED"))
84                                                 backupList.append(( file, file.split(" ")[0] ))
85                 except:
86                         print("[EPGRefresh] getBackupFiles Error")
87                         print_exc(file=stdout)
88                 return backupList
89         
90         def install(self):
91                 self.executeShScript(EPGBACKUP_SHELL_CONSTANTS["INSTALL"])
92         
93         def uninstall(self):
94                 self.executeShScript(EPGBACKUP_SHELL_CONSTANTS["UNINSTALL"])
95
96         def isPatched(self):
97                 ispatched = False
98                 ispatchedtxt = self.executeShScript(EPGBACKUP_SHELL_CONSTANTS["ISPATCHED"], getoutput = True)
99                 if ispatchedtxt == "true":
100                         ispatched = True
101                 return ispatched
102
103         def executeShScript(self, sh_action, param1 = "", getoutput = False):
104                 outtext = ""
105                 if getoutput:
106                         os.system(SH_EXEC_FILE + " %s %s > %s" %(sh_action, param1, str(SH_TMP_OUTPUT)))
107                         fo=open(str(SH_TMP_OUTPUT))
108                         line = fo.readline()
109                         while (line):
110                                 outtext += line
111                                 line = fo.readline()
112                         fo.close
113                 else:
114                         print("[EPGRefresh] execute sh wiht params %s %s" %(sh_action, param1))
115                         os.system(SH_EXEC_FILE + " %s %s" %(sh_action, param1))
116                 return outtext
117
118
119