Some fixes for supporting python3
* Fix imports.
* Use python3 syntax.
* Wrap map() calls with list().
* Use list() only wherever needed.
(Thanks Conley!)
* Fix dictionary iteration methods
(s/iteritems/items/).
* Make use of sorted() in appropriate places
* Use iterators directly in the loop.
* Don't use .keys() wherever it isn't needed.
* Use sys.maxsize instead of sys.maxint
TODO:
* Make repo work fully with python3. :)
Some of this was done by the '2to3' tool [1], by
applying the needed fixes in a way that doesn't
break compatibility with python2.
Links:
[1]: http://docs.python.org/2/library/2to3.html
Change-Id: Ibdf3bf9a530d716db905733cb9bfef83a48820f7
Signed-off-by: Chirayu Desai <cdesai@cyanogenmod.org>
diff --git a/subcmds/__init__.py b/subcmds/__init__.py
index 1fac802..84efb4d 100644
--- a/subcmds/__init__.py
+++ b/subcmds/__init__.py
@@ -38,8 +38,8 @@
try:
cmd = getattr(mod, clsn)()
except AttributeError:
- raise SyntaxError, '%s/%s does not define class %s' % (
- __name__, py, clsn)
+ raise SyntaxError('%s/%s does not define class %s' % (
+ __name__, py, clsn))
name = name.replace('_', '-')
cmd.NAME = name
diff --git a/subcmds/branches.py b/subcmds/branches.py
index 06d45ab..c2e7c4b 100644
--- a/subcmds/branches.py
+++ b/subcmds/branches.py
@@ -98,14 +98,13 @@
project_cnt = len(projects)
for project in projects:
- for name, b in project.GetBranches().iteritems():
+ for name, b in project.GetBranches().items():
b.project = project
if name not in all_branches:
all_branches[name] = BranchInfo(name)
all_branches[name].add(b)
- names = all_branches.keys()
- names.sort()
+ names = list(sorted(all_branches))
if not names:
print(' (no branches)', file=sys.stderr)
diff --git a/subcmds/help.py b/subcmds/help.py
index 7842882..4aa3f86 100644
--- a/subcmds/help.py
+++ b/subcmds/help.py
@@ -34,8 +34,7 @@
def _PrintAllCommands(self):
print('usage: repo COMMAND [ARGS]')
print('The complete list of recognized repo commands are:')
- commandNames = self.commands.keys()
- commandNames.sort()
+ commandNames = list(sorted(self.commands))
maxlen = 0
for name in commandNames:
@@ -55,10 +54,9 @@
def _PrintCommonCommands(self):
print('usage: repo COMMAND [ARGS]')
print('The most commonly used repo commands are:')
- commandNames = [name
- for name in self.commands.keys()
- if self.commands[name].common]
- commandNames.sort()
+ commandNames = list(sorted([name
+ for name, command in self.commands.items()
+ if command.common]))
maxlen = 0
for name in commandNames:
diff --git a/subcmds/info.py b/subcmds/info.py
index 325874b..c10e56c 100644
--- a/subcmds/info.py
+++ b/subcmds/info.py
@@ -163,7 +163,7 @@
all_branches = []
for project in self.GetProjects(args):
br = [project.GetUploadableBranch(x)
- for x in project.GetBranches().keys()]
+ for x in project.GetBranches()]
br = [x for x in br if x]
if self.opt.current_branch:
br = [x for x in br if x.name == project.CurrentBranch]
diff --git a/subcmds/overview.py b/subcmds/overview.py
index 418459a..eed8cf2 100644
--- a/subcmds/overview.py
+++ b/subcmds/overview.py
@@ -42,7 +42,7 @@
all_branches = []
for project in self.GetProjects(args):
br = [project.GetUploadableBranch(x)
- for x in project.GetBranches().keys()]
+ for x in project.GetBranches()]
br = [x for x in br if x]
if opt.current_branch:
br = [x for x in br if x.name == project.CurrentBranch]
diff --git a/subcmds/status.py b/subcmds/status.py
index cce00c8..9810337 100644
--- a/subcmds/status.py
+++ b/subcmds/status.py
@@ -21,10 +21,15 @@
import dummy_threading as _threading
import glob
+try:
+ # For python2
+ import StringIO as io
+except ImportError:
+ # For python3
+ import io
import itertools
import os
import sys
-import StringIO
from color import Coloring
@@ -142,7 +147,7 @@
for project in all_projects:
sem.acquire()
- class BufList(StringIO.StringIO):
+ class BufList(io.StringIO):
def dump(self, ostream):
for entry in self.buflist:
ostream.write(entry)
@@ -182,7 +187,7 @@
try:
os.chdir(self.manifest.topdir)
- outstring = StringIO.StringIO()
+ outstring = io.StringIO()
self._FindOrphans(glob.glob('.*') + \
glob.glob('*'), \
proj_dirs, proj_dirs_parents, outstring)
diff --git a/subcmds/sync.py b/subcmds/sync.py
index 42c5f91..8fb9488 100644
--- a/subcmds/sync.py
+++ b/subcmds/sync.py
@@ -24,8 +24,24 @@
import subprocess
import sys
import time
-import urlparse
-import xmlrpclib
+try:
+ # For python3
+ import urllib.parse
+except ImportError:
+ # For python2
+ import imp
+ import urlparse
+ 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
try:
import threading as _threading
@@ -498,7 +514,7 @@
file=sys.stderr)
else:
try:
- parse_result = urlparse.urlparse(manifest_server)
+ parse_result = urllib.parse(manifest_server)
if parse_result.hostname:
username, _account, password = \
info.authenticators(parse_result.hostname)
@@ -516,7 +532,7 @@
1)
try:
- server = xmlrpclib.Server(manifest_server)
+ server = xmlrpc.client.Server(manifest_server)
if opt.smart_sync:
p = self.manifest.manifestProject
b = p.GetBranch(p.CurrentBranch)
@@ -525,8 +541,7 @@
branch = branch[len(R_HEADS):]
env = os.environ.copy()
- if (env.has_key('TARGET_PRODUCT') and
- env.has_key('TARGET_BUILD_VARIANT')):
+ if 'TARGET_PRODUCT' in env and 'TARGET_BUILD_VARIANT' in env:
target = '%s-%s' % (env['TARGET_PRODUCT'],
env['TARGET_BUILD_VARIANT'])
[success, manifest_str] = server.GetApprovedManifest(branch, target)
@@ -554,11 +569,11 @@
else:
print('error: %s' % manifest_str, file=sys.stderr)
sys.exit(1)
- except (socket.error, IOError, xmlrpclib.Fault) as e:
+ except (socket.error, IOError, xmlrpc.client.Fault) as e:
print('error: cannot connect to manifest server %s:\n%s'
% (self.manifest.manifest_server, e), file=sys.stderr)
sys.exit(1)
- except xmlrpclib.ProtocolError as e:
+ except xmlrpc.client.ProtocolError as e:
print('error: cannot connect to manifest server %s:\n%d %s'
% (self.manifest.manifest_server, e.errcode, e.errmsg),
file=sys.stderr)
diff --git a/subcmds/upload.py b/subcmds/upload.py
index 48ee685..a34938e 100644
--- a/subcmds/upload.py
+++ b/subcmds/upload.py
@@ -23,6 +23,11 @@
from error import HookError, UploadError
from project import RepoHook
+try:
+ input = raw_input
+except NameError:
+ pass
+
UNUSUAL_COMMIT_THRESHOLD = 5
def _ConfirmManyUploads(multiple_branches=False):
@@ -33,7 +38,7 @@
print('ATTENTION: You are uploading an unusually high number of commits.')
print('YOU PROBABLY DO NOT MEAN TO DO THIS. (Did you rebase across '
'branches?)')
- answer = raw_input("If you are sure you intend to do this, type 'yes': ").strip()
+ answer = input("If you are sure you intend to do this, type 'yes': ").strip()
return answer == "yes"
def _die(fmt, *args):