Show
Ignore:
Timestamp:
11/13/03 00:29:24 (20 years ago)
Author:
jalet
Message:

More work on new backend. This commit may be unstable.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/cupspykota

    r1178 r1180  
    2424# 
    2525# $Log$ 
     26# Revision 1.3  2003/11/12 23:27:44  jalet 
     27# More work on new backend. This commit may be unstable. 
     28# 
    2629# Revision 1.2  2003/11/12 09:33:34  jalet 
    2730# New CUPS backend supports device enumeration 
     
    120123        # we let the job pass through, but log a warning message 
    121124        thebackend.logger.log_message(_("Printer %s not registered in the PyKota system") % thebackend.printername, "warn") 
     125        action = "ALLOW" 
    122126    else :     
    123127        for dummy in range(2) : 
     
    157161                break     
    158162                     
    159         # if user exists, do accounting 
    160163        if user.Exists : 
    161164            # Is the current user allowed to print at all ? 
     
    168171            return 0 
    169172         
    170     MAXTRIES = 12    # maximum number of tries to get the printer's internal page counter 
    171     TIMETOSLEEP = 10 # number of seconds to sleep between two tries to get the printer's internal page counter 
    172     requester = openRequester(thebackend.config, thebackend.printername) 
    173     def getPPC(requester, backend) : 
    174         for i in range(MAXTRIES) : 
    175             try : 
    176                 counterbeforejob = requester.getPrinterPageCounter(backend.printerhostname) 
    177             except PyKotaRequesterError, msg : 
    178                 # can't get actual page counter, assume printer is off or warming up 
    179                 # log the message anyway. 
    180                 backend.logger.log_message("%s" % msg, "warn") 
    181                 counterbeforejob = None 
    182             else :     
    183                 # printer answered, it is on so we can exit the loop 
    184                 break 
    185             time.sleep(TIMETOSLEEP)     
    186         return counterbeforejob     
    187          
    188173    if action not in ["ALLOW", "WARN"] :     
    189174        # if not allowed to print then die, else proceed. 
     
    193178    else : 
    194179        # pass the job untouched to the underlying layer 
    195         # but get printer page counter before and after 
    196         # print job is submitted to the hardware. 
    197          
    198         # get page counter before job 
    199         before = getPPC(requester, thebackend) 
    200          
    201         # executes backend 
    202         # TODO : use correct original backend. 
     180        # and starts accounting at the same time 
     181        thebackend.accounter.beginJob(printer, user) 
     182         
     183        # executes original backend 
     184        #mustclose = 0     
     185        #if thebackend.inputfile is not None :     
     186        #    if hasattr(thebackend.inputfile, "read") : 
     187        #        infile = thebackend.inputfile 
     188        #    else :     
     189        #        infile = open(thebackend.inputfile, "rb") 
     190        #    mustclose = 1 
     191        #else :     
     192        #    infile = sys.stdin 
     193             
    203194        realbackend = os.path.join(os.path.split(sys.argv[0])[0], thebackend.originalbackend) 
    204195        retcode = os.spawnve(os.P_WAIT, realbackend, [os.environ["DEVICE_URI"]] + sys.argv[1:], os.environ) 
    205196     
    206     # get page counter after job 
    207     after = getPPC(requester, thebackend) 
    208      
    209     # Computes the last job size as the difference between internal page 
    210     # counter in the printer and last page counter taken from the Quota 
    211     # Storage database for this particular printer 
    212     try : 
    213         jobsize = (after - before)     
    214     except :     
    215         jobsize = 0 
    216          
     197    # stops accounting.  
     198    thebackend.accounter.endJob(printer, user) 
     199         
     200    # retrieve the job size     
     201    jobsize = thebackend.accounter.getJobSize() 
     202     
    217203    # update the quota for the current user on this printer  
    218204    if printer.Exists : 
     
    223209         
    224210        # adds the current job to history     
    225         printer.addJobToHistory(thebackend.jobid, user, after, action, jobsize) 
     211        printer.addJobToHistory(thebackend.jobid, user, thebackend.accounter.getLastPageCounter(), action, jobsize) 
    226212     
    227213    return retcode 
     
    242228            if status is None : 
    243229                for d in devices : 
     230                    # each line is of the form : 'xxxx xxxx "xxxx xxx" "xxxx xxx"' 
     231                    # so we have to decompose it carefully 
    244232                    fdevice = cStringIO.StringIO("%s" % d) 
    245233                    tokenizer = shlex.shlex(fdevice) 
     
    253241                            break 
    254242                    fdevice.close() 
    255                     (devicetype, device, name, fullname) = arguments 
    256                     if name.startswith('"') and name.endswith('"') : 
    257                         name = name[1:-1] 
    258                     if fullname.startswith('"') and fullname.endswith('"') : 
    259                         fullname = fullname[1:-1] 
    260                     print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname)     
     243                    try : 
     244                        (devicetype, device, name, fullname) = arguments 
     245                    except ValueError :     
     246                        pass    # ignore this 'bizarre' device 
     247                    else :     
     248                        if name.startswith('"') and name.endswith('"') : 
     249                            name = name[1:-1] 
     250                        if fullname.startswith('"') and fullname.endswith('"') : 
     251                            fullname = fullname[1:-1] 
     252                        print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname)     
    261253        retcode = 0 
    262254    elif len(sys.argv) not in (6, 7) :