Improve gclient Python 3 compatibility
This enables gclient sync and gclient runhooks to run, barring hook script failures.
git cl upload also now works.
The scripts still work with Python 2.
There are no intended behaviour changes.
Bug: 942522
Change-Id: I2ac587b5f803ba7f5bb5e412337ce049f4b1a741
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1524583
Commit-Queue: Raul Tambre <raul@tambre.ee>
Reviewed-by: Dirk Pranke <dpranke@chromium.org>
diff --git a/gclient_scm.py b/gclient_scm.py
index 69faa6c..00263b0 100644
--- a/gclient_scm.py
+++ b/gclient_scm.py
@@ -18,7 +18,11 @@
import tempfile
import threading
import traceback
-import urlparse
+
+try:
+ import urlparse
+except ImportError: # For Py3 compatibility
+ import urllib.parse as urlparse
import download_from_google_storage
import gclient_utils
@@ -311,7 +315,8 @@
if file_list is not None:
files = self._Capture(
['-c', 'core.quotePath=false', 'ls-files']).splitlines()
- file_list.extend([os.path.join(self.checkout_path, f) for f in files])
+ file_list.extend(
+ [os.path.join(self.checkout_path, f.decode()) for f in files])
def _DisableHooks(self):
hook_dir = os.path.join(self.checkout_path, '.git', 'hooks')
@@ -590,10 +595,10 @@
# Skip url auto-correction if remote.origin.gclient-auto-fix-url is set.
# This allows devs to use experimental repos which have a different url
# but whose branch(s) are the same as official repos.
- if (current_url.rstrip('/') != url.rstrip('/') and
- url != 'git://foo' and
+ if (current_url.rstrip(b'/') != url.rstrip('/') and url != 'git://foo' and
subprocess2.capture(
- ['git', 'config', 'remote.%s.gclient-auto-fix-url' % self.remote],
+ ['git', 'config',
+ 'remote.%s.gclient-auto-fix-url' % self.remote],
cwd=self.checkout_path).strip() != 'False'):
self.Print('_____ switching %s to a new upstream' % self.relpath)
if not (options.force or options.reset):
@@ -1117,7 +1122,7 @@
try:
rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
- except subprocess2.CalledProcessError, e:
+ except subprocess2.CalledProcessError as e:
if (re.match(r'cannot rebase: you have unstaged changes', e.stderr) or
re.match(r'cannot rebase: your index contains uncommitted changes',
e.stderr)):
@@ -1456,9 +1461,9 @@
try:
ensure_file = None
with tempfile.NamedTemporaryFile(
- suffix='.ensure', delete=False) as ensure_file:
+ suffix='.ensure', delete=False, mode='w') as ensure_file:
ensure_file.write('$ParanoidMode CheckPresence\n\n')
- for subdir, packages in sorted(self._packages_by_subdir.iteritems()):
+ for subdir, packages in sorted(self._packages_by_subdir.items()):
ensure_file.write('@Subdir %s\n' % subdir)
for package in sorted(packages, key=lambda p: p.name):
ensure_file.write('%s %s\n' % (package.name, package.version))