AutoTimer: Force write xml before backup download starts
[enigma2-plugins.git] / autotimer / src / WebComponents / Sources / AutoTimerEditor.py
1 from Components.Sources.Source import Source
2 from os import remove, path, popen
3 from re import compile as re_compile
4
5 class AutoTimerEditor(Source):
6         BACKUP = 0
7         RESTORE = 1
8
9         BACKUP_PATH = "/tmp"
10         BACKUP_FILENAME = "autotimer_backup.tar"
11
12         def __init__(self, session, func=BACKUP):
13                 Source.__init__(self)
14                 self.func = func
15                 self.session = session
16                 self.command = None
17                 self.bouquet_rootstr = ""
18                 self.result = ( False, "one two three four unknown command" )
19
20         def handleCommand(self, cmd):
21                 print "[WebComponents.AutoTimerEditor] handleCommand with cmd = ", cmd
22                 if self.func is self.BACKUP:
23                         self.result = self.backupFiles(cmd)
24                 elif self.func is self.RESTORE:
25                         self.result = self.restoreFiles(cmd)
26                 else:
27                         self.result = ( False, "one two three four unknown command" )
28
29         def backupFiles(self, param):
30                 filename = param
31                 if not filename:
32                         filename = self.BACKUP_FILENAME
33                 invalidCharacters= re_compile(r'[^A-Za-z0-9_. ]+|^\.|\.$|^ | $|^$')
34                 tarFilename= "%s.tar" % invalidCharacters.sub('_', filename)
35                 backupFilename = path.join(self.BACKUP_PATH, tarFilename)
36                 if path.exists(backupFilename):
37                         remove(backupFilename)
38                 
39                 from Plugins.Extensions.AutoTimer.plugin import autotimer
40                 if autotimer is not None:
41                         try:
42                                 # Force config write
43                                 autotimer.writeXml()
44                         except Exception:
45                                 # TODO: proper error handling
46                                 pass
47                 
48                 checkfile = path.join(self.BACKUP_PATH,'.autotimeredit')
49                 f = open(checkfile, 'w')
50                 if f:
51                         files = []
52                         f.write('created with AutoTimerWebEditor')
53                         f.close()
54                         files.append(checkfile)
55                         files.append("/etc/enigma2/autotimer.xml")
56                         tarFiles = ""
57                         for arg in files:
58                                 if not path.exists(arg):
59                                         return (False, "Error while preparing backup file, %s does not exists." % arg)
60                                 tarFiles += "%s " % arg
61                         lines = popen("tar cvf %s %s" % (backupFilename,tarFiles)).readlines()
62                         remove(checkfile)
63                         return (True, tarFilename)
64                 else:
65                         return (False, "Error while preparing backup file.")
66
67         def restoreFiles(self, param):
68                 backupFilename = param
69                 if path.exists(backupFilename):
70                         check_tar = False
71                         lines = popen('tar -tf %s' % backupFilename).readlines()
72                         for line in lines:
73                                 pos = line.find('tmp/.autotimeredit')
74                                 if pos != -1:
75                                         check_tar = True
76                                         break
77                         if check_tar:
78                                 lines = popen('tar xvf %s -C /' % backupFilename).readlines()
79
80                                 from Plugins.Extensions.AutoTimer.plugin import autotimer
81                                 if autotimer is not None:
82                                         try:
83                                                 # Force config reload
84                                                 autotimer.configMtime = -1
85                                                 autotimer.readXml()
86                                         except Exception:
87                                                 # TODO: proper error handling
88                                                 pass
89                                 
90                                 remove(backupFilename)
91                                 return (True, "AutoTimer-settings were restored successfully")
92                         else:
93                                 return (False, "Error, %s was not created with AutoTimerWebEditor..." % backupFilename)
94                 else:
95                         return (False, "Error, %s does not exists, restore is not possible..." % backupFilename)