Skip to content

Commit

Permalink
[ISSUE-139] Revert JLINK_SDK_NAME value to 'libjlinkarm' (#138)
Browse files Browse the repository at this point in the history
On Linux and macOS, the single constant `JLINK_SDK_NAME` has
been used internally by the pylink Library class as both:

1. The library name parameter for the ctypes API find_library()
2. The library file name prefix when searching the filesystem for the JLink DLL in
    `find_library_{linux,darwin}()`.

To fix the call to `find_library(JLINK_SDK_NAME)`, the PR #132:

1. Sets `JLINK_SDK_NAME` to `'jlinkarm'`
2. Moves the file name prefix semantic to the constant `JLINK_SDK_STARTS_WITH`
    with value `'libjlinkarm'`
  • Loading branch information
dottspina committed Aug 5, 2022
1 parent f24fea5 commit a78e0e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
26 changes: 17 additions & 9 deletions pylink/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,21 @@ class Library(object):
'JLINK_SetFlashProgProgressCallback'
]

# Linux/MacOS: The JLink library name without any prefix like lib,
# suffix like .so, .dylib or version number.
JLINK_SDK_NAME = 'jlinkarm'
# On Linux and macOS, represents the library file name prefix
# (the part just before .so or .dylib).
#
# This is the value appropriate when calling the find_library_{linux,darwin}()
# functions below.
#
# Note: this "constant" is also used by downstream projects,
# and should therefore be considered public (frozen) API.
JLINK_SDK_NAME = 'libjlinkarm'

# Linux/MacOS: The library file name will start with 'libjlinkarm'
# Used by Library.find_library_{linux,darwin}()
JLINK_SDK_STARTS_WITH = 'libjlinkarm'
# Linux/MacOS: The JLink shared object name, without any prefix like lib,
# suffix like .so, .dylib or version number.
#
# This is the value appropriate when invoking the ctypes API find_libary().
JLINK_SDK_OBJECT = 'jlinkarm'

# Windows: these are suitable for both the ctypes find_library() API,
# and for the directory scanning done in Library.find_library_windows()
Expand Down Expand Up @@ -176,7 +184,7 @@ def find_library_linux(cls):
The paths to the J-Link library files in the order that they are
found.
"""
dll = Library.JLINK_SDK_STARTS_WITH
dll = Library.JLINK_SDK_NAME
root = os.path.join('/', 'opt', 'SEGGER')

for (directory_name, subdirs, files) in os.walk(root):
Expand Down Expand Up @@ -225,7 +233,7 @@ def find_library_darwin(cls):
Returns:
The path to the J-Link library files in the order they are found.
"""
dll = Library.JLINK_SDK_STARTS_WITH
dll = Library.JLINK_SDK_NAME
root = os.path.join('/', 'Applications', 'SEGGER')
if not os.path.isdir(root):
return
Expand Down Expand Up @@ -272,7 +280,7 @@ def __init__(self, dllpath=None):
if self._windows or self._cygwin:
self._sdk = self.get_appropriate_windows_sdk_name()
else:
self._sdk = self.JLINK_SDK_NAME
self._sdk = self.JLINK_SDK_OBJECT

if dllpath is not None:
self.load(dllpath)
Expand Down
24 changes: 12 additions & 12 deletions tests/unit/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def test_initialize_default(self, mock_load_library, mock_find_library, mock_ope
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_OBJECT)
mock_open.assert_called_with(self.lib_path, 'rb')
mock_load_library.assert_called_once()

Expand Down Expand Up @@ -182,7 +182,7 @@ def test_initialiaze_no(self, mock_isdir, mock_load_library, mock_find_library,
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_OBJECT)
self.assertEqual(1, mock_find_library.call_count)
self.assertEqual(0, mock_load_library.call_count)

Expand Down Expand Up @@ -317,7 +317,7 @@ def test_load(self, mock_load_library, mock_find_library, mock_open):
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_OBJECT)
self.assertEqual(1, mock_find_library.call_count)

mock_open.assert_called_with(self.lib_path, 'rb')
Expand Down Expand Up @@ -431,7 +431,7 @@ def test_dll_getter(self, mock_load_library, mock_find_library, mock_open):
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_OBJECT)
self.assertEqual(1, mock_find_library.call_count)

mock_open.assert_called_with(self.lib_path, 'rb')
Expand Down Expand Up @@ -469,7 +469,7 @@ def test_darwin_4_98_e(self, mock_os, mock_load_library, mock_find_library, mock
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_OBJECT)
self.assertEqual(1, mock_find_library.call_count)
self.assertEqual(1, mock_load_library.call_count)

Expand Down Expand Up @@ -504,7 +504,7 @@ def test_darwin_5_0_0(self, mock_os, mock_load_library, mock_find_library, mock_
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_OBJECT)
self.assertEqual(1, mock_find_library.call_count)
self.assertEqual(1, mock_load_library.call_count)

Expand Down Expand Up @@ -538,7 +538,7 @@ def test_darwin_6_0_0(self, mock_os, mock_load_library, mock_find_library, mock_
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_OBJECT)
self.assertEqual(1, mock_find_library.call_count)
self.assertEqual(1, mock_load_library.call_count)

Expand Down Expand Up @@ -572,7 +572,7 @@ def test_darwin_empty(self, mock_os, mock_load_library, mock_find_library, mock_
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_OBJECT)
self.assertEqual(1, mock_find_library.call_count)
self.assertEqual(0, mock_load_library.call_count)

Expand Down Expand Up @@ -946,7 +946,7 @@ def test_linux_empty(self, mock_os, mock_load_library, mock_find_library, mock_o
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_OBJECT)
self.assertEqual(1, mock_find_library.call_count)
self.assertEqual(0, mock_load_library.call_count)

Expand Down Expand Up @@ -983,7 +983,7 @@ def test_linux_glibc_unavailable(self, mock_load_library, mock_dlinfo_ctr, mock_
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_OBJECT)
# JLinkarmDlInfo has not been instantiated.
self.assertEquals(0, mock_dlinfo_ctr.call_count)
# Fallback to "search by file name" has succeeded.
Expand Down Expand Up @@ -1027,7 +1027,7 @@ def test_linux_dl_unavailable(self, mock_load_library, mock_find_library, mock_l
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_any_call(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_any_call(library.Library.JLINK_SDK_OBJECT)
mock_find_library.assert_any_call('dl')
self.assertEquals(2, mock_find_library.call_count)
# Called once in JLinkarmDlInfo and once in Library.
Expand Down Expand Up @@ -1068,7 +1068,7 @@ def test_linux_dl_oserror(self, mock_load_library, mock_find_library, mock_libc_
lib = library.Library()
lib.unload = mock.Mock()

mock_find_library.assert_any_call(library.Library.JLINK_SDK_NAME)
mock_find_library.assert_any_call(library.Library.JLINK_SDK_OBJECT)
mock_find_library.assert_any_call('dl')
self.assertEquals(2, mock_find_library.call_count)
self.assertEquals(2, mock_load_library.call_count)
Expand Down

0 comments on commit a78e0e2

Please sign in to comment.