Jelajahi Sumber

depot_tools: Make some changes to make metrics collection compatible with Python 3.

Bug: 984182
Change-Id: I55e9e83d01d5a86464cc234c083e4212f0ba4a1e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1713217
Auto-Submit: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>
Commit-Queue: Andrii Shyshkalov <tandrii@chromium.org>
Edward Lemur 6 tahun lalu
induk
melakukan
73065b2067
4 mengubah file dengan 11 tambahan dan 10 penghapusan
  1. 1 1
      metrics.py
  2. 1 1
      metrics_utils.py
  3. 2 2
      tests/metrics_test.py
  4. 7 6
      upload_metrics.py

+ 1 - 1
metrics.py

@@ -185,7 +185,7 @@ class MetricsCollector(object):
     # so that we are able to return immediately, leaving the upload running in
     # so that we are able to return immediately, leaving the upload running in
     # the background.
     # the background.
     p = subprocess.Popen([sys.executable, UPLOAD_SCRIPT], stdin=subprocess.PIPE)
     p = subprocess.Popen([sys.executable, UPLOAD_SCRIPT], stdin=subprocess.PIPE)
-    p.stdin.write(json.dumps(self._reported_metrics))
+    p.stdin.write(json.dumps(self._reported_metrics).encode('utf-8'))
 
 
   def _collect_metrics(self, func, command_name, *args, **kwargs):
   def _collect_metrics(self, func, command_name, *args, **kwargs):
     # If we're already collecting metrics, just execute the function.
     # If we're already collecting metrics, just execute the function.

+ 1 - 1
metrics_utils.py

@@ -168,7 +168,7 @@ def get_git_version():
       ['git', '--version'],
       ['git', '--version'],
       stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
       stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
   stdout, _ = p.communicate()
   stdout, _ = p.communicate()
-  match = GIT_VERSION_RE.match(stdout)
+  match = GIT_VERSION_RE.match(stdout.decode('utf-8'))
   if not match:
   if not match:
     return None
     return None
   return '%s.%s.%s' % match.groups()
   return '%s.%s.%s' % match.groups()

+ 2 - 2
tests/metrics_test.py

@@ -705,7 +705,7 @@ class MetricsUtilsTest(unittest.TestCase):
   def test_get_git_version(self, mockPopen):
   def test_get_git_version(self, mockPopen):
     """Tests that we can get the git version."""
     """Tests that we can get the git version."""
     mockProcess = mock.Mock()
     mockProcess = mock.Mock()
-    mockProcess.communicate.side_effect = [('git version 2.18.0.123.foo', '')]
+    mockProcess.communicate.side_effect = [(b'git version 2.18.0.123.foo', '')]
     mockPopen.side_effect = [mockProcess]
     mockPopen.side_effect = [mockProcess]
 
 
     self.assertEqual('2.18.0', metrics_utils.get_git_version())
     self.assertEqual('2.18.0', metrics_utils.get_git_version())
@@ -714,7 +714,7 @@ class MetricsUtilsTest(unittest.TestCase):
   def test_get_git_version_unrecognized(self, mockPopen):
   def test_get_git_version_unrecognized(self, mockPopen):
     """Tests that we can get the git version."""
     """Tests that we can get the git version."""
     mockProcess = mock.Mock()
     mockProcess = mock.Mock()
-    mockProcess.communicate.side_effect = [('Blah blah blah', 'blah blah')]
+    mockProcess.communicate.side_effect = [(b'Blah blah blah', 'blah blah')]
     mockPopen.side_effect = [mockProcess]
     mockPopen.side_effect = [mockProcess]
 
 
     self.assertIsNone(metrics_utils.get_git_version())
     self.assertIsNone(metrics_utils.get_git_version())

+ 7 - 6
upload_metrics.py

@@ -4,18 +4,19 @@
 # found in the LICENSE file.
 # found in the LICENSE file.
 
 
 import sys
 import sys
-import urllib2
+
+from third_party.six.moves import urllib
+from third_party.six.moves import input # pylint: disable=redefined-builtin
 
 
 import metrics_utils
 import metrics_utils
 
 
 
 
 def main():
 def main():
-  metrics = raw_input()
+  metrics = input()
   try:
   try:
-    urllib2.urlopen(metrics_utils.APP_URL + '/upload', metrics)
-  except urllib2.HTTPError:
-    pass
-  except urllib2.URLError:
+    urllib.request.urlopen(
+        metrics_utils.APP_URL + '/upload', metrics.encode('utf-8'))
+  except (urllib.error.HTTPError, urllib.error.URLError):
     pass
     pass
 
 
   return 0
   return 0