MythTV master
sendProfile.py
Go to the documentation of this file.
1#!/usr/bin/python3
2# -*- coding: utf-8 -*-
3
4# smolt - Fedora hardware profiler
5#
6# Copyright (C) 2007 Mike McGrath
7# Copyright (C) 2009 Sebastian Pipping <sebastian@pipping.org>
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22
23from builtins import str
24from builtins import input
25import sys
26import time
27import os
28import getpass
29
30
32 _code_location = '/usr/share/smolt/client'
33 if sys.path[-1] == _code_location:
34 return
35 sys.path.append(_code_location)
36
37
40 from i18n import _
41 import smolt
42
43 from optparse import OptionParser
44 parser = OptionParser(version = smolt.clientVersion)
45
46 parser.add_option('-d', '--debug',
47 dest = 'DEBUG',
48 default = False,
49 action = 'store_true',
50 help = _('enable debug information'))
51 parser.add_option('--config',
52 dest = 'the_only_config_file',
53 default = None,
54 metavar = 'file.cfg',
55 help = _('specify the location of the (only) config file to use'))
56 parser.add_option('-s', '--server',
57 dest = 'smoonURL',
58 default = smolt.smoonURL,
59 metavar = 'smoonURL',
60 help = _('specify the URL of the server (default "%default")'))
61 parser.add_option('--username',
62 dest = 'userName',
63 default = None,
64 metavar = 'userName',
65 help = _('(optional) Fedora Account System registration'))
66 parser.add_option('--password',
67 dest = 'password',
68 default = None,
69 metavar = 'password',
70 help = _('password, will prompt if not specified'))
71 parser.add_option('-p', '--printOnly',
72 dest = 'printOnly',
73 default = False,
74 action = 'store_true',
75 help = _('print information only, do not send'))
76 parser.add_option('-a', '--autoSend',
77 dest = 'autoSend',
78 default = False,
79 action = 'store_true',
80 help = _('don\'t prompt to send, just send'))
81 parser.add_option('-r', '--retry',
82 dest = 'retry',
83 default = False,
84 action = 'store_true',
85 help = _('continue to send until success'))
86 parser.add_option('-u', '--useragent', '--user_agent',
87 dest = 'user_agent',
88 default = smolt.user_agent,
89 metavar = 'USERAGENT',
90 help = _('specify HTTP user agent (default "%default")'))
91 parser.add_option('-t', '--timeout',
92 dest = 'timeout',
93 type = 'float',
94 default = smolt.timeout,
95 help = _('specify HTTP timeout in seconds (default %default seconds)'))
96 parser.add_option('-c', '--checkin',
97 dest = 'cron_mode',
98 default = False,
99 action = 'store_true',
100 help = _('do an automated checkin as when run from cron (implies --autoSend)'))
101 parser.add_option('-S', '--scanOnly',
102 dest = 'send_profile',
103 default = True,
104 action = 'store_false',
105 help = _('only scan this machine for known hardware errata, do not send profile.'))
106 parser.add_option('--submitOnly',
107 dest = 'scan_remote',
108 default = True,
109 action = 'store_false',
110 help = _('do not scan this machine for know hardware errata, only submit profile.'))
111 parser.add_option('--uuidFile',
112 dest = 'uuidFile',
113 default = smolt.hw_uuid_file,
114 help = _('specify which uuid to use, useful for debugging and testing mostly.'))
115 #parser.add_option('-b', '--bodhi',
116 # dest = 'bodhi',
117 # default = False,
118 # action = 'store_true',
119 # help = _('Submit this profile to Bodhi as well, for Fedora Developmnent'))
120 parser.add_option('-n', '--newPublicUUID',
121 dest = 'new_pub',
122 default = False,
123 action = 'store_true',
124 help = _('Request a new public UUID'))
125 parser.add_option('--http-proxy',
126 dest = 'httpproxy',
127 default = None,
128 help = _('HTTP proxy'))
129
130 (opts, args) = parser.parse_args()
131
132 if opts.cron_mode:
133 # Smolt is set to run
134 opts.autoSend = True
135
136 return opts, args
137
138
141 from i18n import _
142 from smolt import to_ascii
143
144 def inner_indent(text):
145 return ('\n' + 5 * ' ').join(text.split('\n'))
146
147 excerpts = {
148 'label_intro':_('Smolt has collected four types of information:'),
149 'label_question':_('Do you want to ..'),
150 'label_question_view':_('(v)iew details on collected information?'),
151 'label_question_send':_('(s)end this information to the Smolt server?'),
152 'label_question_quit':_('(q)uit Smolt?'),
153 'label_general':_('General'),
154 'label_devices':_('Devices'),
155 'label_fs_related':_('File system-related'),
156 'label_distro_specific':_('Distribution-specific'),
157
158 'general':inner_indent(to_ascii(profile.get_general_info_excerpt())),
159 'devices':inner_indent(to_ascii(profile.get_devices_info_excerpt())),
160 'file_system':inner_indent(to_ascii(profile.get_file_system_info_excerpt())),
161 'distro':inner_indent(to_ascii(profile.get_distro_info_excerpt())),
162 }
163 return excerpts
164
165
166def dump_excerpts(excerpts):
167 print("""\
168=====================================================
169%(label_intro)s
170
171%(label_general)s
172 %(general)s
173
174%(label_devices)s
175 %(devices)s
176
177%(label_fs_related)s
178 %(file_system)s
179
180%(label_distro_specific)s
181 %(distro)s
182
183=====================================================
184%(label_question)s
185%(label_question_view)s
186%(label_question_send)s
187%(label_question_quit)s
188""" % excerpts)
189
190
191def present_and_require_confirmation(profile):
192 import subprocess
193 from tempfile import NamedTemporaryFile
194
196 from i18n import _
197 from smolt import error
198
199 excerpts = make_display_excerpts(profile)
200
201 submit = False
202 while not submit:
203 dump_excerpts(excerpts)
204
205 try:
206 choice = input(_('Your choice (s)end (v)iew (q)uit: ')).strip()
207 except KeyboardInterrupt:
208 error(_('Exiting...'))
209 sys.exit(4)
210 if choice in (_('s|y|yes')).split('|'):
211 submit = True
212 print('\n\n')
213 elif choice in (_('q|n|no')).split('|'):
214 sys.exit(0)
215 elif choice in (_('v')).split('|'):
216 f = NamedTemporaryFile(suffix=b'')
217 for line in profile.getProfile():
218 try:
219 f.write(bytes(line + '\n', 'latin1'))
220 except UnicodeEncodeError:
221 pass
222 f.flush()
223 os.chmod(f.name, 0o400)
224 try:
225 pager_command = os.environ['PAGER']
226 except KeyError:
227 if os.path.exists('/usr/bin/less'):
228 pager_command = '/usr/bin/less'
229 elif os.path.exists('/bin/less'):
230 pager_command = '/bin/less'
231 else:
232 #fallback to more , could use /bin/more but might as well let the path sort it out.
233 pager_command = 'more'
234 try:
235 subprocess.call([pager_command, f.name])
236 except NameError:
237 os.system(' '.join([pager_command, f.name]))
238 f.close()
239 print('\n\n')
240 else:
241 error(_('Exiting...'))
242 sys.exit(4)
243
244
245def do_send_profile(uuiddb, uuid, profile, opts, proxies):
246 (error_code, pub_uuid, admin) = profile.send(uuiddb, uuid, user_agent=opts.user_agent,
247 smoonURL=opts.smoonURL,
248 timeout=opts.timeout,
249 proxies=proxies,
250 batch=opts.cron_mode)
251 return (error_code, pub_uuid, admin)
252
253
254def send_profile(uuiddb, uuid, profile, opts, proxies):
256 from i18n import _
257 from smolt import error
258
259 if opts.retry:
260 while 1:
261 (error_code, pub_uuid, admin) = do_send_profile(uuiddb, uuid, profile, opts, proxies)
262 if not error_code:
263 break
264 error(_('Retry Enabled - Retrying'))
265 time.sleep(30)
266 else:
267 (error_code, pub_uuid, admin) = do_send_profile(uuiddb, uuid, profile, opts, proxies)
268 if error_code:
269 print(_('Could not send - Exiting'))
270 sys.exit(1)
271
272 return (error_code, pub_uuid, admin)
273
274
275def mention_profile_web_view(opts, pub_uuid, admin):
277 import smolt
278 from i18n import _
279
280 pubUrl = smolt.get_profile_link(opts.smoonURL, pub_uuid)
281 print()
282 print(_('To share your profile: \n\t%s (public)') % pubUrl)
283 if not smolt.secure:
284 print(_('\tAdmin Password: %s') % admin)
285
286
287def get_proxies(opts):
288 if opts.httpproxy is None:
289 proxies = dict()
290 else:
291 proxies = {'http':opts.httpproxy}
292 return proxies
293
294
295def read_profile(gate, uuid):
297 from i18n import _
298 import smolt
299
300 try:
301 profile = smolt.create_profile(gate, uuid)
302 except smolt.UUIDError as e:
303 sys.stderr.write(_('%s\n' % e))
304 sys.exit(9)
305 return profile
306
307
310 from i18n import _
311
312 if not opts.password:
313 password = getpass.getpass('\n' + _('Password:') + ' ')
314 else:
315 password = opts.password
316
317 if profile.register(userName=opts.userName, password=password, user_agent=opts.user_agent, smoonURL=opts.smoonURL, timeout=opts.timeout):
318 print(_('Registration Failed, Try again'))
319
320
321def do_scan_remote(profile, opts, gate):
323 from scan import scan, rating
324
325 scan(profile, opts.smoonURL, gate)
326 try:
327 rating(profile, opts.smoonURL, gate)
328 except ValueError:
329 print("Could not get rating!")
330
331
334 from i18n import _
335 print()
336 print(_('No Public UUID found! Please re-run with -n to generate a new public uuid'))
337
338
339def main_request_new_public_uuid(uuiddb, uuid, profile, opts):
341 from i18n import _
342 from smolt import error, ServerError
343
344 try:
345 pub_uuid = profile.regenerate_pub_uuid(uuiddb, uuid, user_agent=opts.user_agent,
346 smoonURL=opts.smoonURL,
347 timeout=opts.timeout)
348 except ServerError as e:
349 error(_('Error contacting server: %s') % str(e))
350 sys.exit(1)
351
352 print(_('Success! Your new public UUID is: %s' % pub_uuid))
353 sys.exit(0)
354
355
356def main_scan_only(profile, opts, gate):
357 do_scan_remote(profile, opts, gate)
358 sys.exit(0)
359
360
361def main_print_only(profile):
362 for line in profile.getProfile():
363 if not line.startswith('#'):
364 print(line)
365 sys.exit(0)
366
367
368def main_send_profile(uuiddb, uuid, profile, opts, gate):
369 proxies = get_proxies(opts)
370
371 if not opts.autoSend:
373
374 (error_code, pub_uuid, admin) = send_profile(uuiddb, uuid, profile, opts, proxies)
375
376 if opts.userName:
378
379 if opts.scan_remote and not opts.cron_mode:
380 do_scan_remote(profile, opts, gate)
381
382 if pub_uuid:
383 mention_profile_web_view(opts, pub_uuid, admin)
384 elif not opts.cron_mode:
386
387
388def main():
390 from i18n import _
391 import smolt
392 from gate import create_default_gate, create_gate_from_file
393 from uuiddb import create_default_uuiddb
394
395 (opts, args) = command_line()
396
397 if opts.the_only_config_file is None:
398 gate = create_default_gate()
399 else:
400 gate = create_gate_from_file(opts.the_only_config_file)
401
402 smolt.DEBUG = opts.DEBUG
403 smolt.hw_uuid_file = opts.uuidFile
404
405 profile = read_profile(gate, smolt.read_uuid())
406
407 if opts.new_pub:
408 uuiddb = create_default_uuiddb()
409 uuid = smolt.read_uuid()
410 main_request_new_public_uuid(uuiddb, uuid, profile, opts)
411 elif not opts.send_profile:
412 main_scan_only(profile, opts, gate)
413 elif opts.printOnly and not opts.autoSend:
414 main_print_only(profile)
415 else:
416 uuiddb = create_default_uuiddb()
417 uuid = smolt.read_uuid()
418 main_send_profile(uuiddb, uuid, profile, opts, gate)
419
420
421if __name__ == '__main__':
422 main()
def create_gate_from_file(filename)
Definition: gate.py:87
def create_default_gate()
Definition: gate.py:83
def scan(profile, smoonURL, gate)
Definition: scan.py:54
def rating(profile, smoonURL, gate)
Definition: scan.py:36
def send_profile(uuiddb, uuid, profile, opts, proxies)
Definition: sendProfile.py:254
def main_scan_only(profile, opts, gate)
Definition: sendProfile.py:356
def register_with_fedora_account_system(opts, profile)
Definition: sendProfile.py:308
def do_scan_remote(profile, opts, gate)
Definition: sendProfile.py:321
def read_profile(gate, uuid)
Definition: sendProfile.py:295
def make_display_excerpts(profile)
Definition: sendProfile.py:139
def do_send_profile(uuiddb, uuid, profile, opts, proxies)
Definition: sendProfile.py:245
def main_request_new_public_uuid(uuiddb, uuid, profile, opts)
Definition: sendProfile.py:339
def mention_profile_web_view(opts, pub_uuid, admin)
Definition: sendProfile.py:275
def present_and_require_confirmation(profile)
Definition: sendProfile.py:191
def main_send_profile(uuiddb, uuid, profile, opts, gate)
Definition: sendProfile.py:368
def to_ascii(o, current_encoding='utf-8')
Definition: smolt.py:203
def error(message)
Definition: smolt.py:409
def create_default_uuiddb()
Definition: uuiddb.py:67
static void print(const QList< uint > &raw_minimas, const QList< uint > &raw_maximas, const QList< float > &minimas, const QList< float > &maximas)