[SOLVED]how to best bundle .mpy and raw Python source

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

[SOLVED]how to best bundle .mpy and raw Python source

Post by jickster » Thu Oct 26, 2017 6:26 pm

For my product, we want to bundle the user-created .mpy and raw Python .py source so that if the hw module is passed around, a new user can suck out the existing source and have a perfect picture of what's running on the module.

What's the most efficient and robust method to do this?

Here are my thoughts:

Take the .py file and create a function inside it called def get_code() and return the entire pasted contents (minus the get_code() function)

Code: Select all

def get_code(): # would be placed at end of file
    code = "the entire content of the file above def_code()\n
    		blah blah blah" 
    return code
I could also compress the contents of code because currently, uzlib can uncompress (though not compress) so I could call get_code() and get the actual uncompressed code AND because it's an actual part of the module in question, there's built-in "traceability"

Code: Select all

def get_code(): # would be placed at end of file
    code = "the entire content of the file above def_code()\n
    		but compressed" 
    return uzlib.decompress(code)
Last edited by jickster on Tue Nov 21, 2017 9:14 pm, edited 1 time in total.

SpotlightKid
Posts: 380
Joined: Wed Apr 08, 2015 5:19 am

Re: how to best bundle .mpy and raw Python source

Post by SpotlightKid » Tue Nov 21, 2017 10:02 am

I would instead provide SHA-sums for the mpy-Files on the module and the raw *.py sources through another channel, so that the user can check, that the *.mpy files on the module were actually produced from the the same *.py files.

stijn
Posts: 362
Joined: Thu Apr 24, 2014 9:13 am

Re: how to best bundle .mpy and raw Python source

Post by stijn » Tue Nov 21, 2017 12:08 pm

Yeah having to acquire code by running Python to return the code as a string is quite a convoluted way for something which can be achieved in many, many other ways, most of them easier. Also because you have to get the source in there in the first place. And what if the .mpy has a problem and the code doesn't run? Then the user cannot even get to the actual source code. I wouldn't go there.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: how to best bundle .mpy and raw Python source

Post by jickster » Tue Nov 21, 2017 9:14 pm

stijn wrote:
Tue Nov 21, 2017 12:08 pm
Yeah having to acquire code by running Python to return the code as a string is quite a convoluted way for something which can be achieved in many, many other ways, most of them easier. Also because you have to get the source in there in the first place. And what if the .mpy has a problem and the code doesn't run? Then the user cannot even get to the actual source code. I wouldn't go there.
SpotlightKid wrote:
Tue Nov 21, 2017 10:02 am
I would instead provide SHA-sums for the mpy-Files on the module and the raw *.py sources through another channel, so that the user can check, that the *.mpy files on the module were actually produced from the the same *.py files.
I know it's convoluted; I wanted to see if there were better ideas than the "two-channel SHA-sums". I guess that's the best one.
I didn't think what would happen if the .mpy couldn't execute.

Thanks :)

Post Reply