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

« back to all changes in this revision

Viewing changes to check-plugin.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/env python
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
# (c) Copyright 2011-2014 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: Suma Byrappa, Amarnath Chitumalla
 
21
#
 
22
#
 
23
 
 
24
__version__ = '1.1'
 
25
__title__ = 'AutoConfig Utility for Plug-in Installation'
 
26
__mod__ = 'hp-check-plugin'
 
27
__doc__ = "Auto config utility for HPLIP supported multifunction Devices for installing proprietary plug-ins."
 
28
 
 
29
# Std Lib
 
30
import sys
 
31
import os
 
32
import os.path
 
33
import getopt
 
34
import signal
 
35
import operator
 
36
import time
 
37
 
 
38
# Local
 
39
from base.g import *
 
40
from base import utils, device, tui, module, pkit
 
41
from installer import core_install
 
42
 
 
43
 
 
44
# Temp values for testing; May not be needed
 
45
username = ""
 
46
device_uri = ""
 
47
printer_name = ""
 
48
LOG_FILE = "/var/log/hp/hplip_ac.log" 
 
49
DBUS_SERVICE='com.hplip.StatusService'
 
50
 
 
51
##### METHODS #####
 
52
 
 
53
# Send dbus event to hpssd on dbus system bus
 
54
def send_message(device_uri, printer_name, event_code, username, job_id, title, pipe_name=''):
 
55
    log.debug("send_message() entered")
 
56
    args = [device_uri, printer_name, event_code, username, job_id, title, pipe_name]
 
57
    msg = lowlevel.SignalMessage('/', DBUS_SERVICE, 'Event')
 
58
    msg.append(signature='ssisiss', *args)
 
59
 
 
60
    SystemBus().send_message(msg)
 
61
    log.debug("send_message() returning")
 
62
 
 
63
# Plugin installation
 
64
def install_Plugin(systray_running_status, run_directly=False):
 
65
    if run_directly:
 
66
        if not utils.canEnterGUIMode4():
 
67
            log.error("%s requires GUI support . Is Qt4 installed?" % __mod__)
 
68
            sys.exit(1)
 
69
 
 
70
        try:
 
71
            from PyQt4.QtGui import QApplication, QMessageBox
 
72
            from ui4.plugindiagnose import PluginDiagnose
 
73
            from installer import core_install
 
74
        except ImportError:
 
75
            log.error("Unable to load Qt4 support. Is it installed?")
 
76
            sys.exit(1)
 
77
 
 
78
        app = QApplication(sys.argv)
 
79
        plugin = PLUGIN_REQUIRED 
 
80
        plugin_reason = PLUGIN_REASON_NONE
 
81
        ok, sudo_ok = pkit.run_plugin_command(plugin == PLUGIN_REQUIRED, plugin_reason)
 
82
        if not ok or not sudo_ok:
 
83
            log.error("Failed to install plug-in.")
 
84
    elif systray_running_status:
 
85
        send_message( device_uri,  "", EVENT_AUTO_CONFIGURE, username, 0, "AutoConfig")
 
86
        log.debug("Event EVENT_AUTO_CONFIGURE sent to hp-systray to invoke hp-plugin")
 
87
    else:
 
88
        log.error("Run hp-systray manually and re-plugin printer")
 
89
        #TBD: needs to run hp-plugin in silent mode. or needs to show error UI to user.
 
90
 
 
91
     
 
92
#Installs/Uploads the firmware to device once plugin installation is completed.
 
93
def install_firmware(Plugin_Installation_Completed, USB_param):
 
94
    #timeout check for plugin installation
 
95
    sleep_timeout = 6000        # 10 mins time out
 
96
    while Plugin_Installation_Completed is False and sleep_timeout != 0:
 
97
        time.sleep(0.3) #0.3 sec delay
 
98
        sleep_timeout = sleep_timeout -3
 
99
        
 
100
        ps_plugin,output = utils.Is_Process_Running('hp-plugin')
 
101
        ps_diagnose_plugin,output = utils.Is_Process_Running('hp-diagnose_plugin')
 
102
 
 
103
        if ps_plugin is False and ps_diagnose_plugin is False:            
 
104
            Plugin_Installation_Completed = True
 
105
            if core.check_for_plugin() == PLUGIN_INSTALLED:
 
106
                break
 
107
            else:
 
108
                log.error("Failed to download firmware required files. manually run hp-plugin command in terminal fisrt")
 
109
                sys.exit(1)
 
110
    
 
111
    execmd="hp-firmware"
 
112
    options=""
 
113
    if USB_param is not None:
 
114
        options += " -y3 %s"%(USB_param)
 
115
    if log_level is 'debug':
 
116
        options += " -g"
 
117
 
 
118
    cmd= execmd + options
 
119
    log.info("Starting Firmware installation.")
 
120
    log.debug("Running command : %s " %cmd)
 
121
    Status, out=utils.run(cmd)
 
122
 
 
123
#    if Status == 0:
 
124
#        log.info("Installed firmware ")
 
125
#    else:
 
126
#        log.error("Failed to install firmware = %s" %Status)
 
127
 
 
128
 
 
129
#Usage details
 
130
USAGE = [(__doc__, "", "name", True),
 
131
         ("Usage: %s [OPTIONS] [USB bus:device]" % __mod__, "", "summary", True),
 
132
         utils.USAGE_OPTIONS,
 
133
         ("Install Plug-in through HP System Tray:", "-m (Default)", "option", False),
 
134
         ("Install Plug-in through hp-plugin:", "-p", "option", False),
 
135
         ("Download firmware into the device:", "-f", "option", False),
 
136
         utils.USAGE_HELP,
 
137
         utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
 
138
         utils.USAGE_EXAMPLES,
 
139
          ("Install plugin:", "$%s 001:002"%(__mod__), "example", False),
 
140
          ("Install plugin and firmware:", "$%s -f 001:002"%(__mod__), "example", False),
 
141
         utils.USAGE_NOTES,
 
142
         ("-m and -p options can't be used together. ","","note",False),
 
143
        ]
 
144
 
 
145
 
 
146
def usage(typ='text'):
 
147
    if typ == 'text':
 
148
        utils.log_title(__title__, __version__)
 
149
 
 
150
    utils.format_text(USAGE, typ, __title__, __mod__, __version__)
 
151
    sys.exit(0)
 
152
 
 
153
##### MAIN #####
 
154
 
 
155
 
 
156
try:
 
157
    import dbus
 
158
    from dbus import SystemBus, lowlevel
 
159
except ImportError:
 
160
        log.error("hp-check-plugin Tool requires dBus and python-dbus")
 
161
        sys.exit(1)
 
162
try:
 
163
    mod = module.Module(__mod__, __title__, __version__, __doc__, USAGE, (INTERACTIVE_MODE, GUI_MODE), (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4), run_as_root_ok=True, quiet=True)
 
164
    opts, device_uri, printer_name, mode, ui_toolkit, loc = \
 
165
         mod.parseStdOpts('l:hHuUmMfFpPgG',['gui','help', 'help-rest', 'help-man', 'help-desc','logging='],handle_device_printer=False)
 
166
 
 
167
except getopt.GetoptError, e:
 
168
        log.error(e.msg)
 
169
        usage()
 
170
        sys.exit(1)
 
171
 
 
172
if os.getenv("HPLIP_DEBUG"):
 
173
        log.set_level('debug')
 
174
 
 
175
log_level = 'info'
 
176
Systray_Msg_Enabled = False
 
177
Plugin_option_Enabled = False
 
178
Firmware_Option_Enabled = False
 
179
GUI_Mode = True
 
180
Is_Plugin_Already_Installed = False
 
181
 
 
182
for o, a in opts:
 
183
    if o in ('-h','-H', '--help'):
 
184
        usage()
 
185
 
 
186
    elif o == '--help-rest':
 
187
        usage('rest')
 
188
 
 
189
    elif o == '--help-man':
 
190
        usage('man')
 
191
 
 
192
    elif o in ('-u', '-U','--gui'):
 
193
        # currenlty only GUI mode is supported. hence not reading this option
 
194
        GUI_Mode = True
 
195
 
 
196
#    elif o in ('-i', '-I', '--interactive'):
 
197
#        #this is future use
 
198
#        GUI_Mode = False 
 
199
 
 
200
    elif o == '--help-desc':
 
201
        print __doc__,
 
202
        sys.exit(0)
 
203
 
 
204
    elif o in ('-l', '--logging'):
 
205
        log_level = a.lower().strip()
 
206
 
 
207
    elif o in('-g', '-G'):
 
208
        log_level = 'debug'
 
209
 
 
210
    elif o in ('-m', '-M'):
 
211
        Systray_Msg_Enabled = True
 
212
    
 
213
    elif o in ('-p', '-P'):
 
214
        Plugin_option_Enabled = True
 
215
 
 
216
    elif o in ('-F','-f'):
 
217
        Firmware_Option_Enabled = True
 
218
 
 
219
if not log.set_level (log_level):
 
220
    usage()
 
221
    
 
222
try:
 
223
    param = mod.args[0]
 
224
except IndexError:
 
225
    param = ''
 
226
 
 
227
LOG_FILE = os.path.normpath(LOG_FILE)
 
228
log.info(log.bold("Saving output in log file: %s" % LOG_FILE))
 
229
if os.path.exists(LOG_FILE):
 
230
    os.remove(LOG_FILE)
 
231
 
 
232
log.set_logfile(LOG_FILE)
 
233
log.set_where(log.LOG_TO_CONSOLE_AND_FILE)
 
234
cmd="chmod 664 "+LOG_FILE
 
235
sts,output = utils.run(cmd)
 
236
if sts != 0:
 
237
    log.warn("Failed to change log file permissions: %s" %output)
 
238
 
 
239
cmd="chgrp lp "+LOG_FILE
 
240
sts,output = utils.run(cmd)
 
241
if sts != 0:
 
242
    log.warn("Failed to change log file group permissions: %s" %output)
 
243
 
 
244
log.debug(" hp-check-plugin started")
 
245
 
 
246
if Plugin_option_Enabled and Systray_Msg_Enabled:
 
247
    log.error("Both -m and -p options can't be used together.")
 
248
    usage()
 
249
    sys.exit(1)
 
250
 
 
251
log.debug("param=%s" % param)
 
252
if len(param) < 1:
 
253
    usage()
 
254
    sys.exit()
 
255
 
 
256
if param:
 
257
    device_uri, sane_uri, fax_uri = device.makeURI(param)
 
258
if not device_uri:
 
259
    log.error("This is not a valid device")
 
260
    sys.exit(0)
 
261
 
 
262
log.debug("\nSetting up device: %s\n" % device_uri)
 
263
#Query model and checks Plugin information.
 
264
mq = device.queryModelByURI(device_uri)
 
265
if not mq or mq.get('support-type', SUPPORT_TYPE_NONE) == SUPPORT_TYPE_NONE:
 
266
    log.error("Unsupported printer model.")
 
267
    sys.exit(1)
 
268
    
 
269
plugin = mq.get('plugin', PLUGIN_NONE)
 
270
if plugin == PLUGIN_NONE:
 
271
    log.debug("This is not a plugin device.")
 
272
    sys.exit()
 
273
 
 
274
if not Plugin_option_Enabled:
 
275
    Systray_Msg_Enabled = True
 
276
 
 
277
# checking whether HP-systray is running or not. Invokes, if systray is not running
 
278
status,output = utils.Is_Process_Running('hp-systray')
 
279
if status is False:
 
280
    Systray_Is_Running=False
 
281
    log.info("hp-systray is not running.")
 
282
    if os.getuid() == 0:
 
283
        log.error(" hp-systray must be running.\n Run \'hp-systray &\' in a terminal. ")
 
284
    else:
 
285
        log.info("Starting hp-systray service")
 
286
        child_pid = os.fork()
 
287
        if child_pid == 0:
 
288
            Systray_Is_Running=True
 
289
            status,output =utils.run('hp-systray &', True, None, 1, False)
 
290
            if status is not 0:
 
291
                log.error("Failed to start \'hp-systray\' service. Manually run \'hp-sysray &\' from terminal as non-root user.")
 
292
                Systray_Is_Running=False
 
293
            
 
294
            sys.exit()
 
295
        else:
 
296
            Systray_Is_Running=True
 
297
            time.sleep(2)
 
298
else:
 
299
    Systray_Is_Running=True
 
300
    log.debug("hp-systray service is running\n")
 
301
 
 
302
core = core_install.CoreInstall()
 
303
core.set_plugin_version()
 
304
plugin_sts = core.check_for_plugin()
 
305
if plugin_sts == PLUGIN_INSTALLED:
 
306
    log.info("Device Plugin is already installed")
 
307
    Is_Plugin_Already_Installed = True
 
308
elif plugin_sts == PLUGIN_VERSION_MISMATCH:
 
309
    log.info("HP Device Plug-in version mismatch or some files are corrupted")
 
310
else:
 
311
    log.info("HP Device Plug-in is not found.")
 
312
 
 
313
if Systray_Msg_Enabled:
 
314
    if not Is_Plugin_Already_Installed:
 
315
        install_Plugin( Systray_Is_Running)
 
316
 
 
317
elif Plugin_option_Enabled:
 
318
    if not Is_Plugin_Already_Installed:
 
319
        install_Plugin (Systray_Is_Running, True)               # needs to run hp-plugin without usig systray
 
320
 
 
321
if Firmware_Option_Enabled:
 
322
    if Is_Plugin_Already_Installed is False:
 
323
        Plugin_Installation_Completed = False
 
324
    else:
 
325
        Plugin_Installation_Completed = True
 
326
 
 
327
    install_firmware(Plugin_Installation_Completed,param)
 
328
 
 
329
log.info()
 
330
log.info("Done.")