Show
Ignore:
Timestamp:
11/28/07 18:28:30 (16 years ago)
Author:
jerome
Message:

Now the presence of executable dependencies is tested at runtime.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pkpgcounter/trunk/pkpgpdls/pdlparser.py

    r522 r527  
    2323import sys 
    2424import os 
    25 import popen2 
    2625 
    2726KILOBYTE = 1024     
     
    4140class PDLParser : 
    4241    """Generic PDL parser.""" 
    43     totiffcommands = None        # Default command to convert to TIFF 
    44     openmode = "rb"              # Default file opening mode 
     42    totiffcommands = None       # Default command to convert to TIFF 
     43    required = []               # Default list of required commands 
     44    openmode = "rb"             # Default file opening mode 
    4545    def __init__(self, parent, (firstblock, lastblock)) : 
    4646        """Initialize the generic parser.""" 
     
    7171            self.infile.close() 
    7272             
     73    def findExecutable(self, command) : 
     74        """Finds an executable in the PATH and returns True if found else False.""" 
     75        for cmd in [p.strip() for p in command.split("|")] : # | can separate alternatives for similar commands (e.g. a2ps|enscript) 
     76            for path in os.environ.get("PATH", "").split(":") : 
     77                fullname = os.path.abspath(os.path.join(os.path.expanduser(path), cmd)) 
     78                if os.path.isfile(fullname) and os.access(fullname, os.X_OK) : 
     79                    return True 
     80        return False 
     81         
     82    def isMissing(self, commands) :     
     83        """Returns True if some required commands are missing, else False."""  
     84        howmanythere = 0 
     85        for command in commands : 
     86            if not self.findExecutable(command) : 
     87                sys.stderr.write("ERROR: %(command)s is missing or not executable. You MUST install it for pkpgcounter to be able to do what you want.\n" % locals()) 
     88                sys.stderr.flush() 
     89            else :     
     90                howmanythere += 1 
     91        if howmanythere == len(commands) : 
     92            return False 
     93        else :    
     94            return True 
     95         
    7396    def logdebug(self, message) :        
    7497        """Logs a debug message if needed.""" 
     
    89112        """    
    90113        if self.totiffcommands : 
     114            if self.isMissing(self.required) : 
     115                raise PDLParserError, "At least one of the following commands is missing and should be installed for the computation of ink coverage : %s" % repr(self.required) 
    91116            infname = self.filename 
    92117            for totiffcommand in self.totiffcommands :