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>
This commit is contained in:
David Pursehouse 2013-05-17 10:49:33 +09:00
parent da45e5d884
commit 59bbb580e3
10 changed files with 62 additions and 49 deletions

View File

@ -25,14 +25,13 @@ try:
except ImportError: except ImportError:
import dummy_threading as _threading import dummy_threading as _threading
import time import time
try:
import urllib2 from pyversion import is_python3
except ImportError: if is_python3():
# For python3
import urllib.request import urllib.request
import urllib.error import urllib.error
else: else:
# For python2 import urllib2
import imp import imp
urllib = imp.new_module('urllib') urllib = imp.new_module('urllib')
urllib.request = urllib2 urllib.request = urllib2

15
main.py
View File

@ -22,13 +22,12 @@ import optparse
import os import os
import sys import sys
import time import time
try:
import urllib2 from pyversion import is_python3
except ImportError: if is_python3():
# For python3
import urllib.request import urllib.request
else: else:
# For python2 import urllib2
urllib = imp.new_module('urllib') urllib = imp.new_module('urllib')
urllib.request = urllib2 urllib.request = urllib2
@ -50,10 +49,10 @@ from pager import RunPager
from subcmds import all_commands from subcmds import all_commands
try: if not is_python3():
# pylint:disable=W0622
input = raw_input input = raw_input
except NameError: # pylint:enable=W0622
pass
global_options = optparse.OptionParser( global_options = optparse.OptionParser(
usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"

View File

@ -18,16 +18,16 @@ import itertools
import os import os
import re import re
import sys import sys
try: import xml.dom.minidom
# For python3
from pyversion import is_python3
if is_python3():
import urllib.parse import urllib.parse
except ImportError: else:
# For python2
import imp import imp
import urlparse import urlparse
urllib = imp.new_module('urllib') urllib = imp.new_module('urllib')
urllib.parse = urlparse urllib.parse = urlparse
import xml.dom.minidom
from git_config import GitConfig from git_config import GitConfig
from git_refs import R_HEADS, HEAD from git_refs import R_HEADS, HEAD

View File

@ -36,10 +36,11 @@ from trace import IsTrace, Trace
from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M 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 input = raw_input
except NameError: # pylint:enable=W0622
pass
def _lwrite(path, content): def _lwrite(path, content):
lock = '%s.lock' % path lock = '%s.lock' % path

19
pyversion.py Normal file
View File

@ -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

8
repo
View File

@ -117,15 +117,13 @@ import re
import stat import stat
import subprocess import subprocess
import sys import sys
try:
import urllib2 if sys.version_info[0] == 3:
except ImportError:
# For python3
import urllib.request import urllib.request
import urllib.error import urllib.error
else: else:
# For python2
import imp import imp
import urllib2
urllib = imp.new_module('urllib') urllib = imp.new_module('urllib')
urllib.request = urllib2 urllib.request = urllib2
urllib.error = urllib2 urllib.error = urllib2

View File

@ -19,11 +19,11 @@ import platform
import re import re
import shutil import shutil
import sys import sys
try:
# For python3 from pyversion import is_python3
if is_python3():
import urllib.parse import urllib.parse
except ImportError: else:
# For python2
import imp import imp
import urlparse import urlparse
urllib = imp.new_module('urllib') urllib = imp.new_module('urllib')

View File

@ -21,12 +21,13 @@ except ImportError:
import dummy_threading as _threading import dummy_threading as _threading
import glob import glob
try:
# For python2 from pyversion import is_python3
import StringIO as io if is_python3():
except ImportError:
# For python3
import io import io
else:
import StringIO as io
import itertools import itertools
import os import os
import sys import sys

View File

@ -24,22 +24,17 @@ import socket
import subprocess import subprocess
import sys import sys
import time import time
try:
# For python3 from pyversion import is_python3
if is_python3():
import urllib.parse import urllib.parse
except ImportError: import xmlrpc.client
# For python2 else:
import imp import imp
import urlparse import urlparse
import xmlrpclib
urllib = imp.new_module('urllib') urllib = imp.new_module('urllib')
urllib.parse = urlparse urllib.parse = urlparse
try:
# For python3
import xmlrpc.client
except ImportError:
# For python2
import imp
import xmlrpclib
xmlrpc = imp.new_module('xmlrpc') xmlrpc = imp.new_module('xmlrpc')
xmlrpc.client = xmlrpclib xmlrpc.client = xmlrpclib

View File

@ -23,10 +23,11 @@ from editor import Editor
from error import HookError, UploadError from error import HookError, UploadError
from project import RepoHook from project import RepoHook
try: from pyversion import is_python3
if not is_python3():
# pylint:disable=W0622
input = raw_input input = raw_input
except NameError: # pylint:enable=W0622
pass
UNUSUAL_COMMIT_THRESHOLD = 5 UNUSUAL_COMMIT_THRESHOLD = 5