Move Python version checking to a separate module

Add a new module with methods for checking the Python version.

Instead of handling Python3 imports with try...except blocks, first
check the python version and then import the relevant modules.  This
makes the code a bit cleaner and will result in less diff when/if we
remove support for Python < 3 later.

Use the same mechanism to handle `input` vs. `raw_input` and add
suppression of pylint warnings caused by redefinition of the built-in
method `input`.

Change-Id: Ia403e525b88d77640a741ac50382146e7d635924
Also-by: Chirayu Desai <cdesai@cyanogenmod.org>
Signed-off-by: Chirayu Desai <cdesai@cyanogenmod.org>
diff --git a/git_config.py b/git_config.py
index 9524df9..431cd45 100644
--- a/git_config.py
+++ b/git_config.py
@@ -25,14 +25,13 @@
 except ImportError:
   import dummy_threading as _threading
 import time
-try:
-  import urllib2
-except ImportError:
-  # For python3
+
+from pyversion import is_python3
+if is_python3():
   import urllib.request
   import urllib.error
 else:
-  # For python2
+  import urllib2
   import imp
   urllib = imp.new_module('urllib')
   urllib.request = urllib2
diff --git a/main.py b/main.py
index 49d2482..e4cdeb0 100755
--- a/main.py
+++ b/main.py
@@ -22,13 +22,12 @@
 import os
 import sys
 import time
-try:
-  import urllib2
-except ImportError:
-  # For python3
+
+from pyversion import is_python3
+if is_python3():
   import urllib.request
 else:
-  # For python2
+  import urllib2
   urllib = imp.new_module('urllib')
   urllib.request = urllib2
 
@@ -50,10 +49,10 @@
 
 from subcmds import all_commands
 
-try:
+if not is_python3():
+  # pylint:disable=W0622
   input = raw_input
-except NameError:
-  pass
+  # pylint:enable=W0622
 
 global_options = optparse.OptionParser(
                  usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
diff --git a/manifest_xml.py b/manifest_xml.py
index cc441dc..16476aa 100644
--- a/manifest_xml.py
+++ b/manifest_xml.py
@@ -18,16 +18,16 @@
 import os
 import re
 import sys
-try:
-  # For python3
+import xml.dom.minidom
+
+from pyversion import is_python3
+if is_python3():
   import urllib.parse
-except ImportError:
-  # For python2
+else:
   import imp
   import urlparse
   urllib = imp.new_module('urllib')
   urllib.parse = urlparse
-import xml.dom.minidom
 
 from git_config import GitConfig
 from git_refs import R_HEADS, HEAD
diff --git a/project.py b/project.py
index effe75c..f299297 100644
--- a/project.py
+++ b/project.py
@@ -36,10 +36,11 @@
 
 from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M
 
-try:
+from pyversion import is_python3
+if not is_python3():
+  # pylint:disable=W0622
   input = raw_input
-except NameError:
-  pass
+  # pylint:enable=W0622
 
 def _lwrite(path, content):
   lock = '%s.lock' % path
diff --git a/pyversion.py b/pyversion.py
new file mode 100644
index 0000000..5b348d9
--- /dev/null
+++ b/pyversion.py
@@ -0,0 +1,19 @@
+#
+# Copyright (C) 2013 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import sys
+
+def is_python3():
+  return sys.version_info[0] == 3
diff --git a/repo b/repo
index 791e40c..58e82fe 100755
--- a/repo
+++ b/repo
@@ -117,15 +117,13 @@
 import stat
 import subprocess
 import sys
-try:
-  import urllib2
-except ImportError:
-  # For python3
+
+if sys.version_info[0] == 3:
   import urllib.request
   import urllib.error
 else:
-  # For python2
   import imp
+  import urllib2
   urllib = imp.new_module('urllib')
   urllib.request = urllib2
   urllib.error = urllib2
diff --git a/subcmds/init.py b/subcmds/init.py
index 1f91526..a44fb7a 100644
--- a/subcmds/init.py
+++ b/subcmds/init.py
@@ -19,11 +19,11 @@
 import re
 import shutil
 import sys
-try:
-  # For python3
+
+from pyversion import is_python3
+if is_python3():
   import urllib.parse
-except ImportError:
-  # For python2
+else:
   import imp
   import urlparse
   urllib = imp.new_module('urllib')
diff --git a/subcmds/status.py b/subcmds/status.py
index 9810337..41c4429 100644
--- a/subcmds/status.py
+++ b/subcmds/status.py
@@ -21,12 +21,13 @@
   import dummy_threading as _threading
 
 import glob
-try:
-  # For python2
-  import StringIO as io
-except ImportError:
-  # For python3
+
+from pyversion import is_python3
+if is_python3():
   import io
+else:
+  import StringIO as io
+
 import itertools
 import os
 import sys
diff --git a/subcmds/sync.py b/subcmds/sync.py
index 8fb9488..b34787d 100644
--- a/subcmds/sync.py
+++ b/subcmds/sync.py
@@ -24,22 +24,17 @@
 import subprocess
 import sys
 import time
-try:
-  # For python3
+
+from pyversion import is_python3
+if is_python3():
   import urllib.parse
-except ImportError:
-  # For python2
+  import xmlrpc.client
+else:
   import imp
   import urlparse
+  import xmlrpclib
   urllib = imp.new_module('urllib')
   urllib.parse = urlparse
-try:
-  # For python3
-  import xmlrpc.client
-except ImportError:
-  # For python2
-  import imp
-  import xmlrpclib
   xmlrpc = imp.new_module('xmlrpc')
   xmlrpc.client = xmlrpclib
 
diff --git a/subcmds/upload.py b/subcmds/upload.py
index a34938e..13b8778 100644
--- a/subcmds/upload.py
+++ b/subcmds/upload.py
@@ -23,10 +23,11 @@
 from error import HookError, UploadError
 from project import RepoHook
 
-try:
+from pyversion import is_python3
+if not is_python3():
+  # pylint:disable=W0622
   input = raw_input
-except NameError:
-  pass
+  # pylint:enable=W0622
 
 UNUSUAL_COMMIT_THRESHOLD = 5