~wattazoum/python-schedule/devel

« back to all changes in this revision

Viewing changes to python-schedule/src/schedule/crontab.py

  • Committer: Oumar Aziz OUATTARA (alias wattazoum)
  • Date: 2007-10-22 20:20:06 UTC
  • Revision ID: wattazoum@gmail.com-20071022202006-7ut5sidsakdn42dt
Starting the modification of the api to be generic

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import re
22
22
import os
23
23
import tempfile
 
24
from gettext import gettext as _
24
25
 
25
26
#custom modules
26
27
import lang
28
29
 
29
30
 
30
31
class Crontab:
 
32
        """
 
33
        The crontab manager class . It's designated to be instanciated for a certain user and group
 
34
        """
 
35
        
31
36
        def __init__(self,root,user,uid,gid):
 
37
                """
 
38
                @param root: 0 if the user is not root, 1 otherwise
 
39
                @param user: The user name ( in string )  use pwd module to get those infos (userdb = pwd.getpwnam(user) )
 
40
                @param uid: 
 
41
                @param gid: 
 
42
                """
 
43
                
32
44
                #default preview length
33
45
                self.preview_len = 50
34
46
                self.root = root
87
99
 
88
100
 
89
101
        def get_type (self):
 
102
                """
 
103
                @return: the string "crontab"
 
104
                """
90
105
                return "crontab"
91
106
        
92
107
        def checkfield (self, expr, type):
93
 
                """Verifies format of Crontab timefields
 
108
                """
 
109
                Verifies format of Crontab timefields
94
110
 
95
111
                Checks a single Crontab time expression.
96
112
                At first possibly contained alias names will be replaced by their
172
188
                self.__write__ ()
173
189
        
174
190
        
175
 
        def delete (self, linenumber, iter):
 
191
        def delete (self, linenumber):
 
192
                """
 
193
                Delete a crontab entry by line number
 
194
                @param linenumber: the linenumber to delete
 
195
                """
176
196
                number = 0
177
197
                newlines = list ()
178
198
                for line in self.lines:
186
206
                
187
207
                
188
208
        def append (self, minute, hour, day, month, weekday, command, nooutput, title, icon = None):
 
209
                """
 
210
                The method to add an entry to crontab (see man 5 crontab )
 
211
                @param minute : 
 
212
                @param hour: 
 
213
                @param day: 
 
214
                @param month:
 
215
                @param weekday: 
 
216
                @param command: The command to execute
 
217
                @param nooutput: True if we don't want any output (False for normal behaviour)
 
218
                @param title: The title of the task (might be used for a search )
 
219
                @param icon: an icon path to associate
 
220
                """
 
221
                
189
222
                record = minute + " " + hour + " " + day + " " + month + " " + weekday + " " + command
190
223
                if nooutput:
191
224
                        space = " "
204
237
                # TODO: let write trow an exception if failed
205
238
                self.__write__ ()
206
239
                
207
 
 
208
 
        #read tasks in crontab
209
 
        def read (self):
210
 
                
 
240
        
 
241
        def humanRead(self,title=None):
 
242
                """
 
243
                Return the task list in a human readable form 
 
244
                @param title: a regexp that should match the title of the tasks to get, if title is None every thing is return
 
245
                @type title: regexp
 
246
                @return: an array with the list of corresponding tasks [linenumber, title, human_readable_string, command]
 
247
                """
 
248
                datas = self.read(title)
 
249
                for d in datas :
 
250
                        d[2] = self.__easy__(d[2][0],d[2][1],d[2][2],d[2][3],d[2][4])
 
251
                return datas
 
252
                
 
253
                
 
254
                
 
255
        #read tasks in crontab
 
256
        def read (self,t_title=None):
 
257
                """
 
258
                Return the task list in a human readable form 
 
259
                @param title: a regexp that should match the title of the tasks to get, if title is None every thing is return
 
260
                @type title: str
 
261
                @return: an array with the list of corresponding tasks [[linenumber, title, (minute, hour, day, month, weekday), command], ]
 
262
                """
 
263
                if t_title :
 
264
                        r = re.compile(t_title)
 
265
                data = []
 
266
 
 
267
                if self.root:
 
268
                        execute = config.getCrontabbin () + " -l -u " + self.user
 
269
                else:
 
270
                        execute = config.getCrontabbin () + " -l"
 
271
                
 
272
                linecount = 0
 
273
                self.lines = os.popen(execute).readlines()
 
274
                for line in self.lines:
 
275
                        #read line and get info
 
276
                        cron_infos = self.parse (line)
 
277
                        if cron_infos :
 
278
                                (minute, hour, day, month, weekday, command, title, icon) = cron_infos
 
279
                                if t_title :
 
280
                                        # match the regexp
 
281
                                        m = r.match(title)
 
282
                                        if m :  data.append([linecount,title, (minute, hour, day, month, weekday),command])
 
283
                                else :
 
284
                                        data.append([linecount,title, (minute, hour, day, month, weekday),command])
 
285
                        linecount+=1 ;
 
286
                
 
287
                return data
 
288
 
 
289
 
 
290
        #read tasks in crontab
 
291
        def legacy_read (self):
 
292
                                
211
293
                data = []
212
294
 
213
295
                if self.root:
256
338
        
257
339
        #get info out of task line
258
340
        def parse (self, line):
 
341
                """
 
342
                                           min        hour      day      month      wday     command   comment/title-icon or end
 
343
                The regexp:     ('([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s([^#\n$]*)(\s#\s([^\n$]*)|$)')
 
344
                A record:       * * * * * echo $a >/dev/null 2>&1 # Untitled, /usr/share/icons/gnome/48x48/mimetypes/gnome-mime-application.png
 
345
                
 
346
                Parse a crontab line and return it as a tuple
 
347
                @param line: The crontab line
 
348
                @return: (minute, hour, day, month, weekday, command, title, icon) or None for empty line and comment
 
349
                @raise Exception: if the line is neither a comment nor empty and is invalid
 
350
                """
259
351
                if len (line) > 1 and line[0] != '#':
260
 
                        """
261
 
                                                   min        hour      day      month      wday     command   comment/title-icon or end
262
 
                        The regexp:     ('([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s([^#\n$]*)(\s#\s([^\n$]*)|$)')
263
 
                        A record:       * * * * * echo $a >/dev/null 2>&1 # Untitled, /usr/share/icons/gnome/48x48/mimetypes/gnome-mime-application.png
264
 
                        """
 
352
                        
265
353
                        m = self.crontabRecordRegex.match(line) 
266
354
                        if m != None:
267
355
                                        #print m.groups()
291
379
                                        return minute, hour, day, month, weekday, command, title, icon
292
380
                                                
293
381
                        else:
294
 
                                print _("ERROR: Failed to parse crontab record")
295
 
                return False
296
 
                # TODO: throw exception
 
382
                                raise Exception( _("ERROR: Failed to parse crontab record : [ %s ]") % line)
 
383
                
 
384
                return None
 
385
                
297
386
 
298
387
        
299
388
        def __easy__ (self, minute, hour, day, month, weekday):