Debian Codegraph @

troubleshoot/CheckPrinterSanity.py

raw
1 #!/usr/bin/python3
2
3 ## Printing troubleshooter
4
5 ## Copyright (C) 2008, 2009, 2010, 2012, 2014 Red Hat, Inc.
6 ## Authors:
7 ## Tim Waugh <[email protected]>
8
9 ## This program is free software; you can redistribute it and/or modify
10 ## it under the terms of the GNU General Public License as published by
11 ## the Free Software Foundation; either version 2 of the License, or
12 ## (at your option) any later version.
13
14 ## This program is distributed in the hope that it will be useful,
15 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ## GNU General Public License for more details.
18
19 ## You should have received a copy of the GNU General Public License
20 ## along with this program; if not, write to the Free Software
21 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
23 from gi.repository import Gtk
24
25 import cups
26 import os
27 import smburi
28 import subprocess
29 from timedops import TimedOperation, TimedSubprocess
30 import urllib.parse
31 from .base import *
32 class CheckPrinterSanity(Question):
33 def __init__ (self, troubleshooter):
34 Question.__init__ (self, troubleshooter, "Check printer sanity")
35 troubleshooter.new_page (Gtk.Label (), self)
36 self.troubleshooter = troubleshooter
37
38 def display (self):
39 # Collect information useful for the various checks.
40
41 self.answers = {}
42
43 answers = self.troubleshooter.answers
44 if not answers['cups_queue_listed']:
45 return False
46
47 name = answers['cups_queue']
48
49 parent = self.troubleshooter.get_window ()
50
51 # Find out if this is a printer or a class.
52 try:
53 cups.setServer ('')
54 c = TimedOperation (cups.Connection, parent=parent).run ()
55 printers = TimedOperation (c.getPrinters, parent=parent).run ()
56 if name in printers:
57 self.answers['is_cups_class'] = False
58 queue = printers[name]
59 self.answers['cups_printer_dict'] = queue
60 else:
61 self.answers['is_cups_class'] = True
62 classes = TimedOperation (c.getClasses, parent=parent).run ()
63 queue = classes[name]
64 self.answers['cups_class_dict'] = queue
65
66 attrs = TimedOperation (c.getPrinterAttributes, (name,),
67 parent=parent).run ()
68 self.answers['local_cups_queue_attributes'] = attrs
69 except:
70 pass
71
72 if 'cups_printer_dict' in self.answers:
73 cups_printer_dict = self.answers['cups_printer_dict']
74 uri = cups_printer_dict['device-uri']
75 (scheme, rest) = urllib.parse.splittype (uri)
76 self.answers['cups_device_uri_scheme'] = scheme
77 if scheme in ["ipp", "http", "https"]:
78 (hostport, rest) = urllib.parse.splithost (rest)
79 (host, port) = urllib.parse.splitnport (hostport, defport=631)
80 self.answers['remote_server_name'] = host
81 self.answers['remote_server_port'] = port
82 elif scheme == "smb":
83 u = smburi.SMBURI (uri)
84 (group, host, share, user, password) = u.separate ()
85 new_environ = os.environ.copy()
86 new_environ['LC_ALL'] = "C"
87 if group:
88 args = ["nmblookup", "-W", group, host]
89 else:
90 args = ["nmblookup", host]
91 try:
92 p = TimedSubprocess (parent=parent,
93 timeout=5000,
94 args=args,
95 env=new_environ,
96 close_fds=True,
97 stdin=subprocess.DEVNULL,
98 stdout=subprocess.PIPE,
99 stderr=subprocess.PIPE)
100 result = p.run ()
101 self.answers['nmblookup_output'] = result
102 for line in result[0]:
103 if line.startswith ("querying"):
104 continue
105 spc = line.find (' ')
106 if (spc != -1 and
107 not line[spc:].startswith (" failed ")):
108 # Remember the IP address.
109 self.answers['remote_server_name'] = line[:spc]
110 break
111 except OSError:
112 # Problem executing command.
113 pass
114 elif scheme == "hp":
115 new_environ = os.environ.copy()
116 new_environ['LC_ALL'] = "C"
117 new_environ['DISPLAY'] = ""
118 try:
119 p = TimedSubprocess (parent=parent,
120 timeout=3000,
121 args=["hp-info", "-d" + uri],
122 close_fds=True,
123 env=new_environ,
124 stdin=subprocess.DEVNULL,
125 stdout=subprocess.PIPE,
126 stderr=subprocess.PIPE)
127 self.answers['hplip_output'] = p.run ()
128 except OSError:
129 # Problem executing command.
130 pass
131
132 r = cups_printer_dict['printer-type'] & cups.CUPS_PRINTER_REMOTE
133 self.answers['cups_printer_remote'] = (r != 0)
134 return False
135
136 def collect_answer (self):
137 return self.answers