Skip to content

Commit

Permalink
feat(tools): add global build context
Browse files Browse the repository at this point in the history
Introduces get_build_context() helper, which allows to get build context, e.g.
project description, at places where this info is not available. The
build context is set within ensure_build_directory.

Signed-off-by: Frantisek Hrbata <[email protected]>
  • Loading branch information
fhrbata committed Aug 5, 2023
1 parent 7d87b6f commit ed35c3a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tools/idf_py_actions/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,37 @@
SHELL_COMPLETE_RUN = SHELL_COMPLETE_VAR in os.environ


# The ctx dict "abuses" how python evaluates default parameter values.
# https://docs.python.org/3/reference/compound_stmts.html#function-definitions
# Default parameter values are evaluated from left to right
# when the function definition is executed
def get_build_context(ctx: Dict={}) -> Dict:
"""
The build context is set in the ensure_build_directory function. It can be used
in modules or other code, which don't have direct access to such information.
It returns dictionary with the following keys:
'proj_desc' - loaded project_description.json file
Please make sure that ensure_build_directory was called otherwise the build
context dictionary will be empty. Also note that it might not be thread-safe to
modify the returned dictionary. It should be considered read-only.
"""
return ctx


def _set_build_context(args: 'PropertyDict') -> None:
# private helper to set global build context from ensure_build_directory
ctx = get_build_context()

proj_desc_fn = f'{args.build_dir}/project_description.json'
try:
with open(proj_desc_fn, 'r') as f:
ctx['proj_desc'] = json.load(f)
except (OSError, ValueError) as e:
raise FatalError(f'Cannot load {proj_desc_fn}: {e}')


def executable_exists(args: List) -> bool:
try:
subprocess.check_output(args)
Expand Down Expand Up @@ -567,6 +598,9 @@ def ensure_build_directory(args: 'PropertyDict', prog_name: str, always_run_cmak
except KeyError:
pass

# set global build context
_set_build_context(args)


def merge_action_lists(*action_lists: Dict) -> Dict:
merged_actions: Dict = {
Expand Down

0 comments on commit ed35c3a

Please sign in to comment.