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>
This commit is contained in:
Jim Mussared 2021-02-12 16:11:38 +11:00 committed by Damien George
parent 83d23059ef
commit 566020034f
1 changed files with 28 additions and 2 deletions

View File

@ -34,13 +34,27 @@ import subprocess
# Public functions to be used in the manifest # Public functions to be used in the manifest
def include(manifest): def include(manifest, **kwargs):
"""Include another manifest. """Include another manifest.
The manifest argument can be a string (filename) or an iterable of The manifest argument can be a string (filename) or an iterable of
strings. strings.
Relative paths are resolved with respect to the current manifest file. 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): if not isinstance(manifest, str):
@ -53,7 +67,7 @@ def include(manifest):
# Applies to includes and input files. # Applies to includes and input files.
prev_cwd = os.getcwd() prev_cwd = os.getcwd()
os.chdir(os.path.dirname(manifest)) os.chdir(os.path.dirname(manifest))
exec(f.read()) exec(f.read(), globals(), {"options": IncludeOptions(**kwargs)})
os.chdir(prev_cwd) os.chdir(prev_cwd)
@ -125,6 +139,18 @@ VARS = {}
manifest_list = [] 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): class FreezeError(Exception):
pass pass