From a78746756978ce217b4af81cfe72090e4b69dc6b Mon Sep 17 00:00:00 2001 From: Tom Soulanille Date: Thu, 30 Jul 2015 21:25:43 -0700 Subject: [PATCH] tools/pyboard: Add -c argument to run a program passed as a string. --- tools/pyboard.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tools/pyboard.py b/tools/pyboard.py index bef9419ca8..413ec1329c 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -253,17 +253,16 @@ def main(): cmd_parser.add_argument('-b', '--baudrate', default=115200, help='the baud rate of the serial device') cmd_parser.add_argument('-u', '--user', default='micro', help='the telnet login username') cmd_parser.add_argument('-p', '--password', default='python', help='the telnet login password') + cmd_parser.add_argument('-c', '--command', help='program passed in as string') cmd_parser.add_argument('--follow', action='store_true', help='follow the output after running the scripts [default if no scripts given]') cmd_parser.add_argument('files', nargs='*', help='input files') args = cmd_parser.parse_args() - for filename in args.files: + def execbuffer(buf): try: pyb = Pyboard(args.device, args.baudrate, args.user, args.password) pyb.enter_raw_repl() - with open(filename, 'rb') as f: - pyfile = f.read() - ret, ret_err = pyb.exec_raw(pyfile, timeout=None, data_consumer=stdout_write_bytes) + ret, ret_err = pyb.exec_raw(buf, timeout=None, data_consumer=stdout_write_bytes) pyb.exit_raw_repl() pyb.close() except PyboardError as er: @@ -275,7 +274,15 @@ def main(): stdout_write_bytes(ret_err) sys.exit(1) - if args.follow or len(args.files) == 0: + if args.command is not None: + execbuffer(bytes(args.command, 'utf-8')) + + for filename in args.files: + with open(filename, 'rb') as f: + pyfile = f.read() + execbuffer(pyfile) + + if args.follow or (args.command is None and len(args.files) == 0): try: pyb = Pyboard(args.device, args.baudrate, args.user, args.password) ret, ret_err = pyb.follow(timeout=None, data_consumer=stdout_write_bytes)