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