home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-cupshelpers / cupshelpers / cupshelpers.py < prev    next >
Encoding:
Python Source  |  2009-05-05  |  25.4 KB  |  771 lines

  1. ## system-config-printer
  2.  
  3. ## Copyright (C) 2006, 2007, 2008, 2009 Red Hat, Inc.
  4. ## Copyright (C) 2006 Florian Festi <ffesti@redhat.com>
  5. ## Copyright (C) 2006, 2007, 2008, 2009 Tim Waugh <twaugh@redhat.com>
  6.  
  7. ## This program is free software; you can redistribute it and/or modify
  8. ## it under the terms of the GNU General Public License as published by
  9. ## the Free Software Foundation; either version 2 of the License, or
  10. ## (at your option) any later version.
  11.  
  12. ## This program is distributed in the hope that it will be useful,
  13. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ## GNU General Public License for more details.
  16.  
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with this program; if not, write to the Free Software
  19. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. import cups, pprint, os, tempfile, re, string
  22. import locale
  23. from . import _debugprint
  24.  
  25. class Printer:
  26.     _flags_blacklist = ["options", "local"]
  27.  
  28.     def __init__(self, name, connection, **kw):
  29.         """
  30.         @param name: printer name
  31.         @type name: string
  32.         @param connection: CUPS connection
  33.         @type connection: CUPS.Connection object
  34.         @param kw: printer attributes
  35.         @type kw: dict indexed by string
  36.         """
  37.         self.name = name
  38.         self.connection = connection
  39.         self.class_members = []
  40.         self.update (**kw)
  41.         self._ppd = None # load on demand
  42.  
  43.     def __del__ (self):
  44.         if self._ppd != None:
  45.             os.unlink(self._ppd)
  46.  
  47.     def __repr__ (self):
  48.         return "<cupshelpers.Printer \"%s\">" % self.name
  49.  
  50.     def _expand_flags(self):
  51.  
  52.         def _ascii_lower(str):
  53.             return str.translate(string.maketrans(string.ascii_uppercase,
  54.                                                   string.ascii_lowercase));
  55.  
  56.         prefix = "CUPS_PRINTER_"
  57.         prefix_length = len(prefix)
  58.  
  59.         # loop over cups constants
  60.         for name in cups.__dict__:
  61.             if name.startswith(prefix):
  62.                 attr_name = \
  63.                     _ascii_lower(name[prefix_length:])
  64.                 if attr_name in self._flags_blacklist: continue
  65.                 if attr_name == "class": attr_name = "is_class"
  66.                 # set as attribute
  67.                 setattr(self, attr_name,
  68.                         bool(self.type & getattr(cups, name)))
  69.  
  70.     def update(self, **kw):
  71.         """
  72.         Update object from printer attributes.
  73.  
  74.         @param kw: printer attributes
  75.         @type kw: dict indexed by string
  76.         """
  77.         self.state = kw.get('printer-state', 0)
  78.         self.enabled = self.state != cups.IPP_PRINTER_STOPPED
  79.         self.device_uri = kw.get('device-uri', "")
  80.         self.info = kw.get('printer-info', "")
  81.         self.is_shared = kw.get('printer-is-shared', None)
  82.         self.location = kw.get('printer-location', "")
  83.         self.make_and_model = kw.get('printer-make-and-model', "")
  84.         self.type = kw.get('printer-type', 0)
  85.         self.uri_supported = kw.get('printer-uri-supported', "")
  86.         if type (self.uri_supported) != list:
  87.             self.uri_supported = [self.uri_supported]
  88.         self._expand_flags()
  89.         if self.is_shared is None:
  90.             self.is_shared = not self.not_shared
  91.         del self.not_shared
  92.  
  93.     def getAttributes(self):
  94.         """
  95.         Fetch further attributes for the printer.
  96.  
  97.         Normally only a small set of attributes is fetched.  This
  98.         method is for fetching more.
  99.         """
  100.         attrs = self.connection.getPrinterAttributes(self.name)
  101.         self.attributes = {}
  102.         self.other_attributes = {}
  103.         self.possible_attributes = {
  104.             'landscape' : ('False', ['True', 'False']),
  105.             'page-border' : ('none', ['none', 'single', 'single-thick',
  106.                                      'double', 'double-thick']),
  107.             }
  108.  
  109.         for key, value in attrs.iteritems():
  110.             if key.endswith("-default"):
  111.                 name = key[:-len("-default")]
  112.                 if name in ["job-sheets", "printer-error-policy",
  113.                             "printer-op-policy", # handled below
  114.                             "notify-events", # cannot be set
  115.                             "document-format", # cannot be set
  116.                             "notify-lease-duration"]: # cannot be set
  117.                     continue 
  118.  
  119.                 supported = attrs.get(name + "-supported", None) or \
  120.                             self.possible_attributes.get(name, None) or \
  121.                             ""
  122.  
  123.                 # Convert a list into a comma-separated string, since
  124.                 # it can only really have been misinterpreted as a list
  125.                 # by CUPS.
  126.                 if isinstance (value, list):
  127.                     value = reduce (lambda x, y: x+','+y, value)
  128.  
  129.                 self.attributes[name] = value
  130.                     
  131.                 if attrs.has_key(name+"-supported"):
  132.                     supported = attrs[name+"-supported"]
  133.                     self.possible_attributes[name] = (value, supported)
  134.             elif (not key.endswith ("-supported") and
  135.                   key != 'job-sheets-default' and
  136.                   key != 'printer-error-policy' and
  137.                   key != 'printer-op-policy' and
  138.                   not key.startswith ('requesting-user-name-')):
  139.                 self.other_attributes[key] = value
  140.         
  141.         self.job_sheet_start, self.job_sheet_end = attrs.get(
  142.             'job-sheets-default', ('none', 'none'))
  143.         self.job_sheets_supported = attrs.get('job-sheets-supported', ['none'])
  144.         self.error_policy = attrs.get('printer-error-policy', 'none')
  145.         self.error_policy_supported = attrs.get(
  146.             'printer-error-policy-supported', ['none'])
  147.         self.op_policy = attrs.get('printer-op-policy', "") or "default"
  148.         self.op_policy_supported = attrs.get(
  149.             'printer-op-policy-supported', ["default"])
  150.  
  151.         self.default_allow = True
  152.         self.except_users = []
  153.         if attrs.has_key('requesting-user-name-allowed'):
  154.             self.except_users = attrs['requesting-user-name-allowed']
  155.             self.default_allow = False
  156.         elif attrs.has_key('requesting-user-name-denied'):
  157.             self.except_users = attrs['requesting-user-name-denied']
  158.         self.except_users_string = ', '.join(self.except_users)
  159.         if (attrs.has_key ('device-uri') and
  160.             attrs['device-uri'].startswith ("smb:")):
  161.             # Don't update device-uri for smb printers as we need to have
  162.             # the original from printers.conf, in case it has authentication
  163.             # details in it.
  164.             attrs['device-uri'] = self.device_uri
  165.         self.update (**attrs)
  166.  
  167.     def getServer(self):
  168.         """
  169.         Find out which server defines this printer.
  170.  
  171.         @returns: server URI or None
  172.         """
  173.         if not self.uri_supported[0].startswith('ipp://'):
  174.             return None
  175.         uri = self.uri_supported[0][6:]
  176.         uri = uri.split('/')[0]
  177.         uri = uri.split(':')[0]
  178.         if uri == "localhost.localdomain":
  179.             uri = "localhost"
  180.         return uri
  181.  
  182.     def getPPD(self):
  183.         """
  184.         Obtain the printer's PPD.
  185.  
  186.         @returns: cups.PPD object, or False for raw queues
  187.         @raise cups.IPPError: IPP error
  188.         """
  189.         result = None
  190.         if self._ppd is None:
  191.             try:
  192.                 self._ppd = self.connection.getPPD(self.name)
  193.                 result = cups.PPD (self._ppd)
  194.             except cups.IPPError, (e, m):
  195.                 if e == cups.IPP_NOT_FOUND:
  196.                     result = False
  197.                 else:
  198.                     raise
  199.  
  200.         if result != False and self._ppd != None:
  201.             result = cups.PPD (self._ppd)
  202.  
  203.         return result
  204.  
  205.     def setOption(self, name, value):
  206.         """
  207.         Set a printer's option.
  208.  
  209.         @param name: option name
  210.         @type name: string
  211.         @param value: option value
  212.         @type value: option-specific
  213.         """
  214.         if isinstance (value, float):
  215.             radixchar = locale.nl_langinfo (locale.RADIXCHAR)
  216.             if radixchar != '.':
  217.                 # Convert floats to strings, being careful with decimal points.
  218.                 value = str (value).replace (radixchar, '.')
  219.         self.connection.addPrinterOptionDefault(self.name, name, value)
  220.  
  221.     def unsetOption(self, name):
  222.         """
  223.         Unset a printer's option.
  224.  
  225.         @param name: option name
  226.         @type name: string
  227.         """
  228.         self.connection.deletePrinterOptionDefault(self.name, name)
  229.  
  230.     def setEnabled(self, on, reason=None):
  231.         """
  232.         Set the printer's enabled state.
  233.  
  234.         @param on: whether it will be enabled
  235.         @type on: bool
  236.         @param reason: reason for this state
  237.         @type reason: string
  238.         """
  239.         if on:
  240.             self.connection.enablePrinter(self.name)
  241.         else:
  242.             if reason:
  243.                 self.connection.disablePrinter(self.name, reason=reason)
  244.             else:
  245.                 self.connection.disablePrinter(self.name)
  246.  
  247.     def setAccepting(self, on, reason=None):
  248.         """
  249.         Set the printer's accepting state.
  250.  
  251.         @param on: whether it will be accepting
  252.         @type on: bool
  253.         @param reason: reason for this state
  254.         @type reason: string
  255.         """
  256.         if on:
  257.             self.connection.acceptJobs(self.name)
  258.         else:
  259.             if reason:
  260.                 self.connection.rejectJobs(self.name, reason=reason)
  261.             else:
  262.                 self.connection.rejectJobs(self.name)
  263.  
  264.     def setShared(self,on):
  265.         """
  266.         Set the printer's shared state.
  267.  
  268.         @param on: whether it will be accepting
  269.         @type on: bool
  270.         """
  271.         self.connection.setPrinterShared(self.name, on)
  272.  
  273.     def setErrorPolicy (self, policy):
  274.         """
  275.         Set the printer's error policy.
  276.  
  277.         @param policy: error policy
  278.         @type policy: string
  279.         """
  280.         self.connection.setPrinterErrorPolicy(self.name, policy)
  281.  
  282.     def setOperationPolicy(self, policy):
  283.         """
  284.         Set the printer's operation policy.
  285.  
  286.         @param policy: operation policy
  287.         @type policy: string
  288.         """
  289.         self.connection.setPrinterOpPolicy(self.name, policy)    
  290.  
  291.     def setJobSheets(self, start, end):
  292.         """
  293.         Set the printer's job sheets.
  294.  
  295.         @param start: start sheet
  296.         @type start: string
  297.         @param end: end sheet
  298.         @type end: string
  299.         """
  300.         self.connection.setPrinterJobSheets(self.name, start, end)
  301.  
  302.     def setAccess(self, allow, except_users):
  303.         """
  304.         Set access control list.
  305.  
  306.         @param allow: whether to allow by default, otherwise deny
  307.         @type allow: bool
  308.         @param except_users: exception list
  309.         @type except_users: string list
  310.         """
  311.         if isinstance(except_users, str):
  312.             users = except_users.split()
  313.             users = [u.split(",") for u in users]
  314.             except_users = []
  315.             for u in users:
  316.                 except_users.extend(u)
  317.             except_users = [u.strip() for u in except_users]
  318.             except_users = filter(None, except_users)
  319.             
  320.         if allow:
  321.             self.connection.setPrinterUsersDenied(self.name, except_users)
  322.         else:
  323.             self.connection.setPrinterUsersAllowed(self.name, except_users)
  324.  
  325.     def jobsQueued(self, only_tests=False):
  326.         """
  327.         Find out whether jobs are queued for this printer.
  328.  
  329.         @param only_tests: whether to restrict search to test pages
  330.         @type only_tests: bool
  331.         @returns: list of job IDs
  332.         """
  333.         ret = []
  334.         try:
  335.             jobs = self.connection.getJobs ()
  336.         except cups.IPPError:
  337.             return ret
  338.  
  339.         for id, attrs in jobs.iteritems():
  340.             try:
  341.                 uri = attrs['job-printer-uri']
  342.                 uri = uri[uri.rindex ('/') + 1:]
  343.             except:
  344.                 continue
  345.             if uri != self.name:
  346.                 continue
  347.  
  348.             if (not only_tests or
  349.                 (attrs.has_key ('job-name') and
  350.                  attrs['job-name'] == 'Test Page')):
  351.                 ret.append (id)
  352.         return ret
  353.  
  354.     def testsQueued(self):
  355.         """
  356.         Find out whether test jobs are queued for this printer.
  357.  
  358.         @returns: list of job IDs
  359.         """
  360.         return self.jobsQueued (only_tests=True)
  361.  
  362.     def setAsDefault(self):
  363.         """
  364.         Set this printer as the system default.
  365.         """
  366.         self.connection.setDefault(self.name)
  367.  
  368.         # Also need to check system-wide lpoptions because that's how
  369.         # previous Fedora versions set the default (bug #217395).
  370.         (tmpfd, tmpfname) = tempfile.mkstemp ()
  371.         os.remove (tmpfname)
  372.         try:
  373.             resource = "/admin/conf/lpoptions"
  374.             self.connection.getFile(resource, fd=tmpfd)
  375.         except cups.HTTPError, (s,):
  376.             if s == cups.HTTP_NOT_FOUND:
  377.                 return False
  378.  
  379.             raise cups.HTTPError (s)
  380.  
  381.         f = os.fdopen (tmpfd, 'r+')
  382.         f.seek (0)
  383.         lines = f.readlines ()
  384.         changed = False
  385.         i = 0
  386.         for line in lines:
  387.             if line.startswith ("Default "):
  388.                 # This is the system-wide default.
  389.                 name = line.split (' ')[1]
  390.                 if name != self.name:
  391.                     # Stop it from over-riding the server default.
  392.                     lines[i] = "Dest " + line[8:]
  393.                     changed = True
  394.                 i += 1
  395.  
  396.         if changed:
  397.             f.seek (0)
  398.             f.writelines (lines)
  399.             f.truncate ()
  400.             os.lseek (tmpfd, 0, os.SEEK_SET)
  401.             try:
  402.                 self.connection.putFile (resource, fd=tmpfd)
  403.             except cups.HTTPError, (s,):
  404.                 return False
  405.  
  406.         return changed
  407.  
  408. def getPrinters(connection):
  409.     """
  410.     Obtain a list of printers.
  411.  
  412.     @param connection: CUPS connection
  413.     @type connection: CUPS.Connection object
  414.     @returns: L{Printer} list
  415.     """
  416.     printers = connection.getPrinters()
  417.     classes = connection.getClasses()
  418.     for name, printer in printers.iteritems():
  419.         printer = Printer(name, connection, **printer)
  420.         printers[name] = printer
  421.         if classes.has_key(name):
  422.             printer.class_members = classes[name]
  423.             printer.class_members.sort()
  424.     return printers
  425.  
  426. def parseDeviceID (id):
  427.     """
  428.     Parse an IEEE 1284 Device ID, so that it may be indexed by field name.
  429.  
  430.     @param id: IEEE 1284 Device ID, without the two leading length bytes
  431.     @type id: string
  432.     @returns: dict indexed by field name
  433.     """
  434.     id_dict = {}
  435.     pieces = id.split(";")
  436.     for piece in pieces:
  437.         if piece.find(":") == -1:
  438.             continue
  439.         name, value = piece.split(":",1)
  440.         id_dict[name] = value
  441.     if id_dict.has_key ("MANUFACTURER"):
  442.         id_dict.setdefault("MFG", id_dict["MANUFACTURER"])
  443.     if id_dict.has_key ("MODEL"):
  444.         id_dict.setdefault("MDL", id_dict["MODEL"])
  445.     if id_dict.has_key ("COMMAND SET"):
  446.         id_dict.setdefault("CMD", id_dict["COMMAND SET"])
  447.     for name in ["MFG", "MDL", "CMD", "CLS", "DES", "SN", "S", "P", "J"]:
  448.         id_dict.setdefault(name, "")
  449.     id_dict["CMD"] = id_dict["CMD"].split(',') 
  450.     return id_dict
  451.  
  452. class Device:
  453.     """
  454.     This class represents a CUPS device.
  455.     """
  456.  
  457.     def __init__(self, uri, **kw):
  458.         """
  459.         @param uri: device URI
  460.         @type uri: string
  461.         @param kw: device attributes
  462.         @type kw: dict
  463.         """
  464.         self.uri = uri
  465.         self.device_class = kw.get('device-class', 'Unknown') # XXX better default
  466.         self.info = kw.get('device-info', '')
  467.         self.make_and_model = kw.get('device-make-and-model', 'Unknown')
  468.         self.id = kw.get('device-id', '')
  469.  
  470.         uri_pieces = uri.split(":")
  471.         self.type =  uri_pieces[0]
  472.         self.is_class = len(uri_pieces)==1
  473.  
  474.         #self.id = 'MFG:HEWLETT-PACKARD;MDL:DESKJET 990C;CMD:MLC,PCL,PML;CLS:PRINTER;DES:Hewlett-Packard DeskJet 990C;SN:US05N1J00XLG;S:00808880800010032C1000000C2000000;P:0800,FL,B0;J:                    ;'
  475.  
  476.         self.id_dict = parseDeviceID (self.id)
  477.  
  478.     def __repr__ (self):
  479.         return "<cupshelpers.Device \"%s\">" % self.uri
  480.  
  481.     def __cmp__(self, other):
  482.         """
  483.         Compare devices by order of preference.
  484.         """
  485.         if other == None:
  486.             return -1
  487.  
  488.         if self.is_class != other.is_class:
  489.             if other.is_class:
  490.                 return -1
  491.             return 1
  492.         if not self.is_class and (self.type != other.type):
  493.             # "hp"/"hpfax" before "usb" before * before "parallel" before
  494.             # "serial"
  495.             if other.type == "serial":
  496.                 return -1
  497.             if self.type == "serial":
  498.                 return 1
  499.             if other.type == "parallel":
  500.                 return -1
  501.             if self.type == "parallel":
  502.                 return 1
  503.             if other.type == "hp" or other.type == "hpfax":
  504.                 return 1
  505.             if self.type == "hp" or self.type == "hpfax":
  506.                 return -1
  507.             if other.type == "usb":
  508.                 return 1
  509.             if self.type == "usb":
  510.                 return -1
  511.         result = cmp(bool(self.id), bool(other.id))
  512.         if not result:
  513.             result = cmp(self.info, other.info)
  514.         
  515.         return result
  516.  
  517. def getDevices(connection):
  518.     """
  519.     Obtain a list of available CUPS devices.
  520.  
  521.     @param connection: CUPS connection
  522.     @type connection: cups.Connection object
  523.     @returns: a list of L{Device} objects
  524.     @raise cups.IPPError: IPP Error
  525.     """
  526.     devices = connection.getDevices()
  527.     for uri, data in devices.iteritems():
  528.         device = Device(uri, **data)
  529.         devices[uri] = device
  530.         if device.info != 'Unknown' and device.make_and_model == 'Unknown':
  531.             device.make_and_model = device.info
  532.     return devices
  533.  
  534. def activateNewPrinter(connection, name):
  535.     """
  536.     Set a new printer enabled, accepting jobs, and (if necessary) the
  537.     default printer.
  538.  
  539.     @param connection: CUPS connection
  540.     @type connection: cups.Connection object
  541.     @param name: printer name
  542.     @type name: string
  543.     @raise cups.IPPError: IPP error
  544.     """
  545.     connection.enablePrinter (name)
  546.     connection.acceptJobs (name)
  547.  
  548.     # Set as the default if there is not already a default printer.
  549.     if connection.getDefault () == None:
  550.         connection.setDefault (name)
  551.  
  552. def copyPPDOptions(ppd1, ppd2):
  553.     """
  554.     Copy default options between PPDs.
  555.  
  556.     @param ppd1: source PPD
  557.     @type ppd1: cups.PPD object
  558.     @param ppd2: destination PPD
  559.     @type ppd2: cups.PPD object
  560.     """
  561.     def getPPDGroupOptions(group):
  562.         options = group.options[:]
  563.         for g in group.subgroups:
  564.             options.extend(getPPDGroupOptions(g))
  565.         return options
  566.  
  567.     def iteratePPDOptions(ppd):
  568.         for group in ppd.optionGroups:
  569.             for option in getPPDGroupOptions(group):
  570.                 yield option
  571.  
  572.     for option in iteratePPDOptions(ppd1):
  573.         if option.keyword == "PageRegion":
  574.             continue
  575.         new_option = ppd2.findOption(option.keyword)
  576.         if new_option and option.ui==new_option.ui:
  577.             value = option.defchoice
  578.             for choice in new_option.choices:
  579.                 if choice["choice"]==value:
  580.                     ppd2.markOption(new_option.keyword, value)
  581.                     _debugprint ("set %s = %s" % (new_option.keyword, value))
  582.                     
  583. def setPPDPageSize(ppd, language):
  584.     """
  585.     Set the PPD page size according to locale.
  586.  
  587.     @param ppd: PPD
  588.     @type ppd: cups.PPD object
  589.     @param language: language, as given by the first element of
  590.     locale.setlocale
  591.     @type language: string
  592.     """
  593.     # Just set the page size to A4 or Letter, that's all.
  594.     # Use the same method CUPS uses.
  595.     size = 'A4'
  596.     letter = [ 'C', 'POSIX', 'en', 'en_US', 'en_CA', 'fr_CA' ]
  597.     for each in letter:
  598.         if language == each:
  599.             size = 'Letter'
  600.     try:
  601.         ppd.markOption ('PageSize', size)
  602.         _debugprint ("set PageSize = %s" % size)
  603.     except:
  604.         _debugprint ("Failed to set PageSize (%s not available?)" % size)
  605.  
  606. def missingPackagesAndExecutables(ppd):
  607.     """
  608.     Check that all relevant executables for a PPD are installed.
  609.  
  610.     @param ppd: PPD
  611.     @type ppd: cups.PPD object
  612.     @returns: string list pair, representing missing packages and
  613.     missing executables
  614.     """
  615.  
  616.     # First, a local function.  How to check that something exists
  617.     # in a path:
  618.     def pathcheck (name, path="/usr/bin:/bin"):
  619.         # Strip out foomatic '%'-style place-holders.
  620.         p = name.find ('%')
  621.         if p != -1:
  622.             name = name[:p]
  623.         if len (name) == 0:
  624.             return "true"
  625.         if name[0] == '/':
  626.             if os.access (name, os.X_OK):
  627.                 _debugprint ("%s: found" % name)
  628.                 return name
  629.             else:
  630.                 _debugprint ("%s: NOT found" % name)
  631.                 return None
  632.         if name.find ("=") != -1:
  633.             return "builtin"
  634.         if name in [ ":", ".", "[", "alias", "bind", "break", "cd",
  635.                      "continue", "declare", "echo", "else", "eval",
  636.                      "exec", "exit", "export", "fi", "if", "kill", "let",
  637.                      "local", "popd", "printf", "pushd", "pwd", "read",
  638.                      "readonly", "set", "shift", "shopt", "source",
  639.                      "test", "then", "trap", "type", "ulimit", "umask",
  640.                      "unalias", "unset", "wait" ]:
  641.             return "builtin"
  642.         for component in path.split (':'):
  643.             file = component.rstrip (os.path.sep) + os.path.sep + name
  644.             if os.access (file, os.X_OK):
  645.                 _debugprint ("%s: found" % file)
  646.                 return file
  647.         _debugprint ("%s: NOT found in %s" % (name,path))
  648.         return None
  649.  
  650.     pkgs_to_install = []
  651.     exes_to_install = []
  652.  
  653.     # Find a 'FoomaticRIPCommandLine' attribute.
  654.     exe = exepath = None
  655.     attr = ppd.findAttr ('FoomaticRIPCommandLine')
  656.     if attr:
  657.         # Foomatic RIP command line to check.
  658.         cmdline = attr.value.replace ('&&\n', '')
  659.         cmdline = cmdline.replace ('"', '"')
  660.         cmdline = cmdline.replace ('<', '<')
  661.         cmdline = cmdline.replace ('>', '>')
  662.         if (cmdline.find ("(") != -1 or
  663.             cmdline.find ("&") != -1):
  664.             # Don't try to handle sub-shells or unreplaced HTML entities.
  665.             cmdline = ""
  666.  
  667.         # Strip out foomatic '%'-style place-holders
  668.         pipes = cmdline.split (';')
  669.         for pipe in pipes:
  670.             cmds = pipe.strip ().split ('|')
  671.             for cmd in cmds:
  672.                 args = cmd.strip ().split (' ')
  673.                 exe = args[0]
  674.                 exepath = pathcheck (exe)
  675.                 if not exepath:
  676.                     break
  677.  
  678.                 # Main executable found.  But if it's 'gs',
  679.                 # perhaps there is an IJS server we also need
  680.                 # to check.
  681.                 if os.path.basename (exepath) == 'gs':
  682.                     argn = len (args)
  683.                     argi = 1
  684.                     search = "-sIjsServer="
  685.                     while argi < argn:
  686.                         arg = args[argi]
  687.                         if arg.startswith (search):
  688.                             exe = arg[len (search):]
  689.                             exepath = pathcheck (exe)
  690.                             break
  691.  
  692.                         argi += 1
  693.  
  694.             if not exepath:
  695.                 # Next pipe.
  696.                 break
  697.  
  698.     if exepath or not exe:
  699.         # Look for '*cupsFilter' lines in the PPD and check that
  700.         # the filters are installed.
  701.         (tmpfd, tmpfname) = tempfile.mkstemp ()
  702.         os.unlink (tmpfname)
  703.         ppd.writeFd (tmpfd)
  704.         os.lseek (tmpfd, 0, os.SEEK_SET)
  705.         f = os.fdopen (tmpfd, "r")
  706.         search = "*cupsFilter:"
  707.         for line in f.readlines ():
  708.             if line.startswith (search):
  709.                 line = line[len (search):].strip ().strip ('"')
  710.                 try:
  711.                     (mimetype, cost, exe) = line.split (' ')
  712.                 except:
  713.                     continue
  714.  
  715.                 exepath = pathcheck (exe,
  716.                                      "/usr/lib/cups/filter:"
  717.                                      "/usr/lib64/cups/filter")
  718.  
  719.     if exe and not exepath:
  720.         # We didn't find a necessary executable.  Complain.
  721.  
  722.         # Strip out foomatic '%'-style place-holders.
  723.         p = exe.find ('%')
  724.         if p != -1:
  725.             exe = exe[:p]
  726.  
  727.         pkgs = {
  728.             # Foomatic command line executables
  729.             'gs': 'ghostscript',
  730.             'perl': 'perl',
  731.             'foo2oak-wrapper': None,
  732.             'pnm2ppa': 'pnm2ppa',
  733.             'c2050': 'c2050',
  734.             'c2070': 'c2070',
  735.             'cjet': 'cjet',
  736.             'lm1100': 'lx',
  737.             'esc-m': 'min12xxw',
  738.             'min12xxw': 'min12xxw',
  739.             'pbm2l2030': 'pbm2l2030',
  740.             'pbm2l7k': 'pbm2l7k',
  741.             'pbm2lex': 'pbm2l7k',
  742.             # IJS servers (used by foomatic)
  743.             'hpijs': 'hpijs',
  744.             'ijsgutenprint.5.0': 'gutenprint',
  745.             # CUPS filters
  746.             'rastertogutenprint.5.0': 'gutenprint-cups',
  747.             'commandtoepson': 'gutenprint-cups',
  748.             'commandtocanon': 'gutenprint-cups',
  749.             }
  750.         try:
  751.             pkg = pkgs[exe]
  752.         except:
  753.             pkg = None
  754.  
  755.         if pkg:
  756.             _debugprint ("%s included in package %s" % (exe, pkg))
  757.             pkgs_to_install.append (pkg)
  758.         else:
  759.             exes_to_install.append (exe)
  760.  
  761.     return (pkgs_to_install, exes_to_install)
  762.  
  763. def _main():
  764.     c = cups.Connection()
  765.     #printers = getPrinters(c)
  766.     for device in getDevices(c).itervalues():
  767.         print device.uri, device.id_dict
  768.  
  769. if __name__=="__main__":
  770.     _main()
  771.