From e1a9c8db7e7cf6ba7d70b06ec3f3cf6d74451680 Mon Sep 17 00:00:00 2001 From: Edward Lesmes Date: Wed, 15 Apr 2020 02:54:55 +0000 Subject: [PATCH] Reland "metrics: Use vpython3 instead of sys.executable" This is a reland of 310e1e814d729f68deac5f6c5f21f377c2883dac Use subprocess2, which will set shell=True when running on Windows. Original change's description: > metrics: Use vpython3 instead of sys.executable > > Apparently, uploading metrics might happen after sys.executable > has been deleted. > > Bug: 1068263 > Change-Id: Id4ab465bb97d146b688f42fb2002e29372e65555 > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2148879 > Commit-Queue: Anthony Polito > Reviewed-by: Anthony Polito Bug: 1068263 Change-Id: I19c915f3e8862ae3248b01e0eccdbdf0bfda7369 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2150170 Reviewed-by: Josip Sokcevic Commit-Queue: Edward Lesmes --- metrics.py | 4 ++-- tests/metrics_test.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/metrics.py b/metrics.py index 7b8d04a43..84d775e4f 100644 --- a/metrics.py +++ b/metrics.py @@ -9,7 +9,6 @@ import contextlib import functools import json import os -import subprocess import sys import tempfile import threading @@ -24,6 +23,7 @@ except ImportError: # For Py3 compatibility import detect_host_arch import gclient_utils import metrics_utils +import subprocess2 DEPOT_TOOLS = os.path.dirname(os.path.abspath(__file__)) @@ -184,7 +184,7 @@ class MetricsCollector(object): # We invoke a subprocess, and use stdin.write instead of communicate(), # so that we are able to return immediately, leaving the upload running in # the background. - p = subprocess.Popen([sys.executable, UPLOAD_SCRIPT], stdin=subprocess.PIPE) + p = subprocess2.Popen(['vpython3', UPLOAD_SCRIPT], stdin=subprocess2.PIPE) p.stdin.write(json.dumps(self._reported_metrics).encode('utf-8')) def _collect_metrics(self, func, command_name, *args, **kwargs): diff --git a/tests/metrics_test.py b/tests/metrics_test.py index 8d2863337..5e2a0885a 100644 --- a/tests/metrics_test.py +++ b/tests/metrics_test.py @@ -46,7 +46,7 @@ class MetricsCollectorTest(unittest.TestCase): # version. mock.patch('metrics.metrics_utils.CURRENT_VERSION', 0).start() mock.patch('metrics.urllib', self.urllib).start() - mock.patch('metrics.subprocess.Popen', self.Popen).start() + mock.patch('metrics.subprocess2.Popen', self.Popen).start() mock.patch('metrics.gclient_utils.FileWrite', self.FileWrite).start() mock.patch('metrics.gclient_utils.FileRead', self.FileRead).start() mock.patch('metrics.metrics_utils.print_notice', self.print_notice).start() @@ -138,7 +138,7 @@ class MetricsCollectorTest(unittest.TestCase): self.default_metrics.update(update_metrics or {}) # Assert we invoked the script to upload them. self.Popen.assert_called_with( - [sys.executable, metrics.UPLOAD_SCRIPT], stdin=metrics.subprocess.PIPE) + ['vpython3', metrics.UPLOAD_SCRIPT], stdin=metrics.subprocess2.PIPE) # Assert we collected the right metrics. write_call = self.Popen.return_value.stdin.write.call_args collected_metrics = json.loads(write_call[0][0])