diff options
author | Jim Mussared <jim.mussared@gmail.com> | 2021-02-12 16:11:38 +1100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2021-02-16 17:24:21 +1100 |
commit | 566020034fc64448d33718c575809fcfe8f271db (patch) | |
tree | dbb5959bf9e106216cb90cd2e43e4157710cdf84 | |
parent | 83d23059ef80d0d28d1e0769a721c83665b0095f (diff) |
tools/makemanifest.py: Allow passing option args to include().
This allows customising which features can be enabled in a frozen library.
e.g. `include("path.py", extra_features=True)`
in path.py:
options.defaults(standard_features=True)
if options.standard_features:
# freeze standard modules.
if options.extra_features:
# freeze extra modules.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
-rw-r--r-- | tools/makemanifest.py | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/tools/makemanifest.py b/tools/makemanifest.py index c07a3a6c7..117d1536e 100644 --- a/tools/makemanifest.py +++ b/tools/makemanifest.py @@ -34,13 +34,27 @@ import subprocess # Public functions to be used in the manifest -def include(manifest): +def include(manifest, **kwargs): """Include another manifest. The manifest argument can be a string (filename) or an iterable of strings. Relative paths are resolved with respect to the current manifest file. + + Optional kwargs can be provided which will be available to the + included script via the `options` variable. + + e.g. include("path.py", extra_features=True) + + in path.py: + options.defaults(standard_features=True) + + # freeze minimal modules. + if options.standard_features: + # freeze standard modules. + if options.extra_features: + # freeze extra modules. """ if not isinstance(manifest, str): @@ -53,7 +67,7 @@ def include(manifest): # Applies to includes and input files. prev_cwd = os.getcwd() os.chdir(os.path.dirname(manifest)) - exec(f.read()) + exec(f.read(), globals(), {"options": IncludeOptions(**kwargs)}) os.chdir(prev_cwd) @@ -125,6 +139,18 @@ VARS = {} manifest_list = [] +class IncludeOptions: + def __init__(self, **kwargs): + self._kwargs = kwargs + self._defaults = {} + + def defaults(self, **kwargs): + self._defaults = kwargs + + def __getattr__(self, name): + return self._kwargs.get(name, self._defaults.get(name, None)) + + class FreezeError(Exception): pass |