misc: use new subprocess.DEVNULL (#42522)

This commit is contained in:
Frédéric Péters 2020-05-06 09:07:18 +02:00
parent 757d0333fb
commit a341388806
2 changed files with 6 additions and 5 deletions

View File

@ -55,7 +55,7 @@ from .template import Template
from django.utils.six import BytesIO, StringIO
try:
subprocess.check_call(['which', 'pdftoppm'], stdout=open('/dev/null', 'w'))
subprocess.check_call(['which', 'pdftoppm'], stdout=subprocess.DEVNULL)
HAS_PDFTOPPM = True
except subprocess.CalledProcessError:
HAS_PDFTOPPM = False

View File

@ -64,12 +64,12 @@ XLINK_HREF = '{%s}href' % XLINK_NS
NAME = '{%s}name' % OO_TEXT_NS
try:
subprocess.check_call(['which', 'libreoffice'], stdout=open('/dev/null', 'w'))
subprocess.check_call(['which', 'libreoffice'], stdout=subprocess.DEVNULL)
def transform_to_pdf(instream):
try:
temp_dir = tempfile.mkdtemp()
with open('/dev/null') as dev_null, tempfile.NamedTemporaryFile(dir=temp_dir) as infile:
with tempfile.NamedTemporaryFile(dir=temp_dir) as infile:
while True:
chunk = instream.read(100000)
if not chunk:
@ -77,8 +77,9 @@ try:
infile.write(chunk)
infile.flush()
subprocess.check_call(['libreoffice', '--headless', '--convert-to', 'pdf',
infile.name, '--outdir', temp_dir], stdout=dev_null,
stderr=dev_null)
infile.name, '--outdir', temp_dir],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
return open(infile.name + '.pdf', 'rb')
except subprocess.CalledProcessError:
raise Exception('libreoffice is failing')