Throw error for unicode URLs in .gclient

This is no longer supported to make the move to Python 3 easier.
Only some very old/weird user configurations may have this, so this provides a clear actionable error message.

Bug: 942522
Change-Id: I88106089c53d84b19c4f77b7e60fe251082122d7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1539957
Reviewed-by: Dirk Pranke <dpranke@chromium.org>
Commit-Queue: Raul Tambre <raul@tambre.ee>
diff --git a/gclient.py b/gclient.py
index 034314a..a60d331 100755
--- a/gclient.py
+++ b/gclient.py
@@ -1374,7 +1374,7 @@
 '''  % {'checkout_path': os.path.join(self.root_dir, dep.name),
         'expected_url': dep.url,
         'expected_scm': dep.GetScmName(),
-        'mirror_string' : mirror_string,
+        'mirror_string': mirror_string,
         'actual_url': actual_url,
         'actual_scm': dep.GetScmName()})
 
@@ -1387,6 +1387,21 @@
     except SyntaxError as e:
       gclient_utils.SyntaxErrorToError('.gclient', e)
 
+    # Supporting Unicode URLs in both Python 2 and 3 is annoying. Try to
+    # convert to ASCII in case the URL doesn't actually have any Unicode
+    # characters, otherwise raise an error.
+    # This isn't an issue on Python 3 because everything's Unicode anyway.
+    if sys.version_info.major == 2:
+      try:
+        url = config_dict['solutions'][0]['url']
+        if isinstance(url, unicode):
+          config_dict['solutions'][0]['url'] = url.encode('ascii')
+      except UnicodeEncodeError:
+        raise gclient_utils.Error(
+            "Invalid .gclient file. The url mustn't be unicode.")
+      except KeyError:
+        pass
+
     # Append any target OS that is not already being enforced to the tuple.
     target_os = config_dict.get('target_os', [])
     if config_dict.get('target_os_only', False):