1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3#
4# (c) Copyright 2003-2015 HP 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#
22from __future__ import print_function
23__version__ = '5.1'
24__title__ = 'HPLIP Installer'
25__mod__ = 'hplip-install'
26__doc__ = "Installer for HPLIP tarball (called automatically after invoking the .run file)."
27
28
29# Std Lib
30import getopt
31import os
32import os.path
33import sys
34import time
35import re
36
37# Local
38from base.g import *
39from base import utils, os_utils
40
41
42USAGE = [(__doc__, "", "name", True),
43         ("Usage: sh %s [OPTIONS]" % __mod__, "", "summary", True),
44         utils.USAGE_SPACE,
45         utils.USAGE_SPACE,
46         utils.USAGE_OPTIONS,
47         ("Automatic mode (chooses the most common options):", "-a or --auto", "option", False),
48         ("Dependency package installation retries:", "-r <retries> or --retries=<retries> (default is 3)", "option", False),
49         ("Assume network connection present:", "-n or --network", "option", False),
50         utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
51         utils.USAGE_HELP,
52         utils.USAGE_SPACE,
53         utils.USAGE_SPACE,
54         ("[OPTIONS] (FOR TESTING ONLY/ADVANCED)", "", "header", False),
55         ("Force install of all dependencies:", "-x", "option", False),
56         ("Force unknown distro mode:", "-d", "option", False),
57         ("Force installation of Qt4 support:", "--qt4 (same as --enable=qt4)", "option", False),
58         ("Force disable Qt4 support:", "--no-qt4 (same as --disable=qt4", "option", False),
59         #("Force installation of Qt3 support:", "--qt3 (same as --enable=qt3)", "option", False),
60         #("Force disable Qt3 support:", "--no-qt3 (same as --disable=qt3", "option", False),
61         ("Force installation of PolicyKit support:", "--policykit (same as --enable=policykit)", "option", False),
62         ("Force disable PolicyKit support:", "--no-policykit (same as --disable=policykit)", "option", False),
63         ("Force configure enable/disable flag:", "--enable=<flag> or --disable=<flag>, where <flag> is 'fax-build', 'qt4', 'pp-build', etc. See ./configure --help for more info.", "option", False),
64        ]
65
66def usage(typ='text'):
67    if typ == 'text':
68        utils.log_title(__title__, __version__)
69
70    utils.format_text(USAGE, typ, __title__, __mod__, __version__)
71    sys.exit(0)
72
73
74log.set_module(__mod__)
75
76log.debug("euid = %d" % os.geteuid())
77mode = INTERACTIVE_MODE
78auto = False
79test_depends = False
80test_unknown = False
81language = None
82assume_network = False
83max_retries = 3
84restricted_override = False
85enable = []
86disable = []
87
88if((re.search(' ',os.getcwd()))!= None):
89        log.info("Current hplip source directory path has space character in it. Please update path by removing space characters. Example: Change %s.run to %s.run" % (os.getcwd(),(os.getcwd()).replace(' ','')))
90        cmd = "rm -r ../%s" % (os.getcwd()).rsplit('/').pop()
91        os_utils.execute(cmd)
92        sys.exit(0)
93
94try:
95    opts, args = getopt.getopt(sys.argv[1:], 'hl:giatxdq:nr:b',
96        ['help', 'help-rest', 'help-man', 'help-desc', 'gui', 'lang=',
97        'logging=', 'interactive', 'auto', 'text', 'qt4',
98        'network', 'retries=', 'enable=', 'disable=',
99        'no-qt4', 'policykit', 'no-policykit', 'debug'])
100
101except getopt.GetoptError as e:
102    log.error(e.msg)
103    usage()
104    sys.exit(1)
105
106if os.getenv("HPLIP_DEBUG"):
107    log.set_level('debug')
108
109for o, a in opts:
110    if o in ('-h', '--help'):
111        usage()
112
113    elif o == '--help-rest':
114        usage('rest')
115
116    elif o == '--help-man':
117        usage('man')
118
119    elif o in ('-q', '--lang'):
120        language = a.lower()
121
122    elif o == '--help-desc':
123        print(__doc__, end=' ')
124        sys.exit(0)
125
126    elif o in ('-l', '--logging'):
127        log_level = a.lower().strip()
128        if not log.set_level(log_level):
129            usage()
130
131    elif o in ('-g', '--debug'):
132        log.set_level('debug')
133
134    elif o in ('-i', '--interactive', '--text', '-t'):
135        mode = INTERACTIVE_MODE
136
137    elif o in ('-a', '--auto'):
138        auto = True
139
140    elif o == '-x':
141        log.warn("Install all depends (-x) is for TESTING ONLY")
142        test_depends = True
143
144    elif o == '-d':
145        log.warn("Unknown distro (-d) is for TESTING ONLY")
146        test_unknown = True
147
148    elif o in ('-n', '--network'):
149        assume_network = True
150
151    elif o in ('-r', '--retries'):
152        try:
153            max_retries = int(a)
154        except ValueError:
155            log.error("Invalid value for retries. Set to default of 3.")
156            max_retries = 3
157
158    elif o == '-b':
159        restricted_override = True
160
161    elif o == '--qt4':
162        if 'qt4' not in enable and 'qt4' not in disable:
163            enable.append('qt4')
164        else:
165            log.error("Duplicate configuration flag: %s" % a)
166            sys.exit(1)
167
168    elif o == '--no-qt4':
169        if 'qt4' not in disable and 'qt4' not in enable:
170            disable.append('qt4')
171        else:
172            log.error("Duplicate configuration flag: %s" % a)
173            sys.exit(1)
174
175    elif o == '--policykit':
176        if 'policykit' not in enable and 'policykit' not in disable:
177            enable.append('policykit')
178        else:
179            log.error("Duplicate configuration flag: %s" % a)
180            sys.exit(1)
181
182    elif o == '--no-policykit':
183        if 'policykit' not in disable and 'policykit' not in enable:
184            disable.append('policykit')
185        else:
186            log.error("Duplicate configuration flag: %s" % a)
187            sys.exit(1)
188
189    elif o == '--enable':
190        if a not in enable and a not in disable:
191            enable.append(a)
192        else:
193            log.error("Duplicate configuration flag: %s" % a)
194            sys.exit(1)
195
196    elif o == '--disable':
197        if a not in enable and a not in disable:
198            disable.append(a)
199        else:
200            log.error("Duplicate configuration flag: %s" % a)
201            sys.exit(1)
202
203
204
205if os.getuid() == 0:
206    log.warn("hplip-install should not be run as root.")
207
208log_file = os.path.normpath('./hplip-install_%s.log' % time.strftime("%a-%d-%b-%Y_%H:%M:%S"))
209
210if os.path.exists(log_file):
211    os.remove(log_file)
212
213log.set_logfile(log_file)
214log.set_where(log.LOG_TO_CONSOLE_AND_FILE)
215
216log.debug("Log file=%s" % log_file)
217
218ac_init_pat = re.compile(r"""AC_INIT\(\[(.*?)\], *\[(.*?)\], *\[(.*?)\], *\[(.*?)\] *\)""", re.IGNORECASE)
219try:
220    config_in = open('./configure.in', 'r')
221except IOError:
222    prop.version = 'x.x.x'
223else:
224    for c in config_in:
225        if c.startswith("AC_INIT"):
226            match_obj = ac_init_pat.search(c)
227            prop.version = match_obj.group(2)
228            break
229
230    config_in.close()
231
232utils.log_title(__title__, __version__, True)
233
234log.info("Installer log saved in: %s" % log.bold(log_file))
235log.info("")
236
237
238try:
239    from installer import text_install
240    log.debug("Starting text installer...")
241    text_install.start(language, auto, test_depends, test_unknown, assume_network, max_retries, enable, disable)
242except KeyboardInterrupt:
243    log.error("User exit")
244
245