~wattazoum/python-schedule/devel

« back to all changes in this revision

Viewing changes to src/schedule/at.py

  • Committer: Oumar Aziz OUATTARA
  • Date: 2008-04-27 12:52:10 UTC
  • Revision ID: wattazoum@wattazoum-laptop-20080427125210-hhzlsohyva603el6
at implements scheduler

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import tempfile
25
25
import commands
26
26
import time
 
27
from datetime import datetime
27
28
 
28
29
#custom modules
29
30
import config
30
 
 
31
 
 
32
 
class At:
 
31
import Job, Scheduler
 
32
 
 
33
 
 
34
class At (Scheduler):
 
35
        
 
36
        startLine = "## Python schedule"
 
37
        name = "at"
 
38
        
33
39
        def __init__(self,root,user,uid,gid):
34
40
                """
35
41
                @param root: 0 if the user is not root, 1 otherwise
47
53
                self.atRecordRegexAdd = re.compile('([^\s]+)\s([^\s]+)\s')
48
54
                self.atRecordRegexAdded = re.compile('[^\s]+\s([0-9]+)\sat')
49
55
                
 
56
        def getName(self):
 
57
                return self.name
 
58
                
50
59
        def set_rights(self,user,uid,gid):
51
60
                self.user = user
52
61
                self.uid = uid
53
62
                self.gid = gid
54
63
 
55
64
        
56
 
        def get_type (self):
57
 
                return "at"
58
 
 
59
 
 
60
65
        def parse (self, line, output = 0):
61
66
                if output == 0:
62
67
                        if len (line) > 1 and line[0] != '#':
194
199
 
195
200
                return True, "ok"
196
201
 
197
 
        
198
 
        #TODO merge code of append and update   
199
 
        def append (self, runat, command, title, icon):
 
202
 
 
203
        def append(self,job):
 
204
                if not isinstance(job, Job):
 
205
                        raise Exception("Job should be an instance of Job")
200
206
                tmpfile = tempfile.mkstemp ()
201
207
                fd, path = tmpfile
202
208
                tmp = os.fdopen(fd, 'w')
203
 
                if title:
204
 
                        tmp.write("TITLE=" + title + "\n")
 
209
                tmp.write(self.startLine)
 
210
                if job.title:
 
211
                        tmp.write("TITLE=" + job.title + "\n")
205
212
                else:
206
213
                        tmp.write("TITLE=Untitled\n")
207
 
                if icon:
208
 
                        tmp.write("ICON=" + icon + "\n")
 
214
                if job.icon:
 
215
                        tmp.write("ICON=" + job.icon + "\n")
209
216
                else:
210
217
                        tmp.write("ICON=None\n")
211
218
 
212
 
                tmp.write (command + "\n")
 
219
                tmp.write (job.command + "\n")
213
220
                tmp.close ()
214
221
                
215
222
                temp = None
216
 
 
217
 
                if self.root == 1:
218
 
                        if self.user != "root":
219
 
                                #changes the ownership
220
 
                                os.chown(path, self.uid, self.gid)
221
 
                                execute = config.getSubin() + " " + self.user + " -c \"" + config.getAtbin() + " " + runat + " -f " + path + " && exit\""
222
 
                                temp = commands.getoutput(execute)
223
 
                        else:
224
 
                                execute = config.getAtbin() + " " + runat + " -f " + path
225
 
                                temp = commands.getoutput(execute)
226
 
                else:
227
 
                        execute = config.getAtbin() + " " + runat + " -f " + path
228
 
                        temp = commands.getoutput(execute)
 
223
                
 
224
                runat = self.__convertdatetoAt(job.date)
 
225
                
 
226
                exec_pre = ""
 
227
                exec_post = ""
 
228
                
 
229
                if self.root == 1 and self.user != "root":
 
230
                        #changes the ownership
 
231
                        os.chown(path, self.uid, self.gid)
 
232
                        exec_pre = config.getSubin() + " " + self.user + " -c \""
 
233
                        exec_post = " && exit\""
 
234
                
 
235
                execute = exec_pre + config.getAtbin() + " " + runat + " -f " + path + exec_post
 
236
                temp = commands.getoutput(execute)
229
237
 
230
238
                os.unlink (path)
231
239
                return temp
232
240
 
233
 
 
234
 
        def update (self, job_id, runat, command, title, icon):
 
241
        def update (self, job):
 
242
                """
 
243
                Update a job in the scheduler
 
244
                @param runat: see the format in here /usr/share/doc/at/timespec.
 
245
                """
 
246
                if not isinstance(job, Job):
 
247
                        raise Exception("Job should be an instance of Job")
235
248
                #remove old
236
 
                execute = config.getAtrmbin() + " " + str(job_id)
237
 
                commands.getoutput(execute)
 
249
                self.delete(job.id)
238
250
                
239
251
                #add new
240
 
                tmpfile = tempfile.mkstemp ()
241
 
                fd, path = tmpfile
242
 
                tmp = os.fdopen(fd, 'w')
243
 
                if title:
244
 
                        tmp.write("TITLE=" + title + "\n")
245
 
                else:
246
 
                        tmp.write("TITLE=Untitled\n")
247
 
                if icon:
248
 
                        tmp.write("ICON=" + icon + "\n")
249
 
                else:
250
 
                        tmp.write("ICON=None\n")
251
 
                tmp.write (command + "\n")
252
 
                tmp.close ()
253
 
 
254
 
                if self.root == 1:
255
 
                        if self.user != "root":
256
 
                                #changes the ownership
257
 
                                os.chown(path, self.uid, self.gid)
258
 
                                execute = config.getSubin() + " " + self.ParentClass.user + " -c \"" + config.getAtbin() + " " + runat + " -f " + path + " && exit\""
259
 
                                temp = commands.getoutput(execute)
260
 
 
261
 
                else:
262
 
                        execute = config.getAtbin() + " " + runat + " -f " + path
263
 
                        temp = commands.getoutput(execute)
264
 
 
265
 
                os.unlink (path)
266
 
                
267
 
 
268
 
        def delete (self, jobid, iter):
269
 
                if jobid:
270
 
                        execute = config.getAtrmbin()+ " " + str(jobid)
 
252
                self.append(job)
 
253
        
 
254
        def delete (self, job):
 
255
                if not isinstance(job, Job):
 
256
                        raise Exception("Job should be an instance of Job")
 
257
                if job.id:
 
258
                        execute = config.getAtrmbin()+ " " + str(job.id)
271
259
                        commands.getoutput(execute)
272
260
                        
273
261
                                
309
297
                return data
310
298
 
311
299
        
 
300
        def __convertdatetoAt(self,date):
 
301
                """
 
302
                @param date: the date in the time format
 
303
                @type date: datetime
 
304
                @return: the date in at format
 
305
                @rtype: str
 
306
                """
 
307
                if not isinstance(date, datetime) :
 
308
                        raise Exception("date should be a time object")
 
309
                at_format = str(date.hour)+":"+str(date.minute)+" "+str(date.month)+"/"+str(date.day)+"/"+str(date.year) 
 
310
                return at_format
 
311
                
 
312
        
312
313
        def __prepare_script__ (self, script):
313
314
        
314
315
                # It looks like at prepends a bunch of stuff to each script