diff options
author | David Pursehouse <david.pursehouse@sonymobile.com> | 2013-05-17 10:49:33 +0900 |
---|---|---|
committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2013-05-23 07:28:53 +0000 |
commit | 59bbb580e34bbc5dce76dacaad9ff94f21fa396f (patch) | |
tree | 4d19216215edc174851baa10a00a3bccba9d4e45 | |
parent | da45e5d8848246cddbce80ff56786bd7330ba5af (diff) | |
download | git-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.py | 9 | ||||
-rwxr-xr-x | main.py | 15 | ||||
-rw-r--r-- | manifest_xml.py | 10 | ||||
-rw-r--r-- | project.py | 7 | ||||
-rw-r--r-- | pyversion.py | 19 | ||||
-rwxr-xr-x | repo | 8 | ||||
-rw-r--r-- | subcmds/init.py | 8 | ||||
-rw-r--r-- | subcmds/status.py | 11 | ||||
-rw-r--r-- | subcmds/sync.py | 17 | ||||
-rw-r--r-- | subcmds/upload.py | 7 |
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: | |||
25 | except ImportError: | 25 | except ImportError: |
26 | import dummy_threading as _threading | 26 | import dummy_threading as _threading |
27 | import time | 27 | import time |
28 | try: | 28 | |
29 | import urllib2 | 29 | from pyversion import is_python3 |
30 | except ImportError: | 30 | if is_python3(): |
31 | # For python3 | ||
32 | import urllib.request | 31 | import urllib.request |
33 | import urllib.error | 32 | import urllib.error |
34 | else: | 33 | else: |
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 |
@@ -22,13 +22,12 @@ import optparse | |||
22 | import os | 22 | import os |
23 | import sys | 23 | import sys |
24 | import time | 24 | import time |
25 | try: | 25 | |
26 | import urllib2 | 26 | from pyversion import is_python3 |
27 | except ImportError: | 27 | if is_python3(): |
28 | # For python3 | ||
29 | import urllib.request | 28 | import urllib.request |
30 | else: | 29 | else: |
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 | ||
51 | from subcmds import all_commands | 50 | from subcmds import all_commands |
52 | 51 | ||
53 | try: | 52 | if not is_python3(): |
53 | # pylint:disable=W0622 | ||
54 | input = raw_input | 54 | input = raw_input |
55 | except NameError: | 55 | # pylint:enable=W0622 |
56 | pass | ||
57 | 56 | ||
58 | global_options = optparse.OptionParser( | 57 | global_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 | |||
18 | import os | 18 | import os |
19 | import re | 19 | import re |
20 | import sys | 20 | import sys |
21 | try: | 21 | import xml.dom.minidom |
22 | # For python3 | 22 | |
23 | from pyversion import is_python3 | ||
24 | if is_python3(): | ||
23 | import urllib.parse | 25 | import urllib.parse |
24 | except ImportError: | 26 | else: |
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 |
30 | import xml.dom.minidom | ||
31 | 31 | ||
32 | from git_config import GitConfig | 32 | from git_config import GitConfig |
33 | from git_refs import R_HEADS, HEAD | 33 | from git_refs import R_HEADS, HEAD |
@@ -36,10 +36,11 @@ from trace import IsTrace, Trace | |||
36 | 36 | ||
37 | from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M | 37 | from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M |
38 | 38 | ||
39 | try: | 39 | from pyversion import is_python3 |
40 | if not is_python3(): | ||
41 | # pylint:disable=W0622 | ||
40 | input = raw_input | 42 | input = raw_input |
41 | except NameError: | 43 | # pylint:enable=W0622 |
42 | pass | ||
43 | 44 | ||
44 | def _lwrite(path, content): | 45 | def _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 | |||
16 | import sys | ||
17 | |||
18 | def is_python3(): | ||
19 | return sys.version_info[0] == 3 | ||
@@ -117,15 +117,13 @@ import re | |||
117 | import stat | 117 | import stat |
118 | import subprocess | 118 | import subprocess |
119 | import sys | 119 | import sys |
120 | try: | 120 | |
121 | import urllib2 | 121 | if sys.version_info[0] == 3: |
122 | except ImportError: | ||
123 | # For python3 | ||
124 | import urllib.request | 122 | import urllib.request |
125 | import urllib.error | 123 | import urllib.error |
126 | else: | 124 | else: |
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 | |||
19 | import re | 19 | import re |
20 | import shutil | 20 | import shutil |
21 | import sys | 21 | import sys |
22 | try: | 22 | |
23 | # For python3 | 23 | from pyversion import is_python3 |
24 | if is_python3(): | ||
24 | import urllib.parse | 25 | import urllib.parse |
25 | except ImportError: | 26 | else: |
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 | ||
23 | import glob | 23 | import glob |
24 | try: | 24 | |
25 | # For python2 | 25 | from pyversion import is_python3 |
26 | import StringIO as io | 26 | if is_python3(): |
27 | except ImportError: | ||
28 | # For python3 | ||
29 | import io | 27 | import io |
28 | else: | ||
29 | import StringIO as io | ||
30 | |||
30 | import itertools | 31 | import itertools |
31 | import os | 32 | import os |
32 | import sys | 33 | import 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 | |||
24 | import subprocess | 24 | import subprocess |
25 | import sys | 25 | import sys |
26 | import time | 26 | import time |
27 | try: | 27 | |
28 | # For python3 | 28 | from pyversion import is_python3 |
29 | if is_python3(): | ||
29 | import urllib.parse | 30 | import urllib.parse |
30 | except ImportError: | 31 | import xmlrpc.client |
31 | # For python2 | 32 | else: |
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 |
36 | try: | ||
37 | # For python3 | ||
38 | import xmlrpc.client | ||
39 | except 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 | |||
23 | from error import HookError, UploadError | 23 | from error import HookError, UploadError |
24 | from project import RepoHook | 24 | from project import RepoHook |
25 | 25 | ||
26 | try: | 26 | from pyversion import is_python3 |
27 | if not is_python3(): | ||
28 | # pylint:disable=W0622 | ||
27 | input = raw_input | 29 | input = raw_input |
28 | except NameError: | 30 | # pylint:enable=W0622 |
29 | pass | ||
30 | 31 | ||
31 | UNUSUAL_COMMIT_THRESHOLD = 5 | 32 | UNUSUAL_COMMIT_THRESHOLD = 5 |
32 | 33 | ||