Use subprocess.run() instead of Popen()

Since python3.5, subprocess.run() has been introduced. It works a
wrapper of Popen, and it looks much simple and better. This uses it
instead of Popen to make our code simple.
This commit is contained in:
Takeshi KOMIYA
2019-01-12 20:14:26 +09:00
parent 71dd5f6a3a
commit 46334a2b00
9 changed files with 113 additions and 166 deletions

View File

@@ -9,7 +9,8 @@
"""
import os
from subprocess import Popen, PIPE
import subprocess
from subprocess import CalledProcessError, PIPE
from xml.etree import ElementTree
import pytest
@@ -18,13 +19,10 @@ import pytest
# check given command is runnable
def runnable(command):
try:
p = Popen(command, stdout=PIPE, stderr=PIPE)
except OSError:
# command not found
return False
else:
p.communicate()
return p.returncode == 0
subprocess.run(command, stdout=PIPE, stderr=PIPE, check=True)
return True
except (OSError, CalledProcessError):
return False # command not found or exit with non-zero
class EPUBElementTree:
@@ -377,10 +375,10 @@ def test_run_epubcheck(app):
epubcheck = os.environ.get('EPUBCHECK_PATH', '/usr/share/java/epubcheck.jar')
if runnable(['java', '-version']) and os.path.exists(epubcheck):
p = Popen(['java', '-jar', epubcheck, app.outdir / 'SphinxTests.epub'],
stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
print(stdout)
print(stderr)
assert False, 'epubcheck exited with return code %s' % p.returncode
try:
subprocess.run(['java', '-jar', epubcheck, app.outdir / 'SphinxTests.epub'],
stdout=PIPE, stderr=PIPE, check=True)
except CalledProcessError as exc:
print(exc.stdout)
print(exc.stderr)
assert False, 'epubcheck exited with return code %s' % exc.returncode