Replace 'tap' to 'spaces' to make gbs build succeed
[platform/upstream/hplip.git] / firmware.py
1 #!/usr/bin/env 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, Sarbeswar Meher
21 #
22
23 __version__ = '2.4'
24 __title__ = 'Firmware Download Utility'
25 __mod__ = 'hp-firmware'
26 __doc__ = "Download firmware to a device that requires downloaded firmware to function. (Note: Most printers do not require the use of this utility)."
27
28 # Std Lib
29 import sys
30 import getopt
31 import gzip
32 import operator
33 import time
34 import os
35
36 # Local
37 from base.g import *
38 from base import device, status, utils, tui, module
39 from prnt import cups
40
41
42 try:
43     mod = module.Module(__mod__, __title__, __version__, __doc__, None,
44                         (INTERACTIVE_MODE, GUI_MODE, NON_INTERACTIVE_MODE),
45                         (UI_TOOLKIT_QT4, UI_TOOLKIT_QT3), True, True)
46
47     mod.setUsage(module.USAGE_FLAG_DEVICE_ARGS,
48         extra_options=[
49         ("Use USB IDs to specify printer:", "-s bbb:ddd, where bbb is the USB bus ID and ddd is the USB device ID. The ':' and all leading zeroes must be present.", "option", False),
50         ("Seconds to delay before download:", "-y<secs> or --delay=<secs> (float value, e.g. 0.5)", "option", False)],
51          see_also_list=['hp-plugin', 'hp-toolbox'])
52
53     opts, device_uri, printer_name, mode, ui_toolkit, lang = \
54         mod.parseStdOpts('y:s:', ['delay='])
55
56     device_uri = None
57     printer_name = None
58     usb_bus_node = None
59     usb_bus_id = None
60     usb_device_id = None
61     silent = False
62     delay = 0.0
63
64     for o, a in opts:
65         if o == '-s':
66             silent = True
67             try:
68                 usb_bus_id, usb_device_id = a.split(":", 1)
69                 log.debug("USB bus ID: %s" % usb_bus_id)
70                 log.debug("USB device ID: %s" % usb_device_id)
71             except ValueError:
72                 log.error("Invalid USB IDs: %s" % a)
73                 sys.exit(1)
74
75             if len(usb_bus_id) != 3 or len(usb_device_id) != 3:
76                 log.error("Invalid USB IDs '%s'. Must be the format: bbb.ddd" % a)
77                 sys.exit(1)
78
79             usb_bus_node = a
80             mode = NON_INTERACTIVE_MODE
81
82         elif o in ('-y', '--delay'):
83             try:
84                 delay = float(a)
85             except ValueError:
86                 log.error("Invalid delay value. Must be numeric (float) value. Setting delay to 0.0")
87                 delay = 0.0
88
89             mode = NON_INTERACTIVE_MODE
90
91
92     if ui_toolkit == 'qt4':
93         if not utils.canEnterGUIMode4():
94             log.error("%s -u/--gui requires Qt4 GUI support. Entering interactive mode." % __mod__)
95             mode = INTERACTIVE_MODE
96
97     elif ui_toolkit == 'qt3':
98        if not utils.canEnterGUIMode():
99             log.error("%s -u/--gui requires Qt3 GUI support. Entering interactive mode." % __mod__)
100             mode = INTERACTIVE_MODE
101
102     if mode in (GUI_MODE, INTERACTIVE_MODE):
103         mod.quiet = False
104
105     if mode == GUI_MODE:
106         if ui_toolkit == 'qt4':
107            try:
108             from PyQt4.QtGui import QApplication
109             from ui4.firmwaredialog import FirmwareDialog
110            except ImportError:
111             log.error("Unable to load Qt4 support. Is it installed?")
112             sys.exit(1)
113
114         if ui_toolkit == 'qt3':
115            try:
116             from qt import *
117             from ui.firmwaredialog import FirmwareDialog
118            except ImportError:
119             log.error("Unable to load Qt3 support. Is it installed?")
120             sys.exit(1)
121
122
123         mod.showTitle()
124
125         device_uri = mod.getDeviceUri(device_uri, printer_name,
126             filter={'fw-download': (operator.gt, 0)})
127
128         if 1:
129             app = QApplication(sys.argv)
130
131             dialog = FirmwareDialog(None, device_uri)
132             dialog.show()
133             try:
134                 log.debug("Starting GUI loop...")
135                 if ui_toolkit == 'qt4':
136                    app.exec_()
137                 elif ui_toolkit == 'qt3':
138                    dialog.exec_loop()
139             except KeyboardInterrupt:
140                 sys.exit(0)
141
142         sys.exit(0)
143
144     mod.showTitle()
145
146     if usb_bus_node is not None:
147         log.debug("USB bus node: %s" % usb_bus_node)
148         device_uri, sane_uri, fax_uri = device.makeURI(usb_bus_node, 1)
149
150         if not device_uri:
151             log.error("Invalid USB Device ID or USB bus ID. No device found.")
152             sys.exit(1)
153
154     else:
155         device_uri = mod.getDeviceUri(device_uri, printer_name,
156             filter={'fw-download': (operator.gt, 0)})
157
158     try:
159         d = device.Device(device_uri, printer_name)
160     except Error:
161         log.error("Error opening device. Exiting.")
162         sys.exit(1)
163
164     try:
165         if delay:
166              time.sleep(delay)
167
168         try:
169             d.open()
170             d.queryModel()
171         except Error, e:
172             log.error("Error opening device (%s). Exiting." % e.msg)
173             sys.exit(1)
174
175         fw_download = d.mq.get('fw-download', 0)
176
177         if fw_download:
178             if d.downloadFirmware(usb_bus_id, usb_device_id):
179                 if not silent:
180                     log.info("Done.")
181                 sys.exit(0)
182
183             else:
184                 log.error("Firmware download failed.")
185                 sys.exit(1)
186
187         else:
188             log.error("Device %s does not support or require firmware download." % device_uri)
189             sys.exit(1)
190
191     finally:
192         d.close()
193
194 except KeyboardInterrupt:
195     log.error("User exit")