Home | History | Annotate | Download | only in test
      1 #!/usr/bin/env python
      2 
      3 """
      4 Compatibility module to use the lldb test-suite with Python 2.6.
      5 
      6 Warning: This may be buggy. It has not been extensively tested and should only
      7 be used when it is impossible to use a newer Python version.
      8 It is also a special-purpose class for lldb's test-suite.
      9 """
     10 
     11 import sys
     12 
     13 if sys.version_info >= (2, 7):
     14     raise "This module shouldn't be used when argparse is available (Python >= 2.7)"
     15 else:
     16     print "Using Python 2.6 compatibility layer. Some command line options may not be supported"
     17 
     18 
     19 import optparse
     20 
     21 
     22 class ArgumentParser(object):
     23     def __init__(self, description="My program's description", prefix_chars='-', add_help=True):
     24         self.groups = []
     25         self.parser = optparse.OptionParser(description=description, add_help_option=add_help)
     26         self.prefix_chars = prefix_chars
     27 
     28     def add_argument_group(self, name):
     29         group = optparse.OptionGroup(self.parser, name)
     30         # Hack around our test directories argument (what's left after the
     31         # options)
     32         if name != 'Test directories':
     33             self.groups.append(group)
     34         return ArgumentGroup(group)
     35 
     36     def add_argument(self, *opt_strs, **kwargs):
     37         self.parser.add_option(*opt_strs, **kwargs)
     38     # def add_argument(self, opt_str, action='store', dest=None, metavar=None, help=''):
     39     #     if dest is None and metavar is None:
     40     #         self.parser.add_argument(opt_str, action=action, help=help)
     41 
     42     def parse_args(self, arguments=sys.argv[1:]):
     43         map(lambda g: self.parser.add_option_group(g), self.groups)
     44         (options, args) = self.parser.parse_args(arguments)
     45         d = vars(options)
     46         d['args'] = args
     47         return Namespace(d)
     48 
     49     def print_help(self):
     50         self.parser.print_help()
     51 
     52 
     53 class ArgumentGroup(object):
     54     def __init__(self, option_group):
     55         self.option_group = option_group
     56 
     57     def add_argument(self, *opt_strs, **kwargs):
     58         # Hack around our positional argument (the test directories)
     59         if opt_strs == ('args',):
     60             return
     61 
     62         # Hack around the options that start with '+'
     63         if len(opt_strs) == 1 and opt_strs[0] == '+a':
     64             opt_strs = ('--plus_a',)
     65         if len(opt_strs) == 1 and opt_strs[0] == '+b':
     66             opt_strs = ('--plus_b',)
     67         self.option_group.add_option(*opt_strs, **kwargs)
     68 
     69 
     70 class Namespace(object):
     71     def __init__(self, d):
     72         self.__dict__ = d
     73 
     74     def __str__(self):
     75         strings = []
     76         for (k, v) in self.__dict__.iteritems():
     77             strings.append(str(k) + '=' + str(v))
     78         strings.sort()
     79 
     80         return self.__class__.__name__ + '(' + ', '.join(strings) + ')'
     81