Catch FileNotFoundError from Popen and re-raise a more helpful message

This commit is contained in:
2019-04-03 08:13:01 +02:00
parent 6a8fbe71a4
commit 846a5b3149
2 changed files with 21 additions and 12 deletions

View File

@@ -353,9 +353,16 @@ CREATE VIEW IF NOT EXISTS Capacity_View
def mdb_get_data_text(s3db, filename, table): def mdb_get_data_text(s3db, filename, table):
print("Reading %s..." % table) print("Reading %s..." % table)
# TODO after dropping Python 2 support - use Popen as contextmanager # TODO after dropping Python 2 support - use Popen as contextmanager
mdb_sql = sp.Popen(['mdb-export', '-I', 'postgres', filename, table], try:
bufsize=-1, stdin=None, stdout=sp.PIPE, mdb_sql = sp.Popen(['mdb-export', '-I', 'postgres', filename, table],
universal_newlines=True) bufsize=-1, stdin=None, stdout=sp.PIPE,
universal_newlines=True)
except OSError as e:
if e.errno == 2:
raise RuntimeError('Could not locate the `mdb-export` executable. '
'Check that mdbtools is properly installed.')
else:
raise
try: try:
# Initialize values to avoid NameError in except clause # Initialize values to avoid NameError in except clause
mdb_output = '' mdb_output = ''
@@ -381,9 +388,16 @@ def mdb_get_data_text(s3db, filename, table):
def mdb_get_data_numeric(s3db, filename, table): def mdb_get_data_numeric(s3db, filename, table):
print("Reading %s..." % table) print("Reading %s..." % table)
# TODO after dropping Python 2 support - use Popen as contextmanager # TODO after dropping Python 2 support - use Popen as contextmanager
mdb_sql = sp.Popen(['mdb-export', filename, table], try:
bufsize=-1, stdin=None, stdout=sp.PIPE, mdb_sql = sp.Popen(['mdb-export', filename, table],
universal_newlines=True) bufsize=-1, stdin=None, stdout=sp.PIPE,
universal_newlines=True)
except OSError as e:
if e.errno == 2:
raise RuntimeError('Could not locate the `mdb-export` executable. '
'Check that mdbtools is properly installed.')
else:
raise
try: try:
mdb_csv = csv.reader(mdb_sql.stdout) mdb_csv = csv.reader(mdb_sql.stdout)
mdb_headers = next(mdb_csv) mdb_headers = next(mdb_csv)

View File

@@ -3,7 +3,6 @@
import os import os
import sqlite3 import sqlite3
import subprocess import subprocess
import sys
import pytest import pytest
@@ -28,11 +27,7 @@ def test_convert_Arbin_no_mdbtools(testdata_dir, tmpdir):
"""Checks that the conversion fails with an appropriate error message.""" """Checks that the conversion fails with an appropriate error message."""
res_file = os.path.join(testdata_dir, 'arbin1.res') res_file = os.path.join(testdata_dir, 'arbin1.res')
sqlite_file = os.path.join(str(tmpdir), 'arbin1.s3db') sqlite_file = os.path.join(str(tmpdir), 'arbin1.s3db')
if sys.version_info >= (3, 3): with pytest.raises(RuntimeError, match="Could not locate the `mdb-export` executable."):
expected_exception = FileNotFoundError
else:
expected_exception = OSError
with pytest.raises(expected_exception, match="No such file or directory: 'mdb-export'"):
res2sqlite.convert_arbin_to_sqlite(res_file, sqlite_file) res2sqlite.convert_arbin_to_sqlite(res_file, sqlite_file)