]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/python/FDStartUp.py
0ef8ea993b80fa98aa454c79efe7d89d0b8fe3a4
[bacula/bacula] / bacula / examples / python / FDStartUp.py
1 #
2 # Bacula Python interface script for the File Daemon
3 #
4 # You must import both sys and bacula
5 import sys, bacula
6
7 # This is the list of Bacula daemon events that you
8 #  can receive.
9 class BaculaEvents:
10   def __init__(self):
11      # Called here when a new Bacula Events class is
12      #  is created. Normally not used 
13      noop = 1
14
15   def JobStart(self, job):
16      """
17        Called here when a new job is started. If you want
18        to do anything with the Job, you must register
19        events you want to receive.
20      """
21      events = JobEvents()         # create instance of Job class
22      events.job = job             # save Bacula's job pointer
23      job.set_events(events)       # register events desired
24      sys.stderr = events          # send error output to Bacula
25      sys.stdout = events          # send stdout to Bacula
26      jobid = job.get("JobId")
27      client = job.get("Client")
28      job.set(JobReport="Python FD StartJob: JobId=%d Client=%s \n" % (jobid,client))
29      return 1
30
31   # Bacula Job is going to terminate
32   def JobEnd(self, job):    
33      jobid = job.get("JobId")
34      client = job.get("Client") 
35      job.set(JobReport="Python FD EndJob output: JobId=%d Client=%s.\n" % (jobid, client))
36      return 1
37
38   # Called here when the Bacula daemon is going to exit
39   def Exit(self, job):
40       noop = 1
41      
42 bacula.set_events(BaculaEvents()) # register daemon events desired
43
44 """
45   There are the Job events that you can receive.
46 """
47 class JobEvents:
48   def __init__(self):
49      # Called here when you instantiate the Job. Not
50      # normally used
51      noop = 1
52
53   # Pass output back to Bacula
54   def write(self, text):
55      self.job.write(text)
56
57   # Open file to be backed up. file is the filename
58   def open(self, file):
59      print "Open %s called" % file
60      self.fd = open('m.py', 'rb')
61      jobid = self.job.get("JobId")
62      print "Open: JobId=%d" % jobid
63      print "name=%s" % bacula.name
64
65   # Read file data into Bacula memory buffer (mem)
66   #  return length read. 0 => EOF, -1 => error
67   def read(self, mem):
68      print "Read called\n"
69      len = self.fd.readinto(mem)
70      print "Read %s bytes into mem.\n" % len
71      return len
72
73   # Close file
74   def close(self):
75      self.fd.close()