install.py: split out optparse compat code
authorZac Medico <zmedico@gentoo.org>
Tue, 30 Jul 2013 22:21:48 +0000 (15:21 -0700)
committerZac Medico <zmedico@gentoo.org>
Tue, 30 Jul 2013 22:21:48 +0000 (15:21 -0700)
bin/install.py
pym/portage/util/_argparse.py [new file with mode: 0644]

index 0bf7a1f89c4f7e2abbf7d54ba72abfc1258df7f5..b194941b4ef0573a0d0d2569541b9a59ded6463f 100755 (executable)
@@ -8,24 +8,10 @@ import sys
 import subprocess
 import traceback
 
+from portage.util._argparse import ArgumentParser
 from portage.util.movefile import _copyxattr
 from portage.exception import OperationNotSupported
 
-try:
-       from argparse import ArgumentParser
-except ImportError:
-       # Compatibility with Python 2.6 and 3.1
-       from optparse import OptionParser
-
-       class ArgumentParser(object):
-               def __init__(self, **kwargs):
-                       add_help = kwargs.pop("add_help", None)
-                       if add_help is not None:
-                               kwargs["add_help_option"] = add_help
-                       parser = OptionParser(**kwargs)
-                       self.add_argument = parser.add_option
-                       self.parse_known_args = parser.parse_args
-
 # Change back to original cwd _after_ all imports (bug #469338).
 os.chdir(os.environ["__PORTAGE_HELPER_CWD"])
 
diff --git a/pym/portage/util/_argparse.py b/pym/portage/util/_argparse.py
new file mode 100644 (file)
index 0000000..5e6a9ba
--- /dev/null
@@ -0,0 +1,19 @@
+# Copyright 2013 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+__all__ = ['ArgumentParser']
+
+try:
+       from argparse import ArgumentParser
+except ImportError:
+       # Compatibility with Python 2.6 and 3.1
+       from optparse import OptionParser
+
+       class ArgumentParser(object):
+               def __init__(self, **kwargs):
+                       add_help = kwargs.pop("add_help", None)
+                       if add_help is not None:
+                               kwargs["add_help_option"] = add_help
+                       parser = OptionParser(**kwargs)
+                       self.add_argument = parser.add_option
+                       self.parse_known_args = parser.parse_args