~ubuntu-branches/ubuntu/raring/hplip/raring

« back to all changes in this revision

Viewing changes to .pc/hp-setup-prompt-for-custom-PPD.dpatch/setup.py

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2012-10-06 15:03:44 UTC
  • mfrom: (1.6.1) (20.1.16 quantal)
  • Revision ID: package-import@ubuntu.com-20121006150344-2p3xz26br0t3hu2q
Tags: 3.12.10-1
* New upstream release
  - Fixes "Network scanning fails (Closes: #683033)
* quilt refresh hplip-syslog-fix-debug-messages-to-error.dpatch
* Fix "error in clean build env" updated debian/rules (Closes: #687129)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
# (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 
19
#
 
20
# Author: Don Welch
 
21
#
 
22
 
 
23
 
 
24
__version__ = '9.0'
 
25
__title__ = 'Printer/Fax Setup Utility'
 
26
__mod__ = 'hp-setup'
 
27
__doc__ = "Installs HPLIP printers and faxes in the CUPS spooler. Tries to automatically determine the correct PPD file to use. Allows the printing of a testpage. Performs basic fax parameter setup."
 
28
 
 
29
# Std Lib
 
30
import sys
 
31
import getopt
 
32
import time
 
33
import os.path
 
34
import re
 
35
import os
 
36
import gzip
 
37
 
 
38
try:
 
39
    import readline
 
40
except ImportError:
 
41
    pass
 
42
 
 
43
# Local
 
44
from base.g import *
 
45
from base import device, utils, tui, models, module
 
46
from prnt import cups
 
47
 
 
48
pm = None
 
49
 
 
50
def plugin_download_callback(c, s, t):
 
51
    pm.update(int(100*c*s/t),
 
52
             utils.format_bytes(c*s))
 
53
 
 
54
 
 
55
nickname_pat = re.compile(r'''\*NickName:\s*\"(.*)"''', re.MULTILINE)
 
56
 
 
57
USAGE = [ (__doc__, "", "name", True),
 
58
          ("Usage: %s [MODE] [OPTIONS] [SERIAL NO.|USB bus:device|IP|DEVNODE]" % __mod__, "", "summary", True),
 
59
          utils.USAGE_MODE,
 
60
          utils.USAGE_GUI_MODE,
 
61
          utils.USAGE_INTERACTIVE_MODE,
 
62
          utils.USAGE_SPACE,
 
63
          utils.USAGE_OPTIONS,
 
64
          ("Automatic mode:", "-a or --auto (-i mode only)", "option", False),
 
65
          ("To specify the port on a multi-port JetDirect:", "--port=<port> (Valid values are 1\*, 2, and 3. \*default)", "option", False),
 
66
          ("No testpage in automatic mode:", "-x (-i mode only)", "option", False),
 
67
          ("To specify a CUPS printer queue name:", "-p<printer> or --printer=<printer> (-i mode only)", "option", False),
 
68
          ("To specify a CUPS fax queue name:", "-f<fax> or --fax=<fax> (-i mode only)", "option", False),
 
69
          ("Type of queue(s) to install:", "-t<typelist> or --type=<typelist>. <typelist>: print*, fax\* (\*default) (-i mode only)", "option", False),
 
70
          ("To specify the device URI to install:", "-d<device> or --device=<device> (--qt4 mode only)", "option", False),
 
71
          ("Remove printers or faxes instead of setting-up:", "-r or --rm or --remove (-u only)", "option", False),
 
72
          utils.USAGE_LANGUAGE,
 
73
          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
 
74
          utils.USAGE_HELP,
 
75
          ("[SERIAL NO.|USB ID|IP|DEVNODE]", "", "heading", False),
 
76
          ("USB bus:device (usb only):", """"xxx:yyy" where 'xxx' is the USB bus and 'yyy' is the USB device. (Note: The ':' and all leading zeros must be present.)""", 'option', False),
 
77
          ("", "Use the 'lsusb' command to obtain this information.", "option", False),
 
78
          ("IPs (network only):", 'IPv4 address "a.b.c.d" or "hostname"', "option", False),
 
79
          ("DEVNODE (parallel only):", '"/dev/parportX", X=0,1,2,...', "option", False),
 
80
          ("SERIAL NO. (usb and parallel only):", '"serial no."', "option", True),
 
81
          utils.USAGE_EXAMPLES,
 
82
          ("Setup using GUI mode:", "$ hp-setup", "example", False),
 
83
          ("Setup using GUI mode, specifying usb:", "$ hp-setup -b usb", "example", False),
 
84
          ("Setup using GUI mode, specifying an IP:", "$ hp-setup 192.168.0.101", "example", False),
 
85
          ("One USB printer attached, automatic:", "$ hp-setup -i -a", "example", False),
 
86
          ("USB, IDs specified:", "$ hp-setup -i 001:002", "example", False),
 
87
          ("Network:", "$ hp-setup -i 66.35.250.209", "example", False),
 
88
          ("Network, Jetdirect port 2:", "$ hp-setup -i --port=2 66.35.250.209", "example", False),
 
89
          ("Parallel:", "$ hp-setup -i /dev/parport0", "example", False),
 
90
          ("USB or parallel, using serial number:", "$ hp-setup -i US12345678A", "example", False),
 
91
          ("USB, automatic:", "$ hp-setup -i --auto 001:002", "example", False),
 
92
          ("Parallel, automatic, no testpage:", "$ hp-setup -i -a -x /dev/parport0", "example", False),
 
93
          ("Parallel, choose device:", "$ hp-setup -i -b par", "example", False),
 
94
          utils.USAGE_SPACE,
 
95
          utils.USAGE_NOTES,
 
96
          ("1. If no serial number, USB ID, IP, or device node is specified, the USB and parallel busses will be probed for devices.", "", 'note', False),
 
97
          ("2. Using 'lsusb' to obtain USB IDs: (example)", "", 'note', False),
 
98
          ("   $ lsusb", "", 'note', False),
 
99
          ("         Bus 003 Device 011: ID 03f0:c202 Hewlett-Packard", "", 'note', False),
 
100
          ("   $ hp-setup --auto 003:011", "", 'note', False),
 
101
          ("   (Note: You may have to run 'lsusb' from /sbin or another location. Use '$ locate lsusb' to determine this.)", "", 'note', True),
 
102
          ("3. Parameters -a, -f, -p, or -t are not valid in GUI (-u) mode.", "", 'note', True),
 
103
          utils.USAGE_SPACE,
 
104
          utils.USAGE_SEEALSO,
 
105
          ("hp-makeuri", "", "seealso", False),
 
106
          ("hp-probe", "", "seealso", False),
 
107
        ]
 
108
 
 
109
 
 
110
def showPasswordUI(prompt):
 
111
    import getpass
 
112
    print ""
 
113
    print log.bold(prompt)
 
114
    username = raw_input("Username: ")
 
115
    password = getpass.getpass("Password: ")
 
116
 
 
117
    return (username, password)
 
118
 
 
119
 
 
120
def restart_cups():
 
121
    if os.path.exists('/etc/init.d/cups'):
 
122
        return '/etc/init.d/cups restart'
 
123
 
 
124
    elif os.path.exists('/etc/init.d/cupsys'):
 
125
        return '/etc/init.d/cupsys restart'
 
126
 
 
127
    else:
 
128
        return 'killall -HUP cupsd'
 
129
 
 
130
 
 
131
mod = module.Module(__mod__, __title__, __version__, __doc__, USAGE,
 
132
                    (INTERACTIVE_MODE, GUI_MODE),
 
133
                    (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4),
 
134
                    run_as_root_ok=True)
 
135
 
 
136
opts, device_uri, printer_name, mode, ui_toolkit, loc = \
 
137
    mod.parseStdOpts('axp:P:f:t:b:d:rq',
 
138
                     ['ttl=', 'filter=', 'search=', 'find=',
 
139
                      'method=', 'time-out=', 'timeout=',
 
140
                      'printer=', 'fax=', 'type=', 'port=',
 
141
                       'auto', 'device=', 'rm', 'remove'],
 
142
                      handle_device_printer=False)
 
143
 
 
144
selected_device_name = None
 
145
printer_name = None
 
146
fax_name = None
 
147
bus = None
 
148
setup_print = True
 
149
setup_fax = True
 
150
makeuri = None
 
151
auto = False
 
152
testpage_in_auto_mode = True
 
153
jd_port = 1
 
154
remove = False
 
155
ignore_plugin_check = False
 
156
 
 
157
for o, a in opts:
 
158
    if o == '-x':
 
159
        testpage_in_auto_mode = False
 
160
 
 
161
    elif o in ('-P', '-p', '--printer'):
 
162
        printer_name = a
 
163
 
 
164
    elif o in ('-f', '--fax'):
 
165
        fax_name = a
 
166
 
 
167
    elif o in ('-d', '--device'):
 
168
        device_uri = a
 
169
 
 
170
    elif o in ('-b', '--bus'):
 
171
        bus = [x.lower().strip() for x in a.split(',')]
 
172
        if not device.validateBusList(bus, False):
 
173
            mod.usage(error_msg=['Invalid bus name'])
 
174
 
 
175
    elif o in ('-t', '--type'):
 
176
        setup_fax, setup_print = False, False
 
177
        a = a.strip().lower()
 
178
        for aa in a.split(','):
 
179
            if aa.strip() not in ('print', 'fax'):
 
180
                mod.usage(error_msg=['Invalid type.'])
 
181
 
 
182
            if aa.strip() == 'print':
 
183
                setup_print = True
 
184
 
 
185
            elif aa.strip() == 'fax':
 
186
                if not prop.fax_build:
 
187
                    log.error("Cannot enable fax setup - HPLIP not built with fax enabled.")
 
188
                else:
 
189
                    setup_fax = True
 
190
 
 
191
    elif o == '--port':
 
192
        try:
 
193
            jd_port = int(a)
 
194
        except ValueError:
 
195
            #log.error("Invalid port number. Must be between 1 and 3 inclusive.")
 
196
            mod.usage(error_msg=['Invalid port number. Must be between 1 and 3 inclusive.'])
 
197
 
 
198
    elif o in ('-a', '--auto'):
 
199
        auto = True
 
200
 
 
201
    elif o in ('-r', '--rm', '--remove'):
 
202
        remove = True
 
203
    elif o in ('-q'):
 
204
        ignore_plugin_check = True
 
205
 
 
206
 
 
207
try:
 
208
    param = mod.args[0]
 
209
except IndexError:
 
210
    param = ''
 
211
 
 
212
log.debug("param=%s" % param)
 
213
if printer_name is not None:
 
214
   selected_device_name = printer_name
 
215
else:
 
216
   if fax_name is not None:
 
217
      selected_device_name = fax_name
 
218
log.debug("selected_device_name=%s" % selected_device_name)
 
219
 
 
220
if mode == GUI_MODE:
 
221
    if selected_device_name is not None: 
 
222
        log.warning("-p or -f option is not supported")
 
223
    if ui_toolkit == 'qt3':
 
224
        if not utils.canEnterGUIMode():
 
225
            log.error("%s requires GUI support (try running with --qt4). Also, try using interactive (-i) mode." % __mod__)
 
226
            sys.exit(1)
 
227
    else:
 
228
        if not utils.canEnterGUIMode4():
 
229
            log.error("%s requires GUI support (try running with --qt3). Also, try using interactive (-i) mode." % __mod__)
 
230
            sys.exit(1)
 
231
 
 
232
if mode == GUI_MODE:
 
233
    if ui_toolkit == 'qt3':
 
234
        try:
 
235
            from qt import *
 
236
            from ui import setupform
 
237
        except ImportError:
 
238
            log.error("Unable to load Qt3 support. Is it installed?")
 
239
            sys.exit(1)
 
240
 
 
241
        if remove:
 
242
            log.warn("-r/--rm/--remove not supported in qt3 mode.")
 
243
 
 
244
        app = QApplication(sys.argv)
 
245
        QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
 
246
 
 
247
        if loc is None:
 
248
            loc = user_conf.get('ui', 'loc', 'system')
 
249
            if loc.lower() == 'system':
 
250
                loc = str(QTextCodec.locale())
 
251
                log.debug("Using system locale: %s" % loc)
 
252
 
 
253
        if loc.lower() != 'c':
 
254
            e = 'utf8'
 
255
            try:
 
256
                l, x = loc.split('.')
 
257
                loc = '.'.join([l, e])
 
258
            except ValueError:
 
259
                l = loc
 
260
                loc = '.'.join([loc, e])
 
261
 
 
262
            log.debug("Trying to load .qm file for %s locale." % loc)
 
263
            trans = QTranslator(None)
 
264
 
 
265
            qm_file = 'hplip_%s.qm' % l
 
266
            log.debug("Name of .qm file: %s" % qm_file)
 
267
            loaded = trans.load(qm_file, prop.localization_dir)
 
268
 
 
269
            if loaded:
 
270
                app.installTranslator(trans)
 
271
            else:
 
272
                loc = 'c'
 
273
 
 
274
        if loc == 'c':
 
275
            log.debug("Using default 'C' locale")
 
276
        else:
 
277
            log.debug("Using locale: %s" % loc)
 
278
            QLocale.setDefault(QLocale(loc))
 
279
            prop.locale = loc
 
280
            try:
 
281
                locale.setlocale(locale.LC_ALL, locale.normalize(loc))
 
282
            except locale.Error:
 
283
                pass
 
284
 
 
285
        try:
 
286
            w = setupform.SetupForm(bus, param, jd_port)
 
287
        except Error:
 
288
            log.error("Unable to connect to HPLIP I/O. Please (re)start HPLIP and try again.")
 
289
            sys.exit(1)
 
290
 
 
291
        app.setMainWidget(w)
 
292
        w.show()
 
293
 
 
294
        app.exec_loop()
 
295
 
 
296
    else: # qt4
 
297
        try:
 
298
            from PyQt4.QtGui import QApplication, QMessageBox
 
299
            from ui4.setupdialog import SetupDialog
 
300
        except ImportError:
 
301
            log.error("Unable to load Qt4 support. Is it installed?")
 
302
            sys.exit(1)
 
303
 
 
304
        app = QApplication(sys.argv)
 
305
        log.debug("Sys.argv=%s printer_name=%s param=%s jd_port=%s device_uri=%s remove=%s" % (sys.argv, printer_name, param, jd_port, device_uri, remove))
 
306
        dlg = SetupDialog(None, param, jd_port, device_uri, remove)
 
307
        dlg.show()
 
308
        try:
 
309
            log.debug("Starting GUI loop...")
 
310
            app.exec_()
 
311
        except KeyboardInterrupt:
 
312
            sys.exit(0)
 
313
 
 
314
 
 
315
else: # INTERACTIVE_MODE
 
316
    try:
 
317
 
 
318
        cups.setPasswordCallback(showPasswordUI)
 
319
 
 
320
        if remove:
 
321
            log.error("-r/--rm/--remove not supported in -i mode.")
 
322
            sys.exit(1)
 
323
 
 
324
        if not auto:
 
325
            log.info("(Note: Defaults for each question are maked with a '*'. Press <enter> to accept the default.)")
 
326
            log.info("")
 
327
 
 
328
        # ******************************* MAKEURI
 
329
        if param:
 
330
            device_uri, sane_uri, fax_uri = device.makeURI(param, jd_port)
 
331
 
 
332
        # ******************************* CONNECTION TYPE CHOOSER
 
333
        if not device_uri and bus is None:
 
334
            bus = tui.connection_table()
 
335
 
 
336
            if bus is None:
 
337
                sys.exit(0)
 
338
 
 
339
            log.info("\nUsing connection type: %s" % bus[0])
 
340
 
 
341
            log.info("")
 
342
 
 
343
        # ******************************* DEVICE CHOOSER
 
344
 
 
345
        if not device_uri:
 
346
            log.debug("\nDEVICE CHOOSER setup_fax=%s, setup_print=%s" % (setup_fax, setup_print))
 
347
            device_uri = mod.getDeviceUri(device_uri, selected_device_name, devices = device.probeDevices(bus))
 
348
 
 
349
 
 
350
        # ******************************* QUERY MODEL AND COLLECT PPDS
 
351
        log.info(log.bold("\nSetting up device: %s\n" % device_uri))
 
352
 
 
353
        log.info("")
 
354
        print_uri = device_uri.replace("hpfax:", "hp:")
 
355
        fax_uri = device_uri.replace("hp:", "hpfax:")
 
356
 
 
357
        back_end, is_hp, bus, model, \
 
358
            serial, dev_file, host, zc, port = \
 
359
            device.parseDeviceURI(device_uri)
 
360
 
 
361
        log.debug("Model=%s" % model)
 
362
        mq = device.queryModelByURI(device_uri)
 
363
 
 
364
        if not mq or mq.get('support-type', SUPPORT_TYPE_NONE) == SUPPORT_TYPE_NONE:
 
365
            log.error("Unsupported printer model.")
 
366
            sys.exit(1)
 
367
 
 
368
        if mq.get('fax-type', FAX_TYPE_NONE) in (FAX_TYPE_NONE, FAX_TYPE_NOT_SUPPORTED) and setup_fax:
 
369
            #log.warning("Cannot setup fax - device does not have fax feature.")
 
370
            setup_fax = False
 
371
 
 
372
        # ******************************* PLUGIN
 
373
 
 
374
        norm_model = models.normalizeModelName(model).lower()
 
375
        plugin = mq.get('plugin', PLUGIN_NONE)
 
376
 
 
377
        plugin_installed = utils.to_bool(sys_state.get('plugin', 'installed', '0'))
 
378
        if ignore_plugin_check is False and plugin > PLUGIN_NONE and not plugin_installed:
 
379
            tui.header("PLUG-IN INSTALLATION")
 
380
 
 
381
            hp_plugin = utils.which('hp-plugin')
 
382
 
 
383
            if hp_plugin:
 
384
                if prop.gui_build:
 
385
                    os.system("hp-plugin -i")
 
386
                else:
 
387
                    os.system("hp-plugin")
 
388
 
 
389
        ppds = cups.getSystemPPDs()
 
390
 
 
391
        default_model = utils.xstrip(model.replace('series', '').replace('Series', ''), '_')
 
392
 
 
393
        installed_print_devices = device.getSupportedCUPSDevices(['hp'])
 
394
        for d in installed_print_devices.keys():
 
395
            for p in installed_print_devices[d]:
 
396
                log.debug("found print queue '%s'" % p)
 
397
 
 
398
        installed_fax_devices = device.getSupportedCUPSDevices(['hpfax'])
 
399
        for d in installed_fax_devices.keys():
 
400
            for f in installed_fax_devices[d]:
 
401
                log.debug("found fax queue '%s'" % f)
 
402
 
 
403
        # ******************************* PRINT QUEUE SETUP
 
404
        if setup_print:
 
405
 
 
406
            tui.header("PRINT QUEUE SETUP")
 
407
 
 
408
            if not auto and print_uri in installed_print_devices:
 
409
                log.warning("One or more print queues already exist for this device: %s." %
 
410
                    ', '.join(installed_print_devices[print_uri]))
 
411
 
 
412
                ok, setup_print = tui.enter_yes_no("\nWould you like to install another print queue for this device", 'n')
 
413
                if not ok: sys.exit(0)
 
414
 
 
415
        if setup_print:
 
416
            if auto:
 
417
                printer_name = default_model
 
418
 
 
419
            printer_default_model = default_model
 
420
 
 
421
            installed_printer_names = device.getSupportedCUPSPrinterNames(['hp'])
 
422
            # Check for duplicate names
 
423
            if (device_uri in installed_print_devices and printer_default_model in installed_print_devices[device_uri]) \
 
424
               or (printer_default_model in installed_printer_names):
 
425
                    i = 2
 
426
                    while True:
 
427
                        t = printer_default_model + "_%d" % i
 
428
                        if (t not in installed_printer_names) and(device_uri not in installed_print_devices or t not in installed_print_devices[device_uri]):
 
429
                            printer_default_model += "_%d" % i
 
430
                            break
 
431
                        i += 1
 
432
 
 
433
            if not auto:
 
434
                if printer_name is None:
 
435
                    while True:
 
436
                        printer_name = raw_input(log.bold("\nPlease enter a name for this print queue (m=use model name:'%s'*, q=quit) ?" % printer_default_model))
 
437
 
 
438
                        if printer_name.lower().strip() == 'q':
 
439
                            log.info("OK, done.")
 
440
                            sys.exit(0)
 
441
 
 
442
                        if not printer_name or printer_name.lower().strip() == 'm':
 
443
                            printer_name = printer_default_model
 
444
 
 
445
                        name_ok = True
 
446
 
 
447
                        for d in installed_print_devices.keys():
 
448
                            for p in installed_print_devices[d]:
 
449
                                if printer_name == p: 
 
450
                                    log.error("A print queue with that name already exists. Please enter a different name.")
 
451
                                    name_ok = False
 
452
                                    break
 
453
 
 
454
                        for d in installed_fax_devices.keys():
 
455
                            for f in installed_fax_devices[d]:
 
456
                                if printer_name == f:
 
457
                                    log.error("A fax queue with that name already exists. Please enter a different name.")
 
458
                                    name_ok = False
 
459
                                    break
 
460
 
 
461
                        for c in printer_name:
 
462
                            if c in cups.INVALID_PRINTER_NAME_CHARS:
 
463
                                log.error("Invalid character '%s' in printer name. Please enter a name that does not contain this character." % c)
 
464
                                name_ok = False
 
465
 
 
466
                        if name_ok:
 
467
                            break
 
468
            else:
 
469
                printer_name = printer_default_model
 
470
 
 
471
            log.info("Using queue name: %s" % printer_name)
 
472
 
 
473
            default_model = utils.xstrip(model.replace('series', '').replace('Series', ''), '_')
 
474
 
 
475
 
 
476
            log.info("Locating PPD file... Please wait.")
 
477
            print_ppd = cups.getPPDFile2(mq, default_model, ppds)
 
478
 
 
479
            enter_ppd = False
 
480
            if print_ppd is None:
 
481
                enter_ppd = True
 
482
                log.error("Unable to find an appropriate PPD file.")
 
483
 
 
484
            else:
 
485
                print_ppd, desc = print_ppd
 
486
                log.info("\nFound PPD file: %s" % print_ppd)
 
487
 
 
488
                if desc:
 
489
                    log.info("Description: %s" % desc)
 
490
#
 
491
                    if not auto:
 
492
                        log.info("\nNote: The model number may vary slightly from the actual model number on the device.")
 
493
                        ok, ans = tui.enter_yes_no("\nDoes this PPD file appear to be the correct one")
 
494
                        if not ok: sys.exit(0)
 
495
                        if not ans: enter_ppd = True
 
496
 
 
497
 
 
498
            if enter_ppd:
 
499
                enter_ppd = False
 
500
 
 
501
                ok, enter_ppd = tui.enter_yes_no("\nWould you like to specify the path to the correct PPD file to use", 'n')
 
502
                if not ok: sys.exit(0)
 
503
 
 
504
                if enter_ppd:
 
505
                    ok = False
 
506
 
 
507
                    while True:
 
508
                        user_input = raw_input(log.bold("\nPlease enter the full filesystem path to the PPD file to use (q=quit) :"))
 
509
 
 
510
                        if user_input.lower().strip() == 'q':
 
511
                            log.info("OK, done.")
 
512
                            sys.exit(0)
 
513
 
 
514
                        file_path = user_input
 
515
 
 
516
                        if os.path.exists(file_path) and os.path.isfile(file_path):
 
517
 
 
518
                            if file_path.endswith('.gz'):
 
519
                                nickname = gzip.GzipFile(file_path, 'r').read(4096)
 
520
                            else:
 
521
                                nickname = file(file_path, 'r').read(4096)
 
522
 
 
523
                            try:
 
524
                                desc = nickname_pat.search(nickname).group(1)
 
525
                            except AttributeError:
 
526
                                desc = ''
 
527
 
 
528
                            if desc:
 
529
                                log.info("Description for the file: %s" % desc)
 
530
                            else:
 
531
                                log.error("No PPD 'NickName' found. This file may not be a valid PPD file.")
 
532
 
 
533
                            ok, ans = tui.enter_yes_no("\nUse this file")
 
534
                            if not ok: sys.exit(0)
 
535
                            if ans: print_ppd = file_path
 
536
 
 
537
                        else:
 
538
                            log.error("File not found or not an appropriate (PPD) file.")
 
539
 
 
540
                        if ok:
 
541
                            break
 
542
                else:
 
543
                    log.error("PPD file required. Setup cannot continue. Exiting.")
 
544
                    sys.exit(1)
 
545
 
 
546
            if auto:
 
547
                location, info = '', 'Automatically setup by HPLIP'
 
548
            else:
 
549
                while True:
 
550
                    location = raw_input(log.bold("Enter a location description for this printer (q=quit) ?"))
 
551
 
 
552
                    if location.strip().lower() == 'q':
 
553
                        log.info("OK, done.")
 
554
                        sys.exit(0)
 
555
 
 
556
                    # TODO: Validate chars
 
557
                    break
 
558
 
 
559
                while True:
 
560
                    info = raw_input(log.bold("Enter additonal information or notes for this printer (q=quit) ?"))
 
561
 
 
562
                    if info.strip().lower() == 'q':
 
563
                        log.info("OK, done.")
 
564
                        sys.exit(0)
 
565
 
 
566
                    # TODO: Validate chars
 
567
                    break
 
568
 
 
569
            log.info(log.bold("\nAdding print queue to CUPS:"))
 
570
            log.info("Device URI: %s" % print_uri)
 
571
            log.info("Queue name: %s" % printer_name)
 
572
            log.info("PPD file: %s" % print_ppd)
 
573
            log.info("Location: %s" % location)
 
574
            log.info("Information: %s" % info)
 
575
 
 
576
            log.debug("Restarting CUPS...")
 
577
            status, output = utils.run(restart_cups())
 
578
            log.debug("Restart CUPS returned: exit=%d output=%s" % (status, output))
 
579
 
 
580
            time.sleep(3)
 
581
            cups.setPasswordPrompt("You do not have permission to add a printer.")
 
582
            if not os.path.exists(print_ppd): # assume foomatic: or some such
 
583
                status, status_str = cups.addPrinter(printer_name.encode('utf8'), print_uri,
 
584
                    location, '', print_ppd, info)
 
585
            else:
 
586
                status, status_str = cups.addPrinter(printer_name.encode('utf8'), print_uri,
 
587
                    location, print_ppd, '', info)
 
588
 
 
589
            log.debug("addPrinter() returned (%d, %s)" % (status, status_str))
 
590
 
 
591
            installed_print_devices = device.getSupportedCUPSDevices(['hp'])
 
592
 
 
593
            if print_uri not in installed_print_devices or \
 
594
                printer_name not in installed_print_devices[print_uri]:
 
595
 
 
596
                log.error("Printer queue setup failed. Please restart CUPS and try again.")
 
597
                sys.exit(1)
 
598
            else:
 
599
                # sending Event to add this device in hp-systray
 
600
                utils.sendEvent(EVENT_CUPS_QUEUES_CHANGED,print_uri, printer_name)
 
601
 
 
602
 
 
603
        # ******************************* FAX QUEUE SETUP
 
604
        if setup_fax and not prop.fax_build:
 
605
            log.error("Cannot setup fax - HPLIP not built with fax enabled.")
 
606
            setup_fax = False
 
607
 
 
608
        if setup_fax:
 
609
 
 
610
            try:
 
611
                from fax import fax
 
612
            except ImportError:
 
613
                # This can fail on Python < 2.3 due to the datetime module
 
614
                setup_fax = False
 
615
                log.warning("Fax setup disabled - Python 2.3+ required.")
 
616
 
 
617
        log.info("")
 
618
 
 
619
        if setup_fax:
 
620
 
 
621
            tui.header("FAX QUEUE SETUP")
 
622
 
 
623
            if not auto and fax_uri in installed_fax_devices:
 
624
                log.warning("One or more fax queues already exist for this device: %s." % ', '.join(installed_fax_devices[fax_uri]))
 
625
                ok, setup_fax = tui.enter_yes_no("\nWould you like to install another fax queue for this device", 'n')
 
626
                if not ok: sys.exit(0)
 
627
 
 
628
        if setup_fax:
 
629
            if auto: # or fax_name is None:
 
630
                fax_name = default_model + '_fax'
 
631
 
 
632
            fax_default_model = default_model + '_fax'
 
633
 
 
634
            installed_fax_names = device.getSupportedCUPSPrinterNames(['hpfax'])
 
635
            # Check for duplicate names
 
636
            if (fax_uri in installed_fax_devices and fax_default_model in installed_fax_devices[fax_uri]) \
 
637
                or (fax_default_model in installed_fax_names):
 
638
                    i = 2
 
639
                    while True:
 
640
                        t = fax_default_model + "_%d" % i
 
641
                        if (t in installed_fax_names) and (fax_uri not in installed_fax_devices or t not in installed_fax_devices[fax_uri]):
 
642
                            fax_default_model += "_%d" % i
 
643
                            break
 
644
                        i += 1
 
645
 
 
646
            if not auto:
 
647
                if fax_name is None:
 
648
                    while True:
 
649
                        fax_name = raw_input(log.bold("\nPlease enter a name for this fax queue (m=use model name:'%s'*, q=quit) ?" % fax_default_model))
 
650
 
 
651
                        if fax_name.lower().strip() == 'q':
 
652
                            log.info("OK, done.")
 
653
                            sys.exit(0)
 
654
 
 
655
                        if not fax_name or fax_name.lower().strip() == 'm':
 
656
                            fax_name = fax_default_model
 
657
 
 
658
                        name_ok = True
 
659
 
 
660
                        for d in installed_print_devices.keys():
 
661
                            for p in installed_print_devices[d]:
 
662
                                if fax_name == p:
 
663
                                    log.error("A print queue with that name already exists. Please enter a different name.")
 
664
                                    name_ok = False
 
665
                                    break
 
666
 
 
667
                        for d in installed_fax_devices.keys():
 
668
                            for f in installed_fax_devices[d]:
 
669
                                if fax_name == f:
 
670
                                    log.error("A fax queue with that name already exists. Please enter a different name.")
 
671
                                    name_ok = False
 
672
                                    break
 
673
 
 
674
                        for c in fax_name:
 
675
                            if c in (' ', '#', '/', '%'):
 
676
                                log.error("Invalid character '%s' in fax name. Please enter a name that does not contain this character." % c)
 
677
                                name_ok = False
 
678
 
 
679
                        if name_ok:
 
680
                            break
 
681
 
 
682
            else:
 
683
                fax_name = fax_default_model
 
684
 
 
685
            log.info("Using queue name: %s" % fax_name)
 
686
            fax_ppd,fax_ppd_type,nick = cups.getFaxPPDFile(mq, fax_name)
 
687
            
 
688
            if not fax_ppd:
 
689
                log.error("Unable to find HP fax PPD file! Please check you HPLIP installation and try again.")
 
690
                sys.exit(1)
 
691
 
 
692
            if auto:
 
693
                location, info = '', 'Automatically setup by HPLIP'
 
694
            else:
 
695
                while True:
 
696
                    location = raw_input(log.bold("Enter a location description for this printer (q=quit) ?"))
 
697
 
 
698
                    if location.strip().lower() == 'q':
 
699
                        log.info("OK, done.")
 
700
                        sys.exit(0)
 
701
 
 
702
                    # TODO: Validate chars
 
703
                    break
 
704
 
 
705
                while True:
 
706
                    info = raw_input(log.bold("Enter additonal information or notes for this printer (q=quit) ?"))
 
707
 
 
708
                    if info.strip().lower() == 'q':
 
709
                        log.info("OK, done.")
 
710
                        sys.exit(0)
 
711
 
 
712
                    # TODO: Validate chars
 
713
                    break
 
714
 
 
715
            log.info(log.bold("\nAdding fax queue to CUPS:"))
 
716
            log.info("Device URI: %s" % fax_uri)
 
717
            log.info("Queue name: %s" % fax_name)
 
718
            log.info("PPD file: %s" % fax_ppd)
 
719
            log.info("Location: %s" % location)
 
720
            log.info("Information: %s" % info)
 
721
 
 
722
            cups.setPasswordPrompt("You do not have permission to add a fax device.")
 
723
            if not os.path.exists(fax_ppd): # assume foomatic: or some such
 
724
                status, status_str = cups.addPrinter(fax_name.encode('utf8'), fax_uri,
 
725
                    location, '', fax_ppd, info)
 
726
            else:
 
727
                status, status_str = cups.addPrinter(fax_name.encode('utf8'), fax_uri,
 
728
                    location, fax_ppd, '', info)
 
729
 
 
730
            log.debug("addPrinter() returned (%d, %s)" % (status, status_str))
 
731
 
 
732
            installed_fax_devices = device.getSupportedCUPSDevices(['hpfax'])
 
733
 
 
734
            log.debug(installed_fax_devices)
 
735
 
 
736
            if fax_uri not in installed_fax_devices or \
 
737
                fax_name not in installed_fax_devices[fax_uri]:
 
738
 
 
739
                log.error("Fax queue setup failed. Please restart CUPS and try again.")
 
740
                sys.exit(1)
 
741
            else:
 
742
                # sending Event to add this device in hp-systray
 
743
                utils.sendEvent(EVENT_CUPS_QUEUES_CHANGED,fax_uri, fax_name)
 
744
 
 
745
 
 
746
 
 
747
        # ******************************* FAX HEADER SETUP
 
748
            tui.header("FAX HEADER SETUP")
 
749
 
 
750
            if auto:
 
751
                setup_fax = False
 
752
            else:
 
753
                while True:
 
754
                    user_input = raw_input(log.bold("\nWould you like to perform fax header setup (y=yes*, n=no, q=quit) ?")).strip().lower()
 
755
 
 
756
                    if user_input == 'q':
 
757
                        log.info("OK, done.")
 
758
                        sys.exit(0)
 
759
 
 
760
                    if not user_input:
 
761
                        user_input = 'y'
 
762
 
 
763
                    setup_fax = (user_input == 'y')
 
764
 
 
765
                    if user_input in ('y', 'n', 'q'):
 
766
                        break
 
767
 
 
768
                    log.error("Please enter 'y' or 'n'")
 
769
 
 
770
            if setup_fax:
 
771
                d = fax.getFaxDevice(fax_uri, disable_dbus=True)
 
772
 
 
773
                try:
 
774
                    d.open()
 
775
                except Error:
 
776
                    log.error("Unable to communicate with the device. Please check the device and try again.")
 
777
                else:
 
778
                    try:
 
779
                        tries = 0
 
780
                        ok = True
 
781
 
 
782
                        while True:
 
783
                            tries += 1
 
784
 
 
785
                            try:
 
786
                                current_phone_num = str(d.getPhoneNum())
 
787
                                current_station_name = str(d.getStationName())
 
788
                            except Error:
 
789
                                log.error("Could not communicate with device. Device may be busy. Please wait for retry...")
 
790
                                time.sleep(5)
 
791
                                ok = False
 
792
 
 
793
                                if tries > 12:
 
794
                                    break
 
795
 
 
796
                            else:
 
797
                                ok = True
 
798
                                break
 
799
 
 
800
                        if ok:
 
801
                            while True:
 
802
                                if current_phone_num:
 
803
                                    phone_num = raw_input(log.bold("\nEnter the fax phone number for this device (c=use current:'%s'*, q=quit) ?" % current_phone_num))
 
804
                                else:
 
805
                                    phone_num = raw_input(log.bold("\nEnter the fax phone number for this device (q=quit) ?"))
 
806
                                if phone_num.strip().lower() == 'q':
 
807
                                    log.info("OK, done.")
 
808
                                    sys.exit(0)
 
809
 
 
810
                                if current_phone_num and (not phone_num or phone_num.strip().lower() == 'c'):
 
811
                                    phone_num = current_phone_num
 
812
 
 
813
                                if len(phone_num) > 50:
 
814
                                    log.error("Phone number length is too long (>50 characters). Please enter a shorter number.")
 
815
                                    continue
 
816
 
 
817
                                ok = True
 
818
                                for x in phone_num:
 
819
                                    if x not in '0123456789-(+) ':
 
820
                                        log.error("Invalid characters in phone number. Please only use 0-9, -, (, +, and )")
 
821
                                        ok = False
 
822
                                        break
 
823
 
 
824
                                if not ok:
 
825
                                    continue
 
826
 
 
827
                                break
 
828
 
 
829
                            while True:
 
830
                                if current_station_name:
 
831
                                    station_name = raw_input(log.bold("\nEnter the name and/or company for this device (c=use current:'%s'*, q=quit) ?" % current_station_name))
 
832
                                else:
 
833
                                    station_name = raw_input(log.bold("\nEnter the name and/or company for this device (q=quit) ?"))
 
834
                                if station_name.strip().lower() == 'q':
 
835
                                    log.info("OK, done.")
 
836
                                    sys.exit(0)
 
837
 
 
838
                                if current_station_name and (not station_name or station_name.strip().lower() == 'c'):
 
839
                                    station_name = current_station_name
 
840
 
 
841
 
 
842
                                if len(station_name) > 50:
 
843
                                    log.error("Name/company length is too long (>50 characters). Please enter a shorter name/company.")
 
844
                                    continue
 
845
                                break
 
846
 
 
847
                            try:
 
848
                                d.setStationName(station_name)
 
849
                                d.setPhoneNum(phone_num)
 
850
                            except Error:
 
851
                                log.error("Could not communicate with device. Device may be busy.")
 
852
                            else:
 
853
                                log.info("\nParameters sent to device.")
 
854
 
 
855
                    finally:
 
856
                        d.close()
 
857
 
 
858
        # ******************************* TEST PAGE
 
859
        if setup_print:
 
860
            print_test_page = False
 
861
 
 
862
            tui.header("PRINTER TEST PAGE")
 
863
 
 
864
            if auto:
 
865
                if testpage_in_auto_mode:
 
866
                    print_test_page = True
 
867
            else:
 
868
                ok, print_test_page = tui.enter_yes_no("\nWould you like to print a test page")
 
869
                if not ok: sys.exit(0)
 
870
 
 
871
            if print_test_page:
 
872
                path = utils.which('hp-testpage')
 
873
 
 
874
                if printer_name:
 
875
                    param = "-p%s" % printer_name
 
876
                else:
 
877
                    param = "-d%s" % print_uri
 
878
 
 
879
                if len(path) > 0:
 
880
                    cmd = 'hp-testpage %s' % param
 
881
                else:
 
882
                    cmd = 'python ./testpage.py %s' % param
 
883
 
 
884
                log.debug(cmd)
 
885
 
 
886
                os.system(cmd)
 
887
 
 
888
    except KeyboardInterrupt:
 
889
        log.error("User exit")
 
890
 
 
891
log.info("")
 
892
log.info("Done.")
 
893