summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@sonymobile.com>2013-05-17 10:49:33 +0900
committerDavid Pursehouse <david.pursehouse@sonymobile.com>2013-05-23 07:28:53 +0000
commit59bbb580e34bbc5dce76dacaad9ff94f21fa396f (patch)
tree4d19216215edc174851baa10a00a3bccba9d4e45
parentda45e5d8848246cddbce80ff56786bd7330ba5af (diff)
downloadgit-repo-59bbb580e34bbc5dce76dacaad9ff94f21fa396f.tar.gz
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>
-rw-r--r--git_config.py9
-rwxr-xr-xmain.py15
-rw-r--r--manifest_xml.py10
-rw-r--r--project.py7
-rw-r--r--pyversion.py19
-rwxr-xr-xrepo8
-rw-r--r--subcmds/init.py8
-rw-r--r--subcmds/status.py11
-rw-r--r--subcmds/sync.py17
-rw-r--r--subcmds/upload.py7
10 files changed, 62 insertions, 49 deletions
diff --git a/git_config.py b/git_config.py
index 9524df9b..431cd457 100644
--- a/git_config.py
+++ b/git_config.py
@@ -25,14 +25,13 @@ try:
25except ImportError: 25except ImportError:
26 import dummy_threading as _threading 26 import dummy_threading as _threading
27import time 27import time
28try: 28
29 import urllib2 29from pyversion import is_python3
30except ImportError: 30if is_python3():
31 # For python3
32 import urllib.request 31 import urllib.request
33 import urllib.error 32 import urllib.error
34else: 33else:
35 # For python2 34 import urllib2
36 import imp 35 import imp
37 urllib = imp.new_module('urllib') 36 urllib = imp.new_module('urllib')
38 urllib.request = urllib2 37 urllib.request = urllib2
diff --git a/main.py b/main.py
index 49d24823..e4cdeb0f 100755
--- a/main.py
+++ b/main.py
@@ -22,13 +22,12 @@ import optparse
22import os 22import os
23import sys 23import sys
24import time 24import time
25try: 25
26 import urllib2 26from pyversion import is_python3
27except ImportError: 27if is_python3():
28 # For python3
29 import urllib.request 28 import urllib.request
30else: 29else:
31 # For python2 30 import urllib2
32 urllib = imp.new_module('urllib') 31 urllib = imp.new_module('urllib')
33 urllib.request = urllib2 32 urllib.request = urllib2
34 33
@@ -50,10 +49,10 @@ from pager import RunPager
50 49
51from subcmds import all_commands 50from subcmds import all_commands
52 51
53try: 52if not is_python3():
53 # pylint:disable=W0622
54 input = raw_input 54 input = raw_input
55except NameError: 55 # pylint:enable=W0622
56 pass
57 56
58global_options = optparse.OptionParser( 57global_options = optparse.OptionParser(
59 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" 58 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
diff --git a/manifest_xml.py b/manifest_xml.py
index cc441dc8..16476aa5 100644
--- a/manifest_xml.py
+++ b/manifest_xml.py
@@ -18,16 +18,16 @@ import itertools
18import os 18import os
19import re 19import re
20import sys 20import sys
21try: 21import xml.dom.minidom
22 # For python3 22
23from pyversion import is_python3
24if is_python3():
23 import urllib.parse 25 import urllib.parse
24except ImportError: 26else:
25 # For python2
26 import imp 27 import imp
27 import urlparse 28 import urlparse
28 urllib = imp.new_module('urllib') 29 urllib = imp.new_module('urllib')
29 urllib.parse = urlparse 30 urllib.parse = urlparse
30import xml.dom.minidom
31 31
32from git_config import GitConfig 32from git_config import GitConfig
33from git_refs import R_HEADS, HEAD 33from git_refs import R_HEADS, HEAD
diff --git a/project.py b/project.py
index effe75c4..f299297d 100644
--- a/project.py
+++ b/project.py
@@ -36,10 +36,11 @@ from trace import IsTrace, Trace
36 36
37from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M 37from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M
38 38
39try: 39from pyversion import is_python3
40if not is_python3():
41 # pylint:disable=W0622
40 input = raw_input 42 input = raw_input
41except NameError: 43 # pylint:enable=W0622
42 pass
43 44
44def _lwrite(path, content): 45def _lwrite(path, content):
45 lock = '%s.lock' % path 46 lock = '%s.lock' % path
diff --git a/pyversion.py b/pyversion.py
new file mode 100644
index 00000000..5b348d91
--- /dev/null
+++ b/pyversion.py
@@ -0,0 +1,19 @@
1#
2# Copyright (C) 2013 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import sys
17
18def is_python3():
19 return sys.version_info[0] == 3
diff --git a/repo b/repo
index 791e40cb..58e82fe7 100755
--- a/repo
+++ b/repo
@@ -117,15 +117,13 @@ import re
117import stat 117import stat
118import subprocess 118import subprocess
119import sys 119import sys
120try: 120
121 import urllib2 121if sys.version_info[0] == 3:
122except ImportError:
123 # For python3
124 import urllib.request 122 import urllib.request
125 import urllib.error 123 import urllib.error
126else: 124else:
127 # For python2
128 import imp 125 import imp
126 import urllib2
129 urllib = imp.new_module('urllib') 127 urllib = imp.new_module('urllib')
130 urllib.request = urllib2 128 urllib.request = urllib2
131 urllib.error = urllib2 129 urllib.error = urllib2
diff --git a/subcmds/init.py b/subcmds/init.py
index 1f915268..a44fb7a9 100644
--- a/subcmds/init.py
+++ b/subcmds/init.py
@@ -19,11 +19,11 @@ import platform
19import re 19import re
20import shutil 20import shutil
21import sys 21import sys
22try: 22
23 # For python3 23from pyversion import is_python3
24if is_python3():
24 import urllib.parse 25 import urllib.parse
25except ImportError: 26else:
26 # For python2
27 import imp 27 import imp
28 import urlparse 28 import urlparse
29 urllib = imp.new_module('urllib') 29 urllib = imp.new_module('urllib')
diff --git a/subcmds/status.py b/subcmds/status.py
index 9810337f..41c4429a 100644
--- a/subcmds/status.py
+++ b/subcmds/status.py
@@ -21,12 +21,13 @@ except ImportError:
21 import dummy_threading as _threading 21 import dummy_threading as _threading
22 22
23import glob 23import glob
24try: 24
25 # For python2 25from pyversion import is_python3
26 import StringIO as io 26if is_python3():
27except ImportError:
28 # For python3
29 import io 27 import io
28else:
29 import StringIO as io
30
30import itertools 31import itertools
31import os 32import os
32import sys 33import sys
diff --git a/subcmds/sync.py b/subcmds/sync.py
index 8fb94885..b34787d2 100644
--- a/subcmds/sync.py
+++ b/subcmds/sync.py
@@ -24,22 +24,17 @@ import socket
24import subprocess 24import subprocess
25import sys 25import sys
26import time 26import time
27try: 27
28 # For python3 28from pyversion import is_python3
29if is_python3():
29 import urllib.parse 30 import urllib.parse
30except ImportError: 31 import xmlrpc.client
31 # For python2 32else:
32 import imp 33 import imp
33 import urlparse 34 import urlparse
35 import xmlrpclib
34 urllib = imp.new_module('urllib') 36 urllib = imp.new_module('urllib')
35 urllib.parse = urlparse 37 urllib.parse = urlparse
36try:
37 # For python3
38 import xmlrpc.client
39except ImportError:
40 # For python2
41 import imp
42 import xmlrpclib
43 xmlrpc = imp.new_module('xmlrpc') 38 xmlrpc = imp.new_module('xmlrpc')
44 xmlrpc.client = xmlrpclib 39 xmlrpc.client = xmlrpclib
45 40
diff --git a/subcmds/upload.py b/subcmds/upload.py
index a34938e5..13b87784 100644
--- a/subcmds/upload.py
+++ b/subcmds/upload.py
@@ -23,10 +23,11 @@ from editor import Editor
23from error import HookError, UploadError 23from error import HookError, UploadError
24from project import RepoHook 24from project import RepoHook
25 25
26try: 26from pyversion import is_python3
27if not is_python3():
28 # pylint:disable=W0622
27 input = raw_input 29 input = raw_input
28except NameError: 30 # pylint:enable=W0622
29 pass
30 31
31UNUSUAL_COMMIT_THRESHOLD = 5 32UNUSUAL_COMMIT_THRESHOLD = 5
32 33