~sbeattie/apport/apport-improvements

« back to all changes in this revision

Viewing changes to apport/ui.py

  • Committer: Martin Pitt
  • Date: 2009-08-05 15:47:33 UTC
  • mto: This revision was merged to the branch mainline in revision 1514.
  • Revision ID: martin.pitt@canonical.com-20090805154733-wtv40w61i2xkza6l
show list of symptoms when invoking with -f and nothing else

Show diffs side-by-side

added added

removed removed

Lines of Context:
292
292
        '''Report a bug.
293
293
 
294
294
        If a pid is given on the command line, the report will contain runtime
295
 
        debug information. Either a package or a pid must be specified.
 
295
        debug information. Either a package or a pid must be specified; if none
 
296
        is given, show a list of symptoms.
296
297
 
297
298
        If a symptom script is given, this will be run first (used by
298
299
        run_symptom()).
299
300
        '''
300
 
        if not self.options.package and not self.options.pid and not symptom_script:
301
 
            self.ui_error_message(_('No package specified'), 
302
 
                _('You need to specify a package or a PID. See --help for more information.'))
 
301
        if not self.options.package and not self.options.pid and \
 
302
                not symptom_script:
 
303
            if self.run_symptoms():
 
304
                return True
 
305
            else:
 
306
                self.ui_error_message(_('No package specified'), 
 
307
                    _('You need to specify a package or a PID. See --help for more information.'))
303
308
            return False
 
309
 
304
310
        self.report = apport.Report('Bug')
305
311
 
306
312
        # if PID is given, add info
365
371
 
366
372
        return True
367
373
 
 
374
    def run_symptoms(self):
 
375
        '''Report a bug from a list of available symptoms.
 
376
        
 
377
        Return False if no symptoms are available.
 
378
        '''
 
379
        scripts = glob.glob(os.path.join(symptom_script_dir, '*.py'))
 
380
 
 
381
        symptom_names = []
 
382
        symptom_descriptions = []
 
383
        for script in scripts:
 
384
            symb = {}
 
385
            try:
 
386
                execfile(script, symb)
 
387
            except:
 
388
                print >> sys.stderr, 'symptom script %s is invalid' % script
 
389
                traceback.print_exc()
 
390
                continue
 
391
            symptom_names.append(os.path.splitext(os.path.basename(script))[0])
 
392
            symptom_descriptions.append(symb.get('description', symptom_names[-1]))
 
393
 
 
394
        if not symptom_names:
 
395
            return False
 
396
 
 
397
        symptom_names.append(None)
 
398
        symptom_descriptions.append('Other problem')
 
399
 
 
400
        ch = self.ui_question_choice(_('What kind of problem do you want to report?'), 
 
401
                symptom_descriptions, False)
 
402
 
 
403
        if ch != None:
 
404
            symptom = symptom_names[ch[0]]
 
405
            if symptom:
 
406
                self.run_report_bug(os.path.join(symptom_script_dir, symptom + '.py'))
 
407
            else:
 
408
                return False
 
409
 
 
410
        return True
 
411
 
368
412
    def run_symptom(self):
369
413
        '''Report a bug with a symptom script.'''
370
414
 
407
451
        '''
408
452
        optparser = optparse.OptionParser('%prog [options]')
409
453
        optparser.add_option('-f', '--file-bug',
410
 
            help='Start in bug filing mode. Requires --package and an optional --pid, or just a --pid',
 
454
            help='Start in bug filing mode. Requires --package and an optional --pid, or just a --pid. If neither is given, display a list of known symptoms.',
411
455
            action='store_true', dest='filebug', default=False)
412
456
        optparser.add_option('-s', '--symptom', metavar='SYMPTOM',
413
457
            help='File a bug report about a symptom.', dest='symptom')
1069
1113
            self.msg_title = None
1070
1114
            self.msg_text = None
1071
1115
            self.msg_severity = None # 'warning' or 'error'
 
1116
            self.msg_choices = None
1072
1117
 
1073
1118
        def ui_present_crash(self, desktopentry):
1074
1119
            return self.present_crash_response
1123
1168
 
1124
1169
        def ui_question_choice(self, text, options, multiple):
1125
1170
            self.msg_text = text
 
1171
            self.msg_choices = options
1126
1172
            return self.question_choice_response
1127
1173
 
1128
1174
        def ui_question_file(self, text):
2052
2098
            self.assertEqual(self.ui.report['ProblemType'], 'Bug')
2053
2099
            self.assertEqual(self.ui.report['q'], 'True')
2054
2100
 
 
2101
        def test_run_report_bug_list_symptoms(self):
 
2102
            '''run_report_bug() without specifying arguments and available symptoms.'''
 
2103
 
 
2104
            f = open(os.path.join(symptom_script_dir, 'foo.py'), 'w')
 
2105
            print >> f, '''description = 'foo does not work'
 
2106
def run(report, ui):
 
2107
    return 'bash'
 
2108
'''
 
2109
            f.close()
 
2110
            f = open(os.path.join(symptom_script_dir, 'bar.py'), 'w')
 
2111
            print >> f, 'def run(report, ui):\n  return "coreutils"'
 
2112
            f.close()
 
2113
 
 
2114
            sys.argv = ['ui-test', '-f']
 
2115
            self.ui = _TestSuiteUserInterface()
 
2116
 
 
2117
            self.ui.question_choice_response = None
 
2118
            self.assertEqual(self.ui.run_argv(), True)
 
2119
            self.assertEqual(self.ui.msg_severity, None)
 
2120
            self.assert_('kind of problem' in self.ui.msg_text)
 
2121
            self.assertEqual(self.ui.msg_choices, 
 
2122
                    ['bar', 'foo does not work', 'Other problem'])
 
2123
 
 
2124
            # cancelled
 
2125
            self.assertEqual(self.ui.ic_progress_pulses, 0)
 
2126
            self.assertEqual(self.ui.report, None)
 
2127
 
 
2128
            # now, choose foo -> bash report
 
2129
            self.ui.question_choice_response = [1]
 
2130
            self.assertEqual(self.ui.run_argv(), True)
 
2131
            self.assertEqual(self.ui.msg_severity, None)
 
2132
            self.assert_(self.ui.ic_progress_pulses > 0)
 
2133
            self.assert_(self.ui.report['Package'].startswith('bash'))
 
2134
 
2055
2135
    unittest.main()