Skip to content

Commit

Permalink
Fix for environments where find_library always fails
Browse files Browse the repository at this point in the history
  • Loading branch information
bryan-hunt committed May 13, 2021
1 parent cca794a commit 9e63088
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/atca_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#define _ATCA_VERSION_H

// Version format yyyymmdd
#define ATCA_LIBRARY_VERSION_DATE "20210423"
#define ATCA_LIBRARY_VERSION_DATE "20210514"
#define ATCA_LIBRARY_VERSION_MAJOR 3
#define ATCA_LIBRARY_VERSION_MINOR 3
#define ATCA_LIBRARY_VERSION_BUILD 1
Expand Down
21 changes: 20 additions & 1 deletion python/cryptoauthlib/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import os.path
import json
import sys
from ctypes import *
from ctypes.util import find_library
from .exceptions import LibraryLoadError
Expand Down Expand Up @@ -69,6 +70,21 @@ def __str__(self):
return str(self.value)


def _force_local_library():
"""
In some environments loading seems to fail under all circumstances unless
brute forcing it.
"""
curr_path = os.path.dirname(__file__)

if sys.platform.startswith('win'):
return os.path.join(curr_path, "cryptoauth.dll")
elif sys.platform.startswith('darwin'):
return os.path.join(curr_path, "libcryptoauth.dylib")
else:
return os.path.join(curr_path, "libcryptoauth.so")


def load_cryptoauthlib(lib=None):
"""
Load CryptoAauthLib into Python environment
Expand All @@ -80,7 +96,10 @@ def load_cryptoauthlib(lib=None):
else:
try:
os.environ['PATH'] = os.path.dirname(__file__) + os.pathsep + os.environ['PATH']
_CRYPTO_LIB = cdll.LoadLibrary(find_library('cryptoauth'))
library_file = find_library('cryptoauth')
if library_file is None:
library_file = _force_local_library()
_CRYPTO_LIB = cdll.LoadLibrary(library_file)
except:
raise LibraryLoadError('Unable to find cryptoauthlib. You may need to reinstall')

Expand Down

0 comments on commit 9e63088

Please sign in to comment.