Debian Codegraph @

troubleshoot/CheckPPDSanity.py

raw
1 #!/usr/bin/python3
2
3 ## Printing troubleshooter
4
5 ## Copyright (C) 2008, 2009, 2010, 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 cupshelpers
27 import installpackage
28 import os
29 import subprocess
30 from timedops import TimedOperation, TimedSubprocess
31 from .base import *
32 from functools import reduce
33
34 class CheckPPDSanity(Question):
35 def __init__ (self, troubleshooter):
36 Question.__init__ (self, troubleshooter, "Check PPD sanity")
37 vbox = Gtk.VBox ()
38 vbox.set_border_width (12)
39 vbox.set_spacing (12)
40 self.label = Gtk.Label ()
41 self.label.set_line_wrap (True)
42 self.label.set_use_markup (True)
43 self.label.set_alignment (0, 0)
44 vbox.pack_start (self.label, False, False, 0)
45
46 box = Gtk.HButtonBox ()
47 box.set_layout (Gtk.ButtonBoxStyle.START)
48 self.install_button = Gtk.Button.new_with_label (_("Install"))
49 box.add (self.install_button)
50 # Although we want this hidden initially,
51 # troubleshooter.new_page will call show_all() on the widget
52 # we give it. We'll need to hide this button in the display()
53 # callback instead.
54 vbox.pack_start (box, False, False, 0)
55
56 troubleshooter.new_page (vbox, self)
57
58 def display (self):
59 self.answers = {}
60
61 answers = self.troubleshooter.answers
62 if not answers['cups_queue_listed']:
63 return False
64
65 parent = self.troubleshooter.get_window ()
66 name = answers['cups_queue']
67 tmpf = None
68 try:
69 cups.setServer ('')
70 self.op = TimedOperation (cups.Connection, parent=parent)
71 c = self.op.run ()
72 self.op = TimedOperation (c.getPPD, args=(name,), parent=parent)
73 tmpf = self.op.run ()
74 except RuntimeError:
75 return False
76 except cups.IPPError:
77 return False
78
79 self.install_button.hide ()
80 title = None
81 text = None
82 try:
83 ppd = cups.PPD (tmpf)
84 self.answers['cups_printer_ppd_valid'] = True
85
86 def options (options_list):
87 o = {}
88 for option in options_list:
89 o[option.keyword] = option.defchoice
90 return o
91
92 defaults = {}
93 for group in ppd.optionGroups:
94 g = options (group.options)
95 for subgroup in group.subgroups:
96 g[subgroup.name] = options (subgroup.options)
97 defaults[group.name] = g
98 self.answers['cups_printer_ppd_defaults'] = defaults
99 except RuntimeError:
100 title = _("Invalid PPD File")
101 self.answers['cups_printer_ppd_valid'] = False
102 try:
103 self.op = TimedSubprocess (parent=parent,
104 args=['cupstestppd', '-rvv', tmpf],
105 close_fds=True,
106 stdin=subprocess.DEVNULL,
107 stdout=subprocess.PIPE,
108 stderr=subprocess.PIPE)
109 result = self.op.run ()
110 self.answers['cupstestppd_output'] = result
111 text = _("The PPD file for printer '%s' does not conform "
112 "to the specification. "
113 "Possible reason follows:") % name
114 text += '\n' + reduce (lambda x, y: x + '\n' + y, result[0])
115 except OSError:
116 # Perhaps cupstestppd is not in the path.
117 text = _("There is a problem with the PPD file for "
118 "printer '%s'.") % name
119
120 if tmpf:
121 os.unlink (tmpf)
122
123 if title is None and not answers['cups_printer_remote']:
124 (pkgs, exes) = cupshelpers.missingPackagesAndExecutables (ppd)
125 self.answers['missing_pkgs_and_exes'] = (pkgs, exes)
126 if len (pkgs) > 0 or len (exes) > 0:
127 title = _("Missing Printer Driver")
128 if len (pkgs) > 0:
129 try:
130 self.packagekit = installpackage.PackageKit ()
131 except:
132 pkgs = []
133
134 if len (pkgs) > 0:
135 self.package = pkgs[0]
136 text = _("Printer '%s' requires the %s package but it "
137 "is not currently installed.") % (name,
138 self.package)
139 self.install_button.show ()
140 else:
141 text = _("Printer '%s' requires the '%s' program but it "
142 "is not currently installed.") % (name,
143 (exes + pkgs)[0])
144
145 if title is not None:
146 self.label.set_markup ('<span weight="bold" size="larger">' +
147 title + '</span>\n\n' + text)
148
149 return title is not None
150
151 def connect_signals (self, handle):
152 self.button_sigid = self.install_button.connect ("clicked",
153 self.install_clicked)
154
155 def disconnect_signals (self):
156 self.install_button.disconnect (self.button_sigid)
157
158 def collect_answer (self):
159 return self.answers
160
161 def cancel_operation (self):
162 self.op.cancel ()
163
164 def install_clicked (self, button):
165 pkgs = self.answers.get('packages_installed', [])
166 pkgs.append (self.package)
167 self.answers['packages_installed'] = pkgs
168 try:
169 self.packagekit.InstallPackageName (0, 0, self.package)
170 except:
171 pass