Repository navigation
Expand file tree
/
Copy pathppdsloader.py
More file actions
384 lines (316 loc) · 12.8 KB
/
Copy pathppdsloader.py
File metadata and controls
384 lines (316 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/python3
## system-config-printer
## Copyright (C) 2010, 2011, 2012, 2013, 2014 Red Hat, Inc.
## Author: Tim Waugh <twaugh@redhat.com>
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import threading
import dbus
from gi.repository import GObject, GLib
from gi.repository import Gtk
import cups
import cupshelpers
cups.require ("1.9.52")
import asyncconn
from debug import debugprint
import config
import gettext
gettext.install(domain=config.PACKAGE, localedir=config.localedir)
class PPDsLoader(GObject.GObject):
"""
1. If PackageKit support is available, and this is a local server,
try to use PackageKit to install relevant drivers. We do this
because we can only make the right choice about the "best" driver
when the full complement of drivers is there to choose from.
2. Fetch the list of available drivers from CUPS.
3. If Jockey is available, and there is no appropriate driver
available, try to use Jockey to install one.
4. If Jockey was able to install one, fetch the list of available
drivers again.
"""
__gsignals__ = {
'finished': (GObject.SignalFlags.RUN_LAST, None, ())
}
def __init__ (self, device_id=None, parent=None, device_uri=None,
host=None, encryption=None, language=None,
device_make_and_model=None):
GObject.GObject.__init__ (self)
debugprint ("+%s" % self)
self._device_id = device_id
if self._device_id:
self._devid_dict = cupshelpers.parseDeviceID (self._device_id)
else:
self._devid_dict = None
self._device_uri = device_uri
self._device_make_and_model = device_make_and_model
self._parent = parent
self._host = host
self._encryption = encryption
self._language = language
self._installed_files = []
self._conn = None
self._ppds = None
self._driverless_ppds = {}
self._exc = None
self._ppdsmatch_result = None
self._jockey_queried = False
self._jockey_has_answered = False
self._destroyed = False
self._local_cups = (self._host is None or
self._host == "localhost" or
self._host[0] == '/')
try:
self._bus = dbus.SessionBus ()
except:
debugprint ("Failed to get session bus")
self._bus = None
def run (self):
if self._device_id:
self._devid_dict = cupshelpers.parseDeviceID (self._device_id)
if (self._local_cups and
self._device_id and
self._devid_dict["MFG"] and
self._devid_dict["MDL"] and
self._bus):
self._gpk_device_id = "MFG:%s;MDL:%s;" % (self._devid_dict["MFG"],
self._devid_dict["MDL"])
self._query_packagekit ()
else:
self._query_cups ()
def __del__ (self):
debugprint ("-%s" % self)
def destroy (self):
debugprint ("DESTROY: %s" % self)
self._destroyed = True
self._parent = None
if self._conn:
self._conn.destroy ()
self._conn = None
def get_installed_files (self):
return self._installed_files
def _wait_for_processing (self):
thread = getattr (self, '_processing_thread', None)
if thread is not None:
thread.join ()
self._processing_thread = None
def get_ppds (self):
self._wait_for_processing ()
return self._ppds
def get_driverless_ppds (self):
self._wait_for_processing ()
return self._driverless_ppds
def get_ppdsmatch_result (self):
self._wait_for_processing ()
return self._ppdsmatch_result
def get_error (self):
self._wait_for_processing ()
debugprint ("%s: stored error is %s" % (self, repr (self._exc)))
return self._exc
def _query_cups (self):
debugprint ("Asking CUPS for PPDs")
if (not self._conn):
c = asyncconn.Connection (host=self._host,
encryption=self._encryption,
reply_handler=self._cups_connect_reply,
error_handler=self._cups_error)
self._conn = c
else:
self._cups_connect_reply(self._conn, None)
def _cups_connect_reply (self, conn, UNUSED):
if self._destroyed:
return
conn._begin_operation (_("fetching PPDs"))
conn.getPPDs2 (reply_handler=self._cups_reply,
error_handler=self._cups_error)
def _cups_reply (self, conn, result):
if self._destroyed:
return
def process_ppds ():
try:
driverless_ppds = {}
normal_ppds = {}
if result:
for name, ppd in result.items ():
if name.startswith ("driverless:") or name.startswith ("driverless-fax:"):
driverless_ppds[name] = ppd
else:
normal_ppds[name] = ppd
self._driverless_ppds = driverless_ppds
ppds = cupshelpers.ppds.PPDs (normal_ppds, language=self._language)
self._ppds = ppds
self._need_requery_cups = False
# Pre-index makes on background thread to avoid GTK stall during fillMakeList
try:
ppds.getMakes ()
except Exception:
pass
if self._device_id:
fit = ppds.\
getPPDNamesFromDeviceID (self._devid_dict["MFG"],
self._devid_dict["MDL"],
self._devid_dict["DES"],
self._devid_dict["CMD"],
self._device_uri,
self._device_make_and_model)
ppdnamelist = ppds.\
orderPPDNamesByPreference (list(fit.keys ()),
self._installed_files,
devid=self._devid_dict,
fit=fit)
self._ppdsmatch_result = (fit, ppdnamelist)
else:
self._ppdsmatch_result = None
GLib.idle_add (self._on_ppds_processed, conn, None)
except Exception as e:
GLib.idle_add (self._on_ppds_processed, conn, e)
self._processing_thread = threading.Thread (target=process_ppds, daemon=True)
self._processing_thread.start ()
def _on_ppds_processed (self, conn, exc):
self._wait_for_processing ()
if self._destroyed:
if conn:
conn.destroy ()
self._conn = None
return False
if exc is not None:
if conn:
conn.destroy ()
self._conn = None
self._ppds = None
self._driverless_ppds = {}
self._exc = exc
self.emit ('finished')
return False
if self._device_id and self._ppdsmatch_result:
fit, ppdnamelist = self._ppdsmatch_result
if ppdnamelist:
ppdname = ppdnamelist[0]
if (self._bus and
not fit[ppdname].startswith ("exact") and
not self._jockey_queried and
self._devid_dict["MFG"] and
self._devid_dict["MDL"] and
self._local_cups):
# Try to install packages using jockey if
# - there's no appropriate driver (PPD) locally available
# - we are configuring local CUPS server
self._jockey_queried = True
self._query_jockey ()
return False
if conn:
conn.destroy ()
self._conn = None
self.emit ('finished')
return False
def _cups_error (self, conn, exc):
if self._destroyed:
return
conn.destroy ()
self._conn = None
self._ppds = None
self._driverless_ppds = {}
self._exc = exc
self.emit ('finished')
def _query_packagekit (self):
debugprint ("Asking PackageKit to install drivers")
gpk_device_id = self._gpk_device_id
def worker():
bus = None
success = False
try:
bus = dbus.SessionBus(private=True)
bus.set_exit_on_disconnect(False)
obj = bus.get_object("org.freedesktop.PackageKit",
"/org/freedesktop/PackageKit")
proxy = dbus.Interface(obj, "org.freedesktop.PackageKit.Modify")
resources = [gpk_device_id]
interaction = "hide-finished"
debugprint("Calling InstallPrinterDrivers in worker")
proxy.InstallPrinterDrivers(dbus.UInt32(0), resources, interaction, timeout=3600)
success = True
except Exception as e:
debugprint("Got PackageKit error in worker: %s" % repr(e))
finally:
GLib.idle_add(self._on_packagekit_done, success, bus)
threading.Thread(target=worker, daemon=True).start()
def _on_packagekit_done(self, success, bus=None):
if bus is not None:
bus.close()
if self._destroyed:
return False
if not success:
debugprint("PackageKit installation failed or returned error")
else:
debugprint("Got PackageKit reply")
self._need_requery_cups = True
self._query_cups()
return False
def _query_jockey (self):
debugprint ("Asking Jockey to install drivers")
try:
obj = self._bus.get_object ("com.ubuntu.DeviceDriver", "/GUI")
jockey = dbus.Interface (obj, "com.ubuntu.DeviceDriver")
r = jockey.search_driver ("printer_deviceid:%s" % self._device_id,
reply_handler=self._jockey_reply,
error_handler=self._jockey_error,
timeout=3600)
except Exception as e:
self._jockey_error (e)
def _jockey_reply (self, conn, result):
if self._destroyed:
return
debugprint ("Got Jockey result: %s" % repr (result))
self._jockey_has_answered = True
try:
self._installed_files = result[1]
except:
self._installed_files = []
self._query_cups ()
def _jockey_error (self, exc):
if self._destroyed:
return
debugprint ("Got Jockey error: %s" % repr (exc))
if self._need_requery_cups:
self._query_cups ()
else:
if self._conn is not None:
self._conn.destroy ()
self._conn = None
self.emit ('finished')
if __name__ == "__main__":
class Foo:
def __init__ (self):
w = Gtk.Window ()
b = Gtk.Button.new_with_label ("Go")
w.add (b)
b.connect ('clicked', self.go)
w.connect ('delete-event', Gtk.main_quit)
w.show_all ()
self._window = w
def go (self, button):
loader = PPDsLoader (device_id="MFG:MFG;MDL:MDL;",
parent=self._window)
loader.connect ('finished', self.ppds_loaded)
loader.run ()
def ppds_loaded (self, ppdsloader):
self._window.destroy ()
Gtk.main_quit ()
exc = ppdsloader.get_error ()
print(exc)
ppds = ppdsloader.get_ppds ()
if ppds is not None:
print(len (ppds))
ppdsloader.destroy ()
from debug import set_debugging
set_debugging (True)
Foo ()
Gtk.main ()