~mimose/+junk/hplip-plugin-3.16.11

« back to all changes in this revision

Viewing changes to plugin_install.py

  • Committer: guoyalong
  • Date: 2017-09-22 10:03:54 UTC
  • Revision ID: guoyalong@kylinos.cn-20170922100354-b3h4swua28h7bqjv
Debuild from hplip-plugin-3.16.11.run.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
#
3
 
# (c) Copyright 2003-2015 HP Development Company, L.P.
4
 
#
5
 
# This program is free software; you can redistribute it and/or modify
6
 
# it under the terms of the GNU General Public License as published by
7
 
# the Free Software Foundation; either version 2 of the License, or
8
 
# (at your option) any later version.
9
 
#
10
 
# This program is distributed in the hope that it will be useful,
11
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
# GNU General Public License for more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License
16
 
# along with this program; if not, write to the Free Software
17
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18
 
#
19
 
# Author: Don Welch
20
 
#
21
 
 
22
 
__version__ = '3.0'
23
 
__title__ = 'Plugin Installer'
24
 
__doc__ = "Self-installs the HPLIP plugin."
25
 
__mod__ = "hplip-plugin-install"
26
 
 
27
 
# Std Lib
28
 
import sys
29
 
import os
30
 
import os.path
31
 
import re
32
 
import time
33
 
try:
34
 
    import configparser
35
 
except ImportError:
36
 
    import ConfigParser as configparser
37
 
 
38
 
import getopt
39
 
import shutil
40
 
import stat
41
 
 
42
 
#if os.geteuid() == 0:
43
 
#    print("hp-plugin should not be run as root/superuser. Exiting.")
44
 
#    sys.exit(1)
45
 
 
46
 
def startGUIInstall():
47
 
    ok = installPlugin()
48
 
    app.quit()
49
 
 
50
 
 
51
 
def installPlugin():
52
 
    exec_str = sys.executable
53
 
    passwordObj = password.Password(mode)
54
 
    cmd = "%s installPlugin.py"%exec_str
55
 
    cmd = passwordObj.getAuthCmd()%cmd
56
 
    status, output = utils.run(cmd, passwordObj)
57
 
    if status == 0:
58
 
        result = True
59
 
    else:
60
 
        print("Plugin installation failed")
61
 
        result = False
62
 
    return result
63
 
 
64
 
config_file = '/etc/hp/hplip.conf'
65
 
if os.path.exists(config_file):
66
 
    config = configparser.ConfigParser()
67
 
    config.read(config_file)
68
 
    try:
69
 
        home_dir = config.get('dirs', 'home')
70
 
    except:
71
 
        print("Error setting home directory: home= under [dirs] in %s not found. Is HPLIP installed?" % config_file)
72
 
        sys.exit(1)
73
 
else:
74
 
    print("Error setting home directory: %s not found. Is HPLIP installed?" % config_file)
75
 
    sys.exit(1)
76
 
 
77
 
if not home_dir or not os.path.exists(home_dir):
78
 
    print("Error setting home directory: Home directory %s not found. Is HPLIP installed?" % home_dir)
79
 
    sys.exit(1)
80
 
sys.path.insert(0, home_dir)
81
 
 
82
 
try:
83
 
    from base.g import *
84
 
    from base.codes import *
85
 
    from base import utils, tui, module
86
 
except ImportError:
87
 
    print("Error importing HPLIP modules.  Is HPLIP installed?")
88
 
    sys.exit(1)
89
 
 
90
 
mod = module.Module(__mod__, __title__, __version__, __doc__, None,
91
 
                    (INTERACTIVE_MODE, GUI_MODE),
92
 
                    (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4, UI_TOOLKIT_QT5), True)
93
 
 
94
 
mod.setUsage()
95
 
 
96
 
opts, device_uri, printer_name, mode, ui_toolkit, loc = \
97
 
    mod.parseStdOpts('c:v:p',
98
 
        ['count=', 'pkit-version='],
99
 
        handle_device_printer=False)
100
 
 
101
 
version = None
102
 
num_files = 0
103
 
user_pkit_version = None
104
 
 
105
 
for o, a in opts:
106
 
    if o in ('-c', '--count'):
107
 
        try:
108
 
            num_files = int(a)
109
 
        except TypeError:
110
 
            num_files = 0
111
 
 
112
 
    elif o in ('-v', '--version'):
113
 
        version = a
114
 
 
115
 
    elif o in ('--pkit-version'):
116
 
        try:
117
 
            user_pkit_version = int(a)
118
 
        except:
119
 
            log.error("-v or --version require an integer argument")
120
 
            sys.exit(1)
121
 
        if user_pkit_version < 0 or user_pkit_version > 1:
122
 
            log.error("invalid PolicyKit version...use 0 or 1")
123
 
 
124
 
    elif o in ('-p'):
125
 
        pkit_installed = False
126
 
 
127
 
if version is None:
128
 
    version = open("version.txt", "r").read().strip()
129
 
 
130
 
version_mismatch = version != prop.installed_version
131
 
 
132
 
if version_mismatch:
133
 
    log.error("Plug-in version mismatch:")
134
 
    log.error("  Plug-in version: %s" % version)
135
 
    log.error("  Installed HPLIP version: %s" % prop.installed_version)
136
 
    log.error("  Installed HPLIP version and plug-in version must match.")
137
 
    sys.exit(1)
138
 
 
139
 
else:
140
 
    log.info("Plug-in version: %s" % version)
141
 
    log.info("Installed HPLIP version: %s" % prop.installed_version)
142
 
 
143
 
log.info("Number of files to install: %d" % num_files)
144
 
log.info("")
145
 
 
146
 
PKIT = utils.to_bool(sys_conf.get('configure', 'policy-kit'))
147
 
try:
148
 
    from base.pkit import *
149
 
    pkit_version = policykit_version()
150
 
    if pkit_version:
151
 
        if not user_pkit_version is None:
152
 
            pkit_version = user_pkit_version
153
 
        log.debug("pkit_version %d" % pkit_version)
154
 
    try:
155
 
        pkit = PolicyKit(pkit_version)
156
 
        pkit_installed = True
157
 
    except dbus.DBusException:
158
 
        pkit_installed = False
159
 
except ImportError:
160
 
    log.error("Unable to load pkit...is HPLIP installed?")
161
 
    sys.exit(1)
162
 
if not PKIT:
163
 
    pkit_installed = False
164
 
log.debug("pkit_installed %s" % pkit_installed)
165
 
 
166
 
if mode == INTERACTIVE_MODE:
167
 
 
168
 
 
169
 
    log.info(log.bold("You must agree to the license terms before installing the plug-in:"))
170
 
    log.info()
171
 
 
172
 
    license_f = open("license.txt", "r")
173
 
 
174
 
    for line1 in license_f:
175
 
        if line1:
176
 
            for line2 in tui.format_paragraph(line1):
177
 
                log.info(line2)
178
 
 
179
 
    ok, ans = tui.enter_yes_no("\nDo you accept the license terms for the plug-in")
180
 
 
181
 
    if not ok or not ans:
182
 
        log.error("License agreement not accepted. Exiting.")
183
 
        sys.exit(1)
184
 
 
185
 
    if not installPlugin():
186
 
        log.error("Plugin installation failed")
187
 
 
188
 
else: # GUI_MODE
189
 
 
190
 
    if ui_toolkit == 'qt3':
191
 
 
192
 
        try:
193
 
            from qt import *
194
 
            from ui.pluginlicenseform import PluginLicenseForm
195
 
        except ImportError:
196
 
            log.error("Unable to load Qt3 support. Is it installed?")
197
 
            sys.exit(1)
198
 
 
199
 
        app = QApplication(sys.argv)
200
 
        QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
201
 
 
202
 
 
203
 
        if version_mismatch:
204
 
            QMessageBox.critical(None,
205
 
                                 "HP Device Manager - Plug-in Installer",
206
 
                                 "Plug-in version mismatch. Installed HPLIP version (%s) and plug-in version (%s) must match." % (prop.installed_version, version))
207
 
                                  # QMessageBox.Ok|
208
 
                                  # QMessageBox.NoButton)
209
 
                                  # QMessageBox.NoButton)
210
 
 
211
 
            sys.exit(1)
212
 
 
213
 
        dlg = PluginLicenseForm(open("license.txt", "r").read())
214
 
 
215
 
        if dlg.exec_loop() == QDialog.Accepted:
216
 
            ok = installPlugin()
217
 
            if not ok:
218
 
                log.debug("Plug-in installation failed.")
219
 
                sys.exit(1)
220
 
 
221
 
        else:
222
 
            log.error("License agreement not accepted. Exiting.")
223
 
            sys.exit(1)
224
 
 
225
 
    elif ui_toolkit == "qt4": # qt4
226
 
 
227
 
        try:
228
 
            from PyQt4.QtGui import *
229
 
            from PyQt4.QtCore import *
230
 
            from ui4.pluginlicensedialog import PluginLicenseDialog
231
 
        except ImportError:
232
 
            log.error("Unable to load Qt4 support. Is it installed? Try running with -i or --qt3 instead.")
233
 
            sys.exit(1)
234
 
    elif ui_toolkit == "qt5":
235
 
        try:
236
 
            log.note("Using PyQt5")
237
 
            from PyQt5.QtGui import *
238
 
            from PyQt5.QtCore import *
239
 
            from PyQt5.QtWidgets import *
240
 
            from ui5.pluginlicensedialog import PluginLicenseDialog
241
 
        except ImportError:
242
 
            log.error("Unable to load Qt5 support. Is it installed? Try running with -i or --qt3 instead.")
243
 
            sys.exit(1)
244
 
 
245
 
 
246
 
    app = QApplication(sys.argv)
247
 
    
248
 
    if version_mismatch:
249
 
        QMessageBox.critical(None,
250
 
                             "HP Device Manager - Plug-in Installer",
251
 
                             "Plug-in version mismatch. Installed HPLIP version (%s) and plug-in version (%s) must match." % (prop.installed_version, version))
252
 
        #QMessageBox.Ok,
253
 
        #QMessageBox.NoButton,
254
 
        #QMessageBox.NoButton)
255
 
 
256
 
        sys.exit(1)
257
 
 
258
 
    dlg = PluginLicenseDialog(None, open("license.txt", "r").read())
259
 
 
260
 
    if dlg.exec_() == QDialog.Accepted:
261
 
        ok = installPlugin()
262
 
        if not ok:
263
 
            log.debug("Plug-in installation failed")
264
 
            sys.exit(1)
265
 
        else:
266
 
            # changing permission for hplip.state
267
 
            cmd="chmod 644 /var/lib/hp/hplip.state"
268
 
            utils.run(cmd)
269
 
 
270
 
    else:
271
 
        log.error("License agreement not accepted. Exiting.")
272
 
        sys.exit(1)
273
 
 
274
 
log.info("")
275
 
log.info("Done.")
276
 
sys.exit(0)