From 22b2406b16bc189742f50e00340f058cbe779598 Mon Sep 17 00:00:00 2001 From: "tim.reichard" Date: Thu, 11 Feb 2021 12:10:25 -0600 Subject: [PATCH] Updating pyodbc to work on windows OS --- HISTORY.rst | 6 ++++++ aioradio/pyodbc.py | 24 ++++++++++++++++++------ aioradio/tests/file_ingestion_test.py | 6 +++--- aioradio/tests/pyodbc_test.py | 12 ++++++------ setup.py | 2 +- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index a9a7165..7f14097 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,12 @@ History ======= +v0.10.4 (2021-02-011) +----------------------- + +* Add pyodbc driver string for windows OS. + + v0.10.3 (2021-02-08) ----------------------- diff --git a/aioradio/pyodbc.py b/aioradio/pyodbc.py index aca17f7..a690986 100644 --- a/aioradio/pyodbc.py +++ b/aioradio/pyodbc.py @@ -4,10 +4,13 @@ # pylint: disable=too-many-arguments import os +import platform from typing import Any, List, Union import pyodbc +OPERATING_SYSTEM = platform.system() + # driver location varies based on OS. add to this list if necessary... UNIXODBC_DRIVER_PATHS = [ '/usr/lib/libtdsodbc.so', @@ -39,7 +42,7 @@ async def establish_pyodbc_connection( host: str, user: str, pwd: str, - port: int=1433, + port: int=None, database: str='', driver: str='', autocommit: bool=False @@ -62,11 +65,20 @@ async def establish_pyodbc_connection( pyodbc.Connection: database connection object """ - verified_driver = await get_unixodbc_driver_path([driver]) if driver else await get_unixodbc_driver_path(UNIXODBC_DRIVER_PATHS) - if verified_driver is None: - raise FileNotFoundError('Unable to locate unixodbc driver file: libtdsodbc.so') - - conn_string = f'DRIVER={verified_driver};SERVER={host};PORT={port};UID={user};PWD={pwd};TDS_Version=8.0' + verified_driver = None + if OPERATING_SYSTEM == 'Windows': + verified_driver = driver + else: + if driver and not driver.startswith('{'): + verified_driver = await get_unixodbc_driver_path([driver]) + else: + verified_driver = await get_unixodbc_driver_path(UNIXODBC_DRIVER_PATHS) + if verified_driver is None: + raise FileNotFoundError('Unable to locate unixodbc driver file: libtdsodbc.so') + + conn_string = f'DRIVER={verified_driver};SERVER={host};UID={user};PWD={pwd};TDS_Version=8.0' + if port is not None: + conn_string += f';PORT={port}' if database: conn_string += f';DATABASE={database}' diff --git a/aioradio/tests/file_ingestion_test.py b/aioradio/tests/file_ingestion_test.py index 5adb01a..a4a6ce6 100644 --- a/aioradio/tests/file_ingestion_test.py +++ b/aioradio/tests/file_ingestion_test.py @@ -11,8 +11,8 @@ import pytest -from aioradio.file_ingestion import (async_db_wrapper, async_wrapper, delete_ftp_file, - establish_ftp_connection, +from aioradio.file_ingestion import (async_db_wrapper, async_wrapper, + delete_ftp_file, establish_ftp_connection, get_current_datetime_from_timestamp, list_ftp_objects, send_emails_via_mandrill, @@ -188,7 +188,7 @@ def test_async_wrapper(user): if user != 'tim.reichard': pytest.skip('Skip test_async_wrapper since user is not Tim Reichard') - + @async_wrapper async def func(): return 'Hello World' diff --git a/aioradio/tests/pyodbc_test.py b/aioradio/tests/pyodbc_test.py index e051dad..dd7ba3f 100644 --- a/aioradio/tests/pyodbc_test.py +++ b/aioradio/tests/pyodbc_test.py @@ -1,16 +1,15 @@ """pytest pyodbc script.""" -import os +import json import pytest +from aioradio.aws.secrets import get_secret from aioradio.pyodbc import (establish_pyodbc_connection, pyodbc_query_fetchall, pyodbc_query_fetchone) pytestmark = pytest.mark.asyncio -CREDS = {'mssql_host': os.getenv('MSSQL_HOST'), 'mssql_user': os.getenv('MSSQL_USER'), 'mssql_pwd': os.getenv('MSSQL_PW')} - @pytest.mark.xfail async def test_bad_unixodbc_driver(github_action): @@ -19,8 +18,8 @@ async def test_bad_unixodbc_driver(github_action): if github_action: pytest.skip('Skip test_bad_unixodbc_driver when running via Github Action') - driver = '/usr/lib/bogus.so' - await establish_pyodbc_connection(host=CREDS['mssql_host'], user=CREDS['mssql_user'], pwd=CREDS['mssql_pwd'], driver=driver) + creds = json.loads(await get_secret('production/airflowCluster/sqloltp', 'us-east-1')) + await establish_pyodbc_connection(**creds, driver='/usr/lib/bogus.so') async def test_pyodbc_query_fetchone_and_fetchall(github_action): @@ -33,7 +32,8 @@ async def test_pyodbc_query_fetchone_and_fetchall(github_action): if github_action: pytest.skip('Skip test_pyodbc_query_fetchone_and_fetchall when running via Github Action') - conn = await establish_pyodbc_connection(host=CREDS['mssql_host'], user=CREDS['mssql_user'], pwd=CREDS['mssql_pwd']) + creds = json.loads(await get_secret('production/airflowCluster/sqloltp', 'us-east-1')) + conn = await establish_pyodbc_connection(**creds) query = "SELECT EFIemails FROM DataStage.dbo.EESFileuploadAssignments WHERE FICE = '003800' AND FileCategory = 'EnrollmentLens'" row = await pyodbc_query_fetchone(conn=conn, query=query) diff --git a/setup.py b/setup.py index de94482..6c61a25 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ long_description = fileobj.read() setup(name='aioradio', - version='0.10.3', + version='0.10.4', description='Generic asynchronous i/o python utilities for AWS services (SQS, S3, DynamoDB, Secrets Manager), Redis, MSSQL (pyodbc), JIRA and more', long_description=long_description, long_description_content_type="text/markdown",