Changeset 51:c9a3aa98929f in livinglogic.python.tipimaid

Show
Ignore:
Timestamp:
12/22/08 18:06:24 (11 years ago)
Author:
Nikolas Tautenhahn <nik@…>
Branch:
default
Message:

if our connection should drop, we log locally. for this to work we make sure that we have the proper rights and access to the directory

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • liaalh_sender.py

    r50 r51  
    1 import sys, socket, select, signal, datetime 
     1import sys, socket, select, signal, datetime, os 
    22 
    33def intify(s, defaultint=0): 
     
    88 
    99class Sender(object): 
    10     def __init__(self, ip, port, backupport, stream=sys.stdin): 
     10    def __init__(self, ip, port, backuppath, stream=sys.stdin): 
    1111        self.ip = ip 
    1212        self.port = port 
    13         self.backupport = backupport 
    1413        self.stream = stream 
    1514        self.check_every = datetime.timedelta(seconds=10) 
    1615        self.s = None 
     16        self.f = None 
     17        if backuppath: 
     18            backuppath = backuppath.rstrip(os.path.sep) 
     19            try: 
     20                os.chdir(backuppath) 
     21            except OSError, exc: 
     22                sys.stderr.write("Unable to enter directory %s\n" % backuppath) 
     23                raise 
     24            try: 
     25                filename = str(hash(datetime.datetime.now())) 
     26                f = open(os.path.join(backuppath, filename), "a") 
     27                f.close() 
     28                os.remove(os.path.join(backuppath, filename)) 
     29            except IOError, exc: 
     30                sys.stderr.write("No write permissions for directory %s\n" % backuppath) 
     31                raise 
     32            self.backuppath = backuppath 
    1733        self.create_socket() 
    1834        self.last_check_connection = datetime.datetime.now() 
     
    6278                    raise socket.timeout 
    6379            except (socket.timeout, socket.error), exc: 
    64                 print "Could not send", line # Handle error 
     80                signal.alarm(0) 
    6581                self.s.close() 
    6682                self.s = None 
    6783                self.last_check_connection = datetime.datetime.now() 
     84                self.send_tempfile(line) 
    6885            signal.alarm(0) 
    6986 
    7087    def send_tempfile(self, line): 
    71         print "Writing ", line 
     88        if self.f: 
     89            if self.f.name != os.path.join(self.backuppath, datetime.datetime.now().strftime("%Y%m%d_recovery.log")): 
     90                self.f.close() 
     91                self.f = open(os.path.join(self.backuppath, datetime.datetime.now().strftime("%Y%m%d_recovery.log")), "a") 
     92        else: 
     93            self.f = open(os.path.join(self.backuppath, datetime.datetime.now().strftime("%Y%m%d_recovery.log")), "a") 
     94        self.f.write(line) 
    7295        td = datetime.datetime.now() - self.last_check_connection 
    7396        if td > self.check_every: 
    74             print "Checking..." 
    7597            self.create_socket() 
    7698            self.last_check_connection = datetime.datetime.now() 
    7799            if self.s: 
    78                 print "We have a new connection!" 
     100                self.f.close() 
     101                self.f = None 
    79102 
    80103    def send(self): 
     
    90113    import optparse 
    91114    p = optparse.OptionParser(usage="usage: %prog ip port [options]") 
    92     p.add_option("-b", "--backupport", dest="backupport", type="int", action="store", help="Recovery port", default=None) 
     115    p.add_option("-b", "--backuppath", dest="backuppath", type="string", action="store", help="Directory where recovery logs should be stored if the network connection dies", default=None) 
    93116    (options, args) = p.parse_args() 
    94117    if len(args) != 2 or intify(args[1], None) is None: 
     
    97120        sys.stderr.flush() 
    98121        return 1 
    99     S = Sender(args[0], intify(args[1], None), options.backupport) 
     122    S = Sender(args[0], intify(args[1], None), options.backuppath) 
    100123    S.send() 
    101124