Commit cf06cad1 authored by Edward Lesmes's avatar Edward Lesmes Committed by LUCI CQ

[subprocess2] Replace VOID with DEVNULL

subprocess.DEVNULL was introduced in Python3 to serve same purpose
as subprocess2.VOID, so rename VOID to DEVNULL in subprocess2.

Change-Id: I6dade3306ffc3bc2441ac6083f362b099c2427e9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2587758Reviewed-by: 's avatarAnthony Polito <apolito@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
parent 9c7f6c25
......@@ -40,7 +40,7 @@ def need_to_update(branch):
try:
cmd = [sys.executable, GCLIENT, 'revinfo']
subprocess.check_call(
cmd, cwd=os.path.join(TARGET_DIR), stdout=subprocess.VOID)
cmd, cwd=os.path.join(TARGET_DIR), stdout=subprocess.DEVNULL)
except subprocess.CalledProcessError:
return True # Gclient failed, definitely need to update.
except OSError:
......@@ -53,7 +53,7 @@ def need_to_update(branch):
subprocess.check_call(
['git', 'fetch', 'origin'], cwd=INFRA_DIR,
stdout=subprocess.VOID, stderr=subprocess.STDOUT)
stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
origin_rev = get_git_rev(INFRA_DIR, 'origin/%s' % (branch,))
return origin_rev != local_rev
......@@ -70,11 +70,11 @@ def ensure_infra(branch):
subprocess.check_call(
[sys.executable, os.path.join(SCRIPT_DIR, 'fetch.py'), 'infra'],
cwd=TARGET_DIR,
stdout=subprocess.VOID)
stdout=subprocess.DEVNULL)
subprocess.check_call(
[sys.executable, GCLIENT, 'sync', '--revision', 'origin/%s' % (branch,)],
cwd=TARGET_DIR,
stdout=subprocess.VOID)
stdout=subprocess.DEVNULL)
sys.stderr.write(' done.\n')
sys.stderr.flush()
......
......@@ -195,7 +195,7 @@ def RunGit(args, **kwargs):
def RunGitWithCode(args, suppress_stderr=False):
"""Returns return code and stdout."""
if suppress_stderr:
stderr = subprocess2.VOID
stderr = subprocess2.DEVNULL
else:
stderr = sys.stderr
try:
......
......@@ -743,7 +743,7 @@ def run_stream(*cmd, **kwargs):
stderr is dropped to avoid races if the process outputs to both stdout and
stderr.
"""
kwargs.setdefault('stderr', subprocess2.VOID)
kwargs.setdefault('stderr', subprocess2.DEVNULL)
kwargs.setdefault('stdout', subprocess2.PIPE)
kwargs.setdefault('shell', False)
cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd
......@@ -760,7 +760,7 @@ def run_stream_with_retcode(*cmd, **kwargs):
Raises subprocess2.CalledProcessError on nonzero return code.
"""
kwargs.setdefault('stderr', subprocess2.VOID)
kwargs.setdefault('stderr', subprocess2.DEVNULL)
kwargs.setdefault('stdout', subprocess2.PIPE)
kwargs.setdefault('shell', False)
cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd
......
......@@ -63,10 +63,10 @@ def check_call(*args, **kwargs):
def return_code(*args, **kwargs):
"""subprocess2.call() passing shell=True on Windows for git and
subprocess2.VOID for stdout and stderr."""
subprocess2.DEVNULL for stdout and stderr."""
kwargs.setdefault('shell', NEED_SHELL)
kwargs.setdefault('stdout', subprocess2.VOID)
kwargs.setdefault('stderr', subprocess2.VOID)
kwargs.setdefault('stdout', subprocess2.DEVNULL)
kwargs.setdefault('stderr', subprocess2.DEVNULL)
return subprocess2.call(*args, **kwargs)
......
......@@ -75,8 +75,8 @@ def determine_scm(root):
try:
subprocess2.check_call(
['git', 'rev-parse', '--show-cdup'],
stdout=subprocess2.VOID,
stderr=subprocess2.VOID,
stdout=subprocess2.DEVNULL,
stderr=subprocess2.DEVNULL,
cwd=root)
return 'git'
except (OSError, subprocess2.CalledProcessError):
......
......@@ -21,18 +21,18 @@ import threading
if sys.version_info.major == 2:
import Queue
codecs.lookup('string-escape')
# Sends stdout or stderr to os.devnull.
DEVNULL = open(os.devnull, 'r+')
else:
import queue as Queue
# pylint: disable=redefined-builtin
basestring = (str, bytes)
DEVNULL = subprocess.DEVNULL
# Constants forwarded from subprocess.
PIPE = subprocess.PIPE
STDOUT = subprocess.STDOUT
# Sends stdout or stderr to os.devnull.
VOID = open(os.devnull, 'w')
VOID_INPUT = open(os.devnull, 'r')
class CalledProcessError(subprocess.CalledProcessError):
......@@ -107,7 +107,7 @@ class Popen(subprocess.Popen):
in English.
- Sets shell=True on windows by default. You can override this by forcing
shell parameter to a value.
- Adds support for VOID to not buffer when not needed.
- Adds support for DEVNULL to not buffer when not needed.
- Adds self.start property.
Note: Popen() can throw OSError when cwd or args[0] doesn't exist. Translate
......@@ -197,16 +197,16 @@ def communicate(args, **kwargs):
def call(args, **kwargs):
"""Emulates subprocess.call().
Automatically convert stdout=PIPE or stderr=PIPE to VOID.
Automatically convert stdout=PIPE or stderr=PIPE to DEVNULL.
In no case they can be returned since no code path raises
subprocess2.CalledProcessError.
Returns exit code.
"""
if kwargs.get('stdout') == PIPE:
kwargs['stdout'] = VOID
kwargs['stdout'] = DEVNULL
if kwargs.get('stderr') == PIPE:
kwargs['stderr'] = VOID
kwargs['stderr'] = DEVNULL
return communicate(args, **kwargs)[1]
......@@ -236,7 +236,7 @@ def capture(args, **kwargs):
- Discards returncode.
- Blocks stdin by default if not specified since no output will be visible.
"""
kwargs.setdefault('stdin', VOID_INPUT)
kwargs.setdefault('stdin', DEVNULL)
# Like check_output, deny the caller from using stdout arg.
return communicate(args, stdout=PIPE, **kwargs)[0][0]
......@@ -252,7 +252,7 @@ def check_output(args, **kwargs):
- Blocks stdin by default if not specified since no output will be visible.
- As per doc, "The stdout argument is not allowed as it is used internally."
"""
kwargs.setdefault('stdin', VOID_INPUT)
kwargs.setdefault('stdin', DEVNULL)
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it would be overridden.')
return check_call_out(args, stdout=PIPE, **kwargs)[0]
......@@ -42,7 +42,7 @@ class DefaultsTest(unittest.TestCase):
self.assertEqual(
'stdout', subprocess2.capture(['foo'], a=True))
mockCommunicate.assert_called_with(
['foo'], a=True, stdin=subprocess2.VOID_INPUT, stdout=subprocess2.PIPE)
['foo'], a=True, stdin=subprocess2.DEVNULL, stdout=subprocess2.PIPE)
@mock.patch('subprocess2.Popen')
def test_communicate_defaults(self, mockPopen):
......@@ -80,7 +80,7 @@ class DefaultsTest(unittest.TestCase):
mockCommunicate.return_value = (('stdout', 'stderr'), 0)
self.assertEqual('stdout', subprocess2.check_output(['foo'], a=True))
mockCommunicate.assert_called_with(
['foo'], a=True, stdin=subprocess2.VOID_INPUT, stdout=subprocess2.PIPE)
['foo'], a=True, stdin=subprocess2.DEVNULL, stdout=subprocess2.PIPE)
@mock.patch('subprocess.Popen.__init__')
def test_env_type(self, mockPopen):
......@@ -241,15 +241,15 @@ class SmokeTests(unittest.TestCase):
def test_stdin_void(self):
res = subprocess2.communicate(
TEST_COMMAND + ['--read'],
stdin=subprocess2.VOID_INPUT)
stdin=subprocess2.DEVNULL)
self._check_res(res, None, None, 0)
@_run_test(with_subprocess=False)
def test_stdin_void_stdout(self, c, cmd, un, subp):
# Make sure a mix ofsubprocess2.VOID andsubprocess2.PIPE works.
# Make sure a mix ofsubprocess2.DEVNULL andsubprocess2.PIPE works.
res = subprocess2.communicate(
cmd + ['--stdout', '--read'],
stdin=subprocess2.VOID_INPUT,
stdin=subprocess2.DEVNULL,
stdout=subprocess2.PIPE,
universal_newlines=un,
shell=False)
......@@ -259,7 +259,7 @@ class SmokeTests(unittest.TestCase):
def test_stdout_void(self, c, cmd, un, subp):
res = subprocess2.communicate(
cmd + ['--stdout', '--stderr'],
stdout=subprocess2.VOID,
stdout=subprocess2.DEVNULL,
stderr=subprocess2.PIPE,
universal_newlines=un)
self._check_res(res, None, c('a\nbb\nccc\n'), 0)
......@@ -269,7 +269,7 @@ class SmokeTests(unittest.TestCase):
res = subprocess2.communicate(
cmd + ['--stdout', '--stderr'],
stdout=subprocess2.PIPE,
stderr=subprocess2.VOID,
stderr=subprocess2.DEVNULL,
universal_newlines=un)
self._check_res(res, c('A\nBB\nCCC\n'), None, 0)
......@@ -277,7 +277,7 @@ class SmokeTests(unittest.TestCase):
def test_stdout_void_stderr_redirect(self, c, cmd, un, subp):
res = subprocess2.communicate(
cmd + ['--stdout', '--stderr'],
stdout=subprocess2.VOID,
stdout=subprocess2.DEVNULL,
stderr=subprocess2.STDOUT,
universal_newlines=un)
self._check_res(res, None, None, 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment