Changeset 2164

Show
Ignore:
Timestamp:
03/11/05 08:21:03 (19 years ago)
Author:
jerome
Message:

Avoids an infinite loop when several different backend wrappers try to
autodetect each other.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/cupspykota

    r2147 r2164  
    2727import sys 
    2828import os 
     29import errno 
     30import tempfile 
    2931import popen2 
    3032import cStringIO 
     
    638640    retcode = 0 
    639641    if len(sys.argv) == 1 : 
    640         # we will execute each existing backend in device enumeration mode 
    641         # and generate their PyKota accounting counterpart 
    642642        (directory, myname) = os.path.split(sys.argv[0]) 
    643         for backend in [os.path.join(directory, b) for b in os.listdir(directory) if os.path.isfile(os.path.join(directory, b)) and (b != myname)] : 
    644             answer = os.popen(backend, "r") 
     643        tmpdir = tempfile.gettempdir() 
     644        lockfilename = os.path.join(tmpdir, "%s..LCK" % myname) 
     645        if os.path.exists(lockfilename) : 
     646            # there's already a lockfile, see if still used 
     647            lockfile = open(lockfilename, "r") 
     648            pid = int(lockfile.read()) 
     649            lockfile.close() 
    645650            try : 
    646                 devices = [line.strip() for line in answer.readlines()] 
    647             except :     
    648                 devices = [] 
    649             status = answer.close() 
    650             if status is None : 
    651                 for d in devices : 
    652                     # each line is of the form : 'xxxx xxxx "xxxx xxx" "xxxx xxx"' 
    653                     # so we have to decompose it carefully 
    654                     fdevice = cStringIO.StringIO("%s" % d) 
    655                     tokenizer = shlex.shlex(fdevice) 
    656                     tokenizer.wordchars = tokenizer.wordchars + r".:,?!~/\_$*-+={}[]()#" 
    657                     arguments = [] 
    658                     while 1 : 
    659                         token = tokenizer.get_token() 
    660                         if token : 
    661                             arguments.append(token) 
    662                         else : 
    663                             break 
    664                     fdevice.close() 
    665                     try : 
    666                         (devicetype, device, name, fullname) = arguments 
    667                     except ValueError :     
    668                         pass    # ignore this 'bizarre' device 
    669                     else :     
    670                         if name.startswith('"') and name.endswith('"') : 
    671                             name = name[1:-1] 
    672                         if fullname.startswith('"') and fullname.endswith('"') : 
    673                             fullname = fullname[1:-1] 
    674                         print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname) 
     651                # see if the pid contained in the lock file is still running 
     652                os.kill(pid, 0) 
     653            except OSError, e :     
     654                if e.errno != errno.EPERM : 
     655                    # process doesn't exist anymore, remove the lock 
     656                    os.remove(lockfilename) 
     657             
     658        if not os.path.exists(lockfilename) : 
     659            lockfile = open(lockfilename, "w") 
     660            lockfile.write("%i" % os.getpid()) 
     661            lockfile.close() 
     662            # we will execute each existing backend in device enumeration mode 
     663            # and generate their PyKota accounting counterpart 
     664            for backend in [os.path.join(directory, b) for b in os.listdir(directory) if os.path.isfile(os.path.join(directory, b)) and (b != myname)] : 
     665                answer = os.popen(backend, "r") 
     666                try : 
     667                    devices = [line.strip() for line in answer.readlines()] 
     668                except :     
     669                    devices = [] 
     670                status = answer.close() 
     671                if status is None : 
     672                    for d in devices : 
     673                        # each line is of the form : 'xxxx xxxx "xxxx xxx" "xxxx xxx"' 
     674                        # so we have to decompose it carefully 
     675                        fdevice = cStringIO.StringIO("%s" % d) 
     676                        tokenizer = shlex.shlex(fdevice) 
     677                        tokenizer.wordchars = tokenizer.wordchars + r".:,?!~/\_$*-+={}[]()#" 
     678                        arguments = [] 
     679                        while 1 : 
     680                            token = tokenizer.get_token() 
     681                            if token : 
     682                                arguments.append(token) 
     683                            else : 
     684                                break 
     685                        fdevice.close() 
     686                        try : 
     687                            (devicetype, device, name, fullname) = arguments 
     688                        except ValueError :     
     689                            pass    # ignore this 'bizarre' device 
     690                        else :     
     691                            if name.startswith('"') and name.endswith('"') : 
     692                                name = name[1:-1] 
     693                            if fullname.startswith('"') and fullname.endswith('"') : 
     694                                fullname = fullname[1:-1] 
     695                            print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname) 
     696            os.remove(lockfilename) 
    675697        retcode = 0                 
    676698    elif len(sys.argv) not in (6, 7) :