From e4761312f77e3ea23588a3c211f5e44cbd7cac15 Mon Sep 17 00:00:00 2001 From: Yoann Congal Date: Sat, 15 Mar 2025 00:52:13 +0100 Subject: [PATCH] bitbake: tinfoil: Refactor temporary data tracking in a context manager A new context manager Tinfoil._data_tracked_if_enabled() is introduced to replace the following structure: if self.tracking: self.run_command('enableDataTracking') # Code that need data tracking if self.tracking: self.run_command('disableDataTracking') (Bitbake rev: 0fea4555d2143c6b23a79d3d5cf791103a68141b) Signed-off-by: Yoann Congal Signed-off-by: Richard Purdie --- bitbake/lib/bb/tinfoil.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/bitbake/lib/bb/tinfoil.py b/bitbake/lib/bb/tinfoil.py index 13b05cec2d..60359a1a38 100644 --- a/bitbake/lib/bb/tinfoil.py +++ b/bitbake/lib/bb/tinfoil.py @@ -15,6 +15,7 @@ import atexit import re from collections import OrderedDict, defaultdict from functools import partial +from contextlib import contextmanager import bb.cache import bb.cooker @@ -641,6 +642,22 @@ class Tinfoil: fn = self.get_recipe_file(pn) return self.parse_recipe_file(fn) + @contextmanager + def _data_tracked_if_enabled(self): + """ + A context manager to enable data tracking for a code segment if data + tracking was enabled for this tinfoil instance. + """ + if self.tracking: + # Enable history tracking just for the operation + self.run_command('enableDataTracking') + + # Here goes the operation with the optional data tracking + yield + + if self.tracking: + self.run_command('disableDataTracking') + def finalizeData(self): """ Run anonymous functions and expand keys @@ -659,10 +676,7 @@ class Tinfoil: appendlist: optional list of bbappend files to apply, if you want to filter them """ - if self.tracking: - # Enable history tracking just for the parse operation - self.run_command('enableDataTracking') - try: + with self._data_tracked_if_enabled(): if appends and appendlist == []: appends = False if config_data: @@ -674,9 +688,6 @@ class Tinfoil: return self._reconvert_type(dscon, 'DataStoreConnectionHandle') else: return None - finally: - if self.tracking: - self.run_command('disableDataTracking') def build_file(self, buildfile, task, internal=True): """