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