mpfshell: how to execute a file

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Oberoid
Posts: 6
Joined: Sun Nov 12, 2017 11:26 am

mpfshell: how to execute a file

Post by Oberoid » Sun Nov 12, 2017 11:36 am

I would like to run a file on the esp with mpfshell.
There is a command exec in mpfshell but that does not work and is not documented.
Ampy has a run - function, but I like to use it with a websocket.

ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

Re: mpfshell: how to execute a file

Post by ttmetro » Tue Nov 21, 2017 11:50 pm

Not the answer, but basically the same question.

rshell's remote copy command runs the following code on the remote:

Code: Select all

def recv_file_from_host(src_file, dst_filename, filesize, dst_mode='wb'):
    """Function which runs on the pyboard. Matches up with send_file_to_remote."""
    import sys
    import ubinascii
    if HAS_BUFFER:
        try:
            import pyb
            usb = pyb.USB_VCP()
        except:
            try:
                import machine
                usb = machine.USB_VCP()
            except:
                usb = None
        if usb and usb.isconnected():
            # We don't want 0x03 bytes in the data to be interpreted as a Control-C
            # This gets reset each time the REPL runs a line, so we don't need to
            # worry about resetting it ourselves
            usb.setinterrupt(-1)
    try:
        with open(dst_filename, dst_mode) as dst_file:
            bytes_remaining = filesize
            if not HAS_BUFFER:
                bytes_remaining *= 2  # hexlify makes each byte into 2
            buf_size = BUFFER_SIZE
            write_buf = bytearray(buf_size)
            read_buf = bytearray(buf_size)
            while bytes_remaining > 0:
                read_size = min(bytes_remaining, buf_size)
                buf_remaining = read_size
                buf_index = 0
                while buf_remaining > 0:
                    if HAS_BUFFER:
                        bytes_read = sys.stdin.buffer.readinto(read_buf, bytes_remaining)
                    else:
                        bytes_read = sys.stdin.readinto(read_buf, bytes_remaining)
                    if bytes_read > 0:
                        write_buf[buf_index:bytes_read] = read_buf[0:bytes_read]
                        buf_index += bytes_read
                        buf_remaining -= bytes_read
                if HAS_BUFFER:
                    dst_file.write(write_buf[0:read_size])
                else:
                    dst_file.write(ubinascii.unhexlify(write_buf[0:read_size]))
                # Send back an ack as a form of flow control
                sys.stdout.write('\x06')
                bytes_remaining -= read_size
        return True
    except:
        return False
with the effect of saving everything received from a matching

Code: Select all

send_file_to_remote
method running on the host. Could this function be modified: instead of saving the received content to dst_filename it instead passes the received data to exec and returns the result of the execution?

Looks like a great potential extension of rshell - unfortunately I don't know enough Python to make the modifications without some hints.
Bernhard Boser

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: mpfshell: how to execute a file

Post by dhylands » Wed Nov 22, 2017 2:47 pm

rshell could definitely be modified to exec a file after copying.

I normally just use the repl from within rshell.

You can also use pyboard.py to execute a file from the host on the pyboard. This method doesn't copy it to the filesystem, just loads it into RAM and executes it.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: mpfshell: how to execute a file

Post by pythoncoder » Fri Nov 24, 2017 8:07 am

The tests directory in the source tree illustrates remote execution and capturing results. The subdirectory pyb contains tests intended to run on an attached Pyboard.
Peter Hinch
Index to my micropython libraries.

ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

Re: mpfshell: how to execute a file

Post by ttmetro » Fri Dec 15, 2017 11:46 pm

I added a run command to rshell at https://github.com/bboser/rshell.
Bernhard Boser

Post Reply