2
# -*- coding: utf-8 -*-
4
# (c) Copyright @2015 HP Development Company, L.P.
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.
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.
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
20
# Author: Amarnath Chitumalla
32
from . import utils, tui
33
from base import password, validation
34
from base.codes import *
35
from base.strings import *
38
##### Global variables ###
39
HPLIP_INFO_SITE ="http://hplip.sourceforge.net/hplip_web.conf"
43
########### methods ###########
46
def get_usb_details(vid_pid):
48
param_result = {"idVendor":'', "iProduct":'', "bNumInterfaces":'', "bInterfaceClass":''}
49
param_search = {"idVendor": re.compile(r"""\s*idVendor\s*([0-9a-fx]{1,})\s*.*""", re.I),
50
"iProduct" : re.compile(r"""\s*iProduct\s*[0-9a-fx]{1,}\s*(.*)""", re.I),
51
"bNumInterfaces" : re.compile(r"""\s*bNumInterfaces\s*(\d{1,})\s*.*""", re.I),
52
"bInterfaceClass" : re.compile(r"""\s*bInterfaceClass\s*(\d{1,})\s*.*""", re.I) }
54
lsusb_cmd = utils.which('lsusb',True)
56
sts,out = utils.run("%s -d %s -v"%(lsusb_cmd, vid_pid), passwordObj = None, pswd_msg='', log_output=False)
58
for l in out.splitlines():
59
for s in param_search:
62
if param_search[s].match(l):
63
param_result[s] = param_search[s].match(l).group(1)
65
log.warn("TBD... Shouldn't have entered into this condition. key[%s]"%s)
67
if "idVendor" == s and param_result[s].lower() != "0x03f0": # if non-HP vendor, ignoring usb parsing.
69
elif "iProduct" == s and param_result[s] == "":
74
if result_cnt == len(param_result): # To avoid extra parsing...
77
return True, param_result
80
# get_smartinstall_enabled_devices function checks CD-ROM enabled devices.
84
# smartinstall_dev_list (list) --> Returns CD-ROM enabled device list.
86
def get_smartinstall_enabled_devices():
87
smartinstall_dev_list=[]
88
lsusb_cmd = utils.which('lsusb',True)
91
log.error("Failed to find the lsusb command")
92
return smartinstall_dev_list
95
sts,out = utils.run(lsusb_cmd)
97
log.error("Failed to run the %s command"%lsusb_cmd)
98
return smartinstall_dev_list
100
for d in out.splitlines():
101
usb_dev_pat = re.compile(r""".*([0-9a-f]{4}:([0-9a-f]{4}))\s*""", re.I)
103
if usb_dev_pat.match(d):
104
vid_pid = usb_dev_pat.match(d).group(1)
106
bsts, usb_params = get_usb_details(vid_pid)
108
continue # These are not HP-devices
110
log.debug("Product['%s'],Interfaces[%s],InterfaceClass[%s]"%(usb_params["iProduct"], usb_params["bNumInterfaces"],usb_params["bInterfaceClass"]))
111
if usb_params["bNumInterfaces"] == '1' and usb_params["bInterfaceClass"] == '8' and "laserjet" in usb_params["iProduct"].lower(): #'8' is MASS STORAGE
112
smartinstall_dev_list.append(usb_params["iProduct"])
115
log.warn("Failed to find vid and pid for USB device[%s]"%d)
120
if smartinstall_dev_list:
121
smartinstall_dev_list = utils.uniqueList(smartinstall_dev_list)
123
return smartinstall_dev_list
126
def check_SmartInstall():
127
devices = get_smartinstall_enabled_devices()
134
def get_SmartInstall_tool_info():
135
url, file_name = "", ""
136
if not utils.check_network_connection():
137
log.error("Internet connection not found.")
139
sts, HPLIP_file = utils.download_from_network(HPLIP_INFO_SITE)
141
hplip_si_conf = ConfigBase(HPLIP_file)
142
url = hplip_si_conf.get("SMART_INSTALL","reference","")
144
file_name = 'SmartInstallDisable-Tool.run'
146
log.error("Failed to download %s."%HPLIP_INFO_SITE)
148
log.error("Failed to download %s."%HPLIP_INFO_SITE)
150
return url, file_name
152
def validate(mode, smart_install_run, smart_install_asc, req_checksum=''):
155
calc_checksum = utils.get_checksum(open(smart_install_run, 'r').read())
156
log.debug("File checksum=%s" % calc_checksum)
158
if req_checksum and req_checksum != calc_checksum:
159
return ERROR_FILE_CHECKSUM, queryString(ERROR_CHECKSUM_ERROR, 0, plugin_file)
161
#Validate Digital Signature
162
gpg_obj = validation.GPG_Verification()
163
digsig_sts, error_str = gpg_obj.validate(smart_install_run, smart_install_asc)
165
return digsig_sts, smart_install_run, smart_install_asc, error_str
169
def download(mode, passwordObj):
170
if not utils.check_network_connection():
171
log.error("Internet connection not found.")
172
return ERROR_NO_NETWORK, "" , "" ,queryString(ERROR_NO_NETWORK)
175
sts, HPLIP_file = utils.download_from_network(HPLIP_INFO_SITE)
177
hplip_si_conf = ConfigBase(HPLIP_file)
178
source = hplip_si_conf.get("SMART_INSTALL","url","")
180
log.error("Failed to download %s."%HPLIP_INFO_SITE)
181
return ERROR_FAILED_TO_DOWNLOAD_FILE, "" , "", queryString(ERROR_FAILED_TO_DOWNLOAD_FILE, 0, HPLIP_INFO_SITE)
183
sts, smart_install_run = utils.download_from_network(source)
185
log.error("Failed to download %s."%source)
186
return ERROR_FAILED_TO_DOWNLOAD_FILE, "" , "", queryString(ERROR_FAILED_TO_DOWNLOAD_FILE, 0, source)
188
sts, smart_install_asc = utils.download_from_network(source+'.asc')
190
log.error("Failed to download %s."%(source+'.asc'))
191
return ERROR_FAILED_TO_DOWNLOAD_FILE, "" , "", queryString(ERROR_FAILED_TO_DOWNLOAD_FILE, 0, source + ".asc")
193
digsig_sts, smart_install_run, smart_install_asc, error_str = validate(mode, smart_install_run, smart_install_asc)
195
return digsig_sts, smart_install_run, smart_install_asc, error_str
198
def disable(mode, ui_toolkit='qt4', dialog=None, app=None, passwordObj = None):
200
dev_list = get_smartinstall_enabled_devices()
202
log.debug("No Smart Install Device found")
203
return ERROR_NO_SI_DEVICE, queryString(ERROR_NO_SI_DEVICE)
205
return_val = ERROR_FAILED_TO_DISABLE_SI
206
return_error_str = queryString(ERROR_FAILED_TO_DISABLE_SI)
207
url, file_name = get_SmartInstall_tool_info()
208
printer_names = utils.list_to_string(dev_list)
212
if ui_toolkit == 'qt3':
214
from ui.setupform import FailureMessageUI
216
log.error("Smart Install is enabled in %s device(s).\nAuto Smart Install disable is not supported in QT3.\nPlease refer link \'%s\' to disable manually"%(printer_names,url))
218
FailureMessageUI("Smart Install is enabled in %s device(s).\n\nAuto Smart Install disable is not supported in QT3.\nPlease refer link \'%s\' to disable manually"%(printer_names,url))
221
if not utils.canEnterGUIMode4():
222
log.error("%s requires GUI support . Is Qt4 installed?" % __mod__)
223
return ERROR_FAILED_TO_DISABLE_SI, queryString(ERROR_FAILED_TO_DISABLE_SI)
225
if dialog and app: # If QT app already opened, re-using same object
226
dialog.init(printer_names, "", QUEUES_SMART_INSTALL_ENABLED)
227
else: # If QT object is not created, creating QT app
229
from ui4.queuesconf import QueuesDiagnose
231
log.error("Unable to load Qt4 support. Is it installed?")
232
else: # app = QApplication(sys.argv) # caller needs to inoke this, if already QApplication object is not created.
233
dialog = QueuesDiagnose(None, printer_names ,"",QUEUES_SMART_INSTALL_ENABLED)
235
log.debug("Starting GUI loop...")
238
if check_SmartInstall():
239
dialog.showMessage("Failed to disable smart install.\nPlease refer link \'%s\' for more information" %url)
241
dialog.showSuccessMessage("Smart install disabled successfully.")
246
log.error("Smart Install is enabled in %s device(s). "%printer_names)
247
response, value = tui.enter_choice("Do you want to download and disable smart install?(y=yes*, n=no):",['y', 'n'], 'y')
249
if not response or value != 'y': #User exit
250
return_val = ERROR_FAILED_TO_DISABLE_SI
251
return_error_str = queryString(ERROR_FAILED_TO_DISABLE_SI)
254
sts, smart_install_run, smart_install_asc, error_str = download(mode, passwordObj)
259
if sts == ERROR_SUCCESS:
262
elif sts in (ERROR_UNABLE_TO_RECV_KEYS, ERROR_DIGITAL_SIGN_NOT_FOUND):
263
response, value = tui.enter_yes_no("Digital Sign verification failed, Do you want to continue?")
264
if not response or not value:
266
else: # Continue without validation succes.
269
return_error_str = queryString(sts)
272
sts, out = utils.run("sh %s"%smart_install_run)
274
# Once smart install disabler installation completed, cross verifying to ensure no smart install devices found
275
if sts or check_SmartInstall():
276
log.error("Failed to disable smart install .")
277
log.error("Please refer link \'%s\' to disable manually"%url)
278
return_val = ERROR_FAILED_TO_DISABLE_SI
279
return_error_str = queryString(ERROR_FAILED_TO_DISABLE_SI)
281
log.info("Smart install disabled successfully.")
282
return_val = ERROR_SUCCESS
283
return_error_str = ""
285
except KeyboardInterrupt:
286
log.error("User exit")
289
return return_val ,return_error_str