Frozen Modules:
The custom made firmware mentioned in my previous blog includes a froze module CryptoXo, which is just a simple XOR encryption.
The esp8266 port does not support threading. I want to incorporate concurrent codes in esp8266, so I have modified uasyncio modules from two files; __init__.pt and core.py to one uasyncio.py file, and include a frozen uasyncio module for esp8266.
A micropython running on Unix is a nice thing to have. It is useful for developing and testing scripts. Of course, we can not test scripts using some specific parts of hardware boards' electronics.
Unix port micropython will find modules under the 1) current directory, 2) '$HOME/.micropython/lib' and 3) '/usr/lib/micropython'.
Nevertheless, all frozen modules are only visible in REPL. If you try to import a frozen module in a script executing outside of REPL then you will get an ImportError message.
A shebang micropython, that is to say, a script with #! execute mechanism, will not properly find a frozen module. The only way to import a module is by installing the module under any of the directories listed in sys.path.
Let say we have an executable script test_import_frozen_module.py:
#!/usr/bin/env micropython
import CryptoXo
print(dir(CryptoXo))
Running this script will result in:
$ ./test_import_frozen_module.py
ImportError message; no module named CryptoXo
Similarly with:
$ micropython test_frozen_import.py
ImportError message; no module named CryptoXo
Importing the script in REPL will work.
$ micropython
MicroPython v1.11-631-gb76f0a73b-kaki5 on 2019-12-09; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import test_import_frozen_module
['__class__', '__name__', '__file__', 'sha', 'Crypt']
However, there is a way to use the frozen modules in scripts directly.
Now this will work:
$ micropython -c "import test_import_frozen_module"
['__class__', '__name__', '__file__', 'sha', 'Crypt']
Therefore, we can make a shell command wrapper, say mp:
#!/bin/sh
micropython -c "import $1"
Save it under $HOME/bin and make it executable.
To run any script that uses frozen modules we need just to execute it using our wrapper.
$ mp test_import_frozen_module
['__class__', '__name__', '__file__', 'sha', 'Crypt']
Note that we drop the py extension.
We can also have a wapper script, say exe:
#!/bin/sh
/home/sharil/bin/micropython -c "exec(open('$1').read())"
And running this wrapper,
$ exe test_frozen_import.py
['__class__', '__name__', '__file__', 'sha', 'Crypt']
The result is similar. The mp wrapper uses Python import mechanism, while in exe the python script is read and executed explicitly.
The mp wrapper has the same behavior as main.py with microcontroller boards. We will import our script in /pyboard/main.py startup script.
main.py:
import test_import_frozen_module
The shebang scripts run under the control of OS (example, Linux). All micropython script executing on a microcontroller run under the main application thread. The main thread is REPL.
The custom made firmware mentioned in my previous blog includes a froze module CryptoXo, which is just a simple XOR encryption.
The esp8266 port does not support threading. I want to incorporate concurrent codes in esp8266, so I have modified uasyncio modules from two files; __init__.pt and core.py to one uasyncio.py file, and include a frozen uasyncio module for esp8266.
A micropython running on Unix is a nice thing to have. It is useful for developing and testing scripts. Of course, we can not test scripts using some specific parts of hardware boards' electronics.
Unix port micropython will find modules under the 1) current directory, 2) '$HOME/.micropython/lib' and 3) '/usr/lib/micropython'.
Nevertheless, all frozen modules are only visible in REPL. If you try to import a frozen module in a script executing outside of REPL then you will get an ImportError message.
A shebang micropython, that is to say, a script with #! execute mechanism, will not properly find a frozen module. The only way to import a module is by installing the module under any of the directories listed in sys.path.
Let say we have an executable script test_import_frozen_module.py:
#!/usr/bin/env micropython
import CryptoXo
print(dir(CryptoXo))
Running this script will result in:
$ ./test_import_frozen_module.py
ImportError message; no module named CryptoXo
Similarly with:
$ micropython test_frozen_import.py
ImportError message; no module named CryptoXo
Importing the script in REPL will work.
$ micropython
MicroPython v1.11-631-gb76f0a73b-kaki5 on 2019-12-09; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import test_import_frozen_module
['__class__', '__name__', '__file__', 'sha', 'Crypt']
However, there is a way to use the frozen modules in scripts directly.
Now this will work:
$ micropython -c "import test_import_frozen_module"
['__class__', '__name__', '__file__', 'sha', 'Crypt']
Therefore, we can make a shell command wrapper, say mp:
#!/bin/sh
micropython -c "import $1"
Save it under $HOME/bin and make it executable.
To run any script that uses frozen modules we need just to execute it using our wrapper.
$ mp test_import_frozen_module
['__class__', '__name__', '__file__', 'sha', 'Crypt']
Note that we drop the py extension.
We can also have a wapper script, say exe:
#!/bin/sh
/home/sharil/bin/micropython -c "exec(open('$1').read())"
And running this wrapper,
$ exe test_frozen_import.py
['__class__', '__name__', '__file__', 'sha', 'Crypt']
The result is similar. The mp wrapper uses Python import mechanism, while in exe the python script is read and executed explicitly.
The mp wrapper has the same behavior as main.py with microcontroller boards. We will import our script in /pyboard/main.py startup script.
main.py:
import test_import_frozen_module
The shebang scripts run under the control of OS (example, Linux). All micropython script executing on a microcontroller run under the main application thread. The main thread is REPL.
Comments
Post a Comment