Skip to content

Commit

Permalink
Fix #113: JLink script makes some args optional (#115)
Browse files Browse the repository at this point in the history
* Fix #113: JLink script makes some args optional

* use functools to DRY up get_args

Co-authored-by: Scott Dixon <dixonsco@amazon.com>
  • Loading branch information
karlin and thirtytwobits committed Feb 10, 2020
1 parent 33d37e7 commit 5da41cf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/nanaimo/instruments/jlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import nanaimo
import nanaimo.fixtures
import functools


class ProgramUploader(nanaimo.fixtures.SubprocessFixture):
Expand Down Expand Up @@ -83,7 +84,7 @@ def on_visit_test_arguments(cls, arguments: nanaimo.Arguments) -> None:

def on_construct_command(self, arguments: nanaimo.Namespace, inout_artifacts: nanaimo.Artifacts) -> str:
"""
Construct a command to upload (eke "flash") a firmware to a target device using Segger's JLink
Construct a command to upload (aka "flash") a firmware to a target device using Segger's JLink
Commander program with the assumption that a Segger debug probe like the JLink is attached to the
system.
+----------------+---------------------------------------+--------------------------------------------------+
Expand All @@ -104,6 +105,9 @@ def on_construct_command(self, arguments: nanaimo.Namespace, inout_artifacts: na
tmpfile = tempfile.NamedTemporaryFile(mode='w')
setattr(inout_artifacts, 'tmpfile', tmpfile)

# without a script, other args are required:
get_arg_func = functools.partial(self.get_arg_covariant_or_fail, arguments)

if script_file_path is None:
# keep around for as long as the command exists.
setattr(inout_artifacts, 'scriptfile', None)
Expand All @@ -113,14 +117,15 @@ def on_construct_command(self, arguments: nanaimo.Namespace, inout_artifacts: na
setattr(inout_artifacts, 'scriptfile', script_file_path)
with open(script_file_path, 'r') as user_script_file:
template = user_script_file.read()
get_arg_func = functools.partial(self.get_arg_covariant, arguments) # don't fail fixture on missing args

# poor man's templating
expanded_script_file = template.format(
hexfile=self.get_arg_covariant_or_fail(arguments, 'hexfile'),
device=self.get_arg_covariant_or_fail(arguments, 'device'),
speed=self.get_arg_covariant_or_fail(arguments, 'interface-speed-khz'),
serial_interface=self.get_arg_covariant_or_fail(arguments, 'serial-interface'),
reset_delay_millis=str(self.get_arg_covariant_or_fail(arguments, 'reset-delay-millis'))
hexfile=get_arg_func('hexfile'),
device=get_arg_func('device'),
speed=get_arg_func('interface-speed-khz'),
serial_interface=get_arg_func('serial-interface'),
reset_delay_millis=str(get_arg_func('reset-delay-millis'))
)

with open(tmpfile.name, 'w') as tempfile_handle:
Expand Down
3 changes: 2 additions & 1 deletion test/test_instrument_jlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async def test_jlink_upload(use_internal_template: bool,

if not use_internal_template:
test_args['jlink_up_script'] = str(test_jlink_template)
test_args['jlink_up_device'] = None # fails unless args are optional with script

artifacts = assert_success(await nanaimo_jlink_upload.gather(**test_args))

Expand All @@ -48,7 +49,7 @@ async def test_jlink_upload(use_internal_template: bool,
hexfile_in_script = None # type: typing.Optional[str]
with open(scriptfile, 'r') as scriptfile_handle:
for line in scriptfile_handle:
matchobj = re.search(r'^loadfile\s+((?:/|\w)+\.hex)$', line)
matchobj = re.search(r'^loadfile\s+((?:/|\w)+(?:/|[\w\. -])*\.hex)$', line)
if matchobj:
hexfile_in_script = matchobj.group(1)
print(line, end='')
Expand Down

0 comments on commit 5da41cf

Please sign in to comment.