~wattazoum/python-schedule/devel

« back to all changes in this revision

Viewing changes to src/schedule/crontab.py

  • Committer: Oumar Aziz OUATTARA
  • Date: 2008-05-01 13:13:00 UTC
  • Revision ID: wattazoum@wattazoum-laptop-20080501131300-ghbw4d1xdv0j2xbg
Started crontab

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#custom modules
27
27
import lang
28
28
import config
29
 
 
30
 
 
31
 
class Crontab:
 
29
import calendar
 
30
from datetime import datetime
 
31
import Job, Scheduler
 
32
 
 
33
 
 
34
class Crontab (Scheduler):
32
35
        """
33
36
        The crontab manager class . It's designated to be instanciated for a certain user and group
34
37
        """
35
38
        
 
39
        name = "crontab"
 
40
        
36
41
        def __init__(self,root,user,uid,gid):
37
42
                """
38
43
                @param root: 0 if the user is not root, 1 otherwise
47
52
                self.set_rights(user,uid,gid)
48
53
                
49
54
                self.nooutputtag = ">/dev/null 2>&1"
50
 
                self.crontabRecordRegex = re.compile('([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s([^#\n$]*)(\s#\s([^\n$]*)|$)')
 
55
 
 
56
                self.crontabRecordRegex = 5*"([0-9]{,2}|\*) " + "([^ ]+?) " + "(.+?)( # ([0-9]+?);(.+);(.+))?$"
51
57
 
52
58
                self.timeranges = { 
53
59
                        "minute"   : range(0,60), 
98
104
                self.gid = gid
99
105
 
100
106
 
101
 
        def get_type (self):
 
107
        def getName (self):
102
108
                """
103
109
                @return: the string "crontab"
104
110
                """
105
 
                return "crontab"
 
111
                return self.name
106
112
        
107
113
        def checkfield (self, expr, type):
108
114
                """
205
211
                self.__write__ ()
206
212
                
207
213
                
208
 
        def append (self, minute, hour, day, month, weekday, command, nooutput, title, icon = None):
 
214
        def append (self, job):
209
215
                """
210
216
                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
 
217
                @param job: The job to append
220
218
                """
221
 
                
222
 
                record = minute + " " + hour + " " + day + " " + month + " " + weekday + " " + command
223
 
                if nooutput:
224
 
                        space = " "
225
 
                        if record[len(record)-1] == " ":
226
 
                                space = ""
227
 
                        record = record + space + self.nooutputtag
228
 
                if title != None and icon == None:
229
 
                        record = record + " # " + title
230
 
                elif title != None and icon != None:
231
 
                        record = record + " # " + title + ", " + icon
232
 
                elif title == None and icon != None:
233
 
                        record = record + " # " + _("Untitled") + ", " + icon
234
 
 
 
219
                if not isinstance(job, Job):
 
220
                        raise Exception ("job should be of type Job")
 
221
                
 
222
                d = job.date
 
223
                
 
224
                # set periodicity 
 
225
                if job.periodicity == Scheduler.YEAR :
 
226
                        record = "%d %d %d %d *" % (d.minute,d.hour,d.day, d.month )
 
227
                elif job.periodicity == Scheduler.MONTH :
 
228
                        record = "%d %d %d * *" % (d.minute,d.hour,d.day )
 
229
                elif job.periodicity == Scheduler.WEEK :
 
230
                        record = "%d %d * * %d" % (d.minute,d.hour,calendar.weekday(d.year, d.month, d.day)+1 )
 
231
                elif Job.periodicity == Scheduler.DAY :
 
232
                        record = "%d %d * * *" % (d.minute,d.hour )
 
233
                elif Job.periodicity == Scheduler.HOUR :
 
234
                        record = "%d * * * *" % (d.minute )
 
235
                else :
 
236
                        record = "%d %d %d %d %d" % (d.minute,d.hour,d.day, d.month, calendar.weekday(d.year, d.month, d.day)+1 )
 
237
                
 
238
                record = record + " %s " % job.command
 
239
                
 
240
                record = record + " # %d;%s;%s" % (job.id, job.title, job.icon)
 
241
                
235
242
                self.lines.append (record)
236
243
                
237
244
                # TODO: let write trow an exception if failed
238
245
                self.__write__ ()
239
 
                
240
246
        
241
247
        def humanRead(self,title=None):
242
248
                """
341
347
                """
342
348
                                           min        hour      day      month      wday     command   comment/title-icon or end
343
349
                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
 
350
                A record:       * * * * * echo $a >/dev/null 2>&1 # 1;Untitled;/usr/share/icons/gnome/48x48/mimetypes/gnome-mime-application.png
345
351
                
346
352
                Parse a crontab line and return it as a tuple
347
353
                @param line: The crontab line
348
354
                @return: (minute, hour, day, month, weekday, command, title, icon) or None for empty line and comment
349
355
                @raise Exception: if the line is neither a comment nor empty and is invalid
350
356
                """
 
357
                def getPeriodicity(line):
 
358
                        yearlyRE = re.compile("([0-9]{,2}) ([0-9]{,2}) ([0-9]{,2}) ([0-9]{,2}) (\*) .+$")
 
359
                        monthlyRE = re.compile("([0-9]{,2}) ([0-9]{,2}) ([0-9]{,2}) (\*) (\*) .+$")
 
360
                        weeklyRE = re.compile("([0-9]{,2}) ([0-9]{,2}) (\*) (\*) ([0-9]{,2}) .+$")
 
361
                        dailyRE = re.compile("([0-9]{,2}) ([0-9]{,2}) (\*) (\*) (\*) .+$")
 
362
                        hourlyRE = re.compile("([0-9]{,2}) (\*) (\*) (\*) (\*) .+$")
 
363
                        
 
364
                        if yearlyRE.match(line) : return Scheduler.YEAR
 
365
                        elif monthlyRE.match(line) : return Scheduler.MONTH
 
366
                        elif weeklyRE.match(line) : return Scheduler.WEEK
 
367
                        elif dailyRE.match(line) : return Scheduler.DAY
 
368
                        elif hourlyRE.match(line) : return Scheduler.HOUR
 
369
                
 
370
                def convertToDatetime(srematch):
 
371
                        now = datetime.today()
 
372
                                
 
373
                        #print m.groups()
 
374
                        if srematch.group(1) == '*' : 
 
375
                                minute = now.minute
 
376
                        else : minute = int(srematch.group (1))
 
377
                        
 
378
                        if srematch.group(2) == '*' : 
 
379
                                hour = now.hour  
 
380
                        else : hour = int(srematch.group (2))
 
381
                        
 
382
                        if  srematch.group (3) == '*' :
 
383
                                day = now.day 
 
384
                        else : day = int(srematch.group(3))
 
385
                        
 
386
                        if srematch.group (4) == '*' : 
 
387
                                month = now.month
 
388
                        else : month = int(srematch.group(4))
 
389
                        
 
390
                        year = now.year
 
391
                        if srematch.group (5) == '*' :
 
392
                                weekday = calendar.weekday(year, month, day)
 
393
                        else : weekday = int(srematch.group(5))
 
394
                
 
395
                        return datetime(year,month,day,hour,minute)
 
396
                
351
397
                if len (line) > 1 and line[0] != '#':
352
398
                        
353
399
                        m = self.crontabRecordRegex.match(line) 
354
400
                        if m != None:
355
 
                                        #print m.groups()
356
 
                                        minute = m.groups ()[0]
357
 
                                        hour = m.groups ()[1]
358
 
                                        day = m.groups ()[2]
359
 
                                        month = m.groups ()[3]
360
 
                                        weekday = m.groups ()[4]
361
 
                                        command = m.groups ()[5]
362
 
 
363
 
                                        #icon path is in comment of the task, this is the default
364
 
                                        icon = None
365
 
                                        
366
 
                                        #title is in comment of the task
367
 
                                        title = None
368
 
                                        if m.groups ()[7] != None:
369
 
                                                # TODO: check if works
370
 
                                                lastpiece = m.groups ()[7]
371
 
                                                keep = lastpiece.split (", ")
372
 
                                                title = keep[0]
373
 
                                                if len (keep) > 1:
374
 
                                                        icon = keep[1]
375
 
 
376
 
                                        if title == None:
377
 
                                                title = _("Untitled")
378
 
 
379
 
                                        return minute, hour, day, month, weekday, command, title, icon
 
401
                                result = Job()
 
402
                                
 
403
                                result.date = convertToDatetime(m)
 
404
 
 
405
                                # periodicity 
 
406
                                result.periodicity = self.__getPeriodicity(line)
 
407
                                
 
408
                                # User
 
409
                                result.user = m.group(6)
 
410
                                
 
411
                                # Command
 
412
                                result.command = m.group(7)
 
413
                                
 
414
                                # Job id
 
415
                                result.id = m.group(9)
 
416
                                
 
417
                                # Title
 
418
                                result.title = m.group(10)
 
419
                                
 
420
                                #icon path is in comment of the task, this is the default
 
421
                                result.icon = m.group(11)
 
422
                                
 
423
                                
 
424
                                return result
380
425
                                                
381
426
                        else:
382
427
                                raise Exception( _("ERROR: Failed to parse crontab record : [ %s ]") % line)
383
428
                
384
429
                return None
385
 
                
386
430
 
387
431
        
388
432
        def __easy__ (self, minute, hour, day, month, weekday):