Back to index

system-config-printer  1.3.9+20120706
CheckPrinterSanity.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## Printing troubleshooter
00004 
00005 ## Copyright (C) 2008, 2009, 2010, 2012 Red Hat, Inc.
00006 ## Authors:
00007 ##  Tim Waugh <twaugh@redhat.com>
00008 
00009 ## This program is free software; you can redistribute it and/or modify
00010 ## it under the terms of the GNU General Public License as published by
00011 ## the Free Software Foundation; either version 2 of the License, or
00012 ## (at your option) any later version.
00013 
00014 ## This program is distributed in the hope that it will be useful,
00015 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 ## GNU General Public License for more details.
00018 
00019 ## You should have received a copy of the GNU General Public License
00020 ## along with this program; if not, write to the Free Software
00021 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022 
00023 import cups
00024 import gobject
00025 import os
00026 import smburi
00027 import subprocess
00028 from timedops import TimedOperation, TimedSubprocess
00029 import urllib
00030 from base import *
00031 class CheckPrinterSanity(Question):
00032     def __init__ (self, troubleshooter):
00033         Question.__init__ (self, troubleshooter, "Check printer sanity")
00034         troubleshooter.new_page (gtk.Label (), self)
00035         self.troubleshooter = troubleshooter
00036 
00037     def display (self):
00038         # Collect information useful for the various checks.
00039 
00040         self.answers = {}
00041 
00042         answers = self.troubleshooter.answers
00043         if not answers['cups_queue_listed']:
00044             return False
00045 
00046         name = answers['cups_queue']
00047 
00048         parent = self.troubleshooter.get_window ()
00049 
00050         # Find out if this is a printer or a class.
00051         try:
00052             cups.setServer ('')
00053             c = TimedOperation (cups.Connection, parent=parent).run ()
00054             printers = TimedOperation (c.getPrinters, parent=parent).run ()
00055             if printers.has_key (name):
00056                 self.answers['is_cups_class'] = False
00057                 queue = printers[name]
00058                 self.answers['cups_printer_dict'] = queue
00059             else:
00060                 self.answers['is_cups_class'] = True
00061                 classes = TimedOperation (c.getClasses, parent=parent).run ()
00062                 queue = classes[name]
00063                 self.answers['cups_class_dict'] = queue
00064 
00065             attrs = TimedOperation (c.getPrinterAttributes, (name,),
00066                                     parent=parent).run ()
00067             self.answers['local_cups_queue_attributes'] = attrs
00068         except:
00069             pass
00070 
00071         if self.answers.has_key ('cups_printer_dict'):
00072             cups_printer_dict = self.answers['cups_printer_dict']
00073             uri = cups_printer_dict['device-uri']
00074             (scheme, rest) = urllib.splittype (uri)
00075             self.answers['cups_device_uri_scheme'] = scheme
00076             if scheme in ["ipp", "http", "https"]:
00077                 (hostport, rest) = urllib.splithost (rest)
00078                 (host, port) = urllib.splitnport (hostport, defport=631)
00079                 self.answers['remote_server_name'] = host
00080                 self.answers['remote_server_port'] = port
00081             elif scheme == "smb":
00082                 u = smburi.SMBURI (uri)
00083                 (group, host, share, user, password) = u.separate ()
00084                 new_environ = os.environ.copy()
00085                 new_environ['LC_ALL'] = "C"
00086                 if group:
00087                     args = ["nmblookup", "-W", group, host]
00088                 else:
00089                     args = ["nmblookup", host]
00090                 try:
00091                     p = TimedSubprocess (parent=parent,
00092                                          timeout=5000,
00093                                          args=args,
00094                                          env=new_environ,
00095                                          close_fds=True,
00096                                          stdin=file("/dev/null"),
00097                                          stdout=subprocess.PIPE,
00098                                          stderr=subprocess.PIPE)
00099                     result = p.run ()
00100                     self.answers['nmblookup_output'] = result
00101                     for line in result[0]:
00102                         if line.startswith ("querying"):
00103                             continue
00104                         spc = line.find (' ')
00105                         if (spc != -1 and
00106                             not line[spc:].startswith (" failed ")):
00107                             # Remember the IP address.
00108                             self.answers['remote_server_name'] = line[:spc]
00109                             break
00110                 except OSError:
00111                     # Problem executing command.
00112                     pass
00113             elif scheme == "hp":
00114                 new_environ = os.environ.copy()
00115                 new_environ['LC_ALL'] = "C"
00116                 new_environ['DISPLAY'] = ""
00117                 try:
00118                     p = TimedSubprocess (parent=parent,
00119                                          timeout=3000,
00120                                          args=["hp-info", "-d" + uri],
00121                                          close_fds=True,
00122                                          env=new_environ,
00123                                          stdin=file("/dev/null"),
00124                                          stdout=subprocess.PIPE,
00125                                          stderr=subprocess.PIPE)
00126                     self.answers['hplip_output'] = p.run ()
00127                 except OSError:
00128                     # Problem executing command.
00129                     pass
00130 
00131             r = cups_printer_dict['printer-type'] & cups.CUPS_PRINTER_REMOTE
00132             self.answers['cups_printer_remote'] = (r != 0)
00133         return False
00134 
00135     def collect_answer (self):
00136         return self.answers