[Lldb-commits] [lldb] r175341 - Include a small argparse compatibility layer for Python < 2.7

Filipe Cabecinhas me at filcab.net
Sat Feb 16 01:05:24 PST 2013


Author: filcab
Date: Sat Feb 16 03:05:23 2013
New Revision: 175341

URL: http://llvm.org/viewvc/llvm-project?rev=175341&view=rev
Log:
Include a small argparse compatibility layer for Python < 2.7

Added:
    lldb/trunk/test/argparse_compat.py   (with props)
Modified:
    lldb/trunk/test/dotest.py

Added: lldb/trunk/test/argparse_compat.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/argparse_compat.py?rev=175341&view=auto
==============================================================================
--- lldb/trunk/test/argparse_compat.py (added)
+++ lldb/trunk/test/argparse_compat.py Sat Feb 16 03:05:23 2013
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+
+"""
+Compatibility module to use the lldb test-suite with Python 2.6.
+
+Warning: This may be buggy. It has not been extensively tested and should only
+be used when it is impossible to use a newer Python version.
+It is also a special-purpose class for lldb's test-suite.
+"""
+
+import sys
+
+if sys.version_info >= (2, 7):
+    raise "This module shouldn't be used when argparse is available (Python >= 2.7)"
+else:
+    print "Using Python 2.6 compatibility layer. Some command line options may not be supported"
+
+
+import optparse
+
+
+class ArgumentParser(object):
+    def __init__(self, description="My program's description", prefix_chars='-', add_help=True):
+        self.groups = []
+        self.parser = optparse.OptionParser(description=description, add_help_option=add_help)
+        self.prefix_chars = prefix_chars
+
+    def add_argument_group(self, name):
+        group = optparse.OptionGroup(self.parser, name)
+        # Hack around our test directories argument (what's left after the
+        # options)
+        if name != 'Test directories':
+            self.groups.append(group)
+        return ArgumentGroup(group)
+
+    def add_argument(self, *opt_strs, **kwargs):
+        self.parser.add_option(*opt_strs, **kwargs)
+    # def add_argument(self, opt_str, action='store', dest=None, metavar=None, help=''):
+    #     if dest is None and metavar is None:
+    #         self.parser.add_argument(opt_str, action=action, help=help)
+
+    def parse_args(self, arguments=sys.argv[1:]):
+        map(lambda g: self.parser.add_option_group(g), self.groups)
+        (options, args) = self.parser.parse_args(arguments)
+        d = vars(options)
+        d['args'] = args
+        return Namespace(d)
+
+    def print_help(self):
+        self.parser.print_help()
+
+
+class ArgumentGroup(object):
+    def __init__(self, option_group):
+        self.option_group = option_group
+
+    def add_argument(self, *opt_strs, **kwargs):
+        # Hack around our positional argument (the test directories)
+        if opt_strs == ('args',):
+            return
+
+        # Hack around the options that start with '+'
+        if len(opt_strs) == 1 and opt_strs[0] == '+a':
+            opt_strs = ('--plus_a',)
+        if len(opt_strs) == 1 and opt_strs[0] == '+b':
+            opt_strs = ('--plus_b',)
+        self.option_group.add_option(*opt_strs, **kwargs)
+
+
+class Namespace(object):
+    def __init__(self, d):
+        self.__dict__ = d
+
+    def __str__(self):
+        strings = []
+        for (k, v) in self.__dict__.iteritems():
+            strings.append(str(k) + '=' + str(v))
+        strings.sort()
+
+        return self.__class__.__name__ + '(' + ', '.join(strings) + ')'

Propchange: lldb/trunk/test/argparse_compat.py
------------------------------------------------------------------------------
    svn:executable = *

Modified: lldb/trunk/test/dotest.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/dotest.py?rev=175341&r1=175340&r2=175341&view=diff
==============================================================================
--- lldb/trunk/test/dotest.py (original)
+++ lldb/trunk/test/dotest.py Sat Feb 16 03:05:23 2013
@@ -20,7 +20,6 @@ Type:
 for available options.
 """
 
-import argparse
 import os
 import platform
 import signal
@@ -31,6 +30,11 @@ import time
 import unittest2
 import progress
 
+if sys.version_info >= (2, 7):
+    argparse = __import__('argparse')
+else:
+    argparse = __import__('argparse_compat')
+
 def is_exe(fpath):
     """Returns true if fpath is an executable."""
     return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
@@ -133,7 +137,7 @@ post_flight = None
 
 # The 'archs' and 'compilers' can be specified via either command line or configFile,
 # with the command line overriding the configFile.  The corresponding options can be
-# specified more than once. For example, "-A x86_64 -A i386" => archs=['x86_64', 'i386'] 
+# specified more than once. For example, "-A x86_64 -A i386" => archs=['x86_64', 'i386']
 # and "-C gcc -C clang" => compilers=['gcc', 'clang'].
 archs = None        # Must be initialized after option parsing
 compilers = None    # Must be initialized after option parsing





More information about the lldb-commits mailing list