This is an automated email from the ASF dual-hosted git repository.

akitouni pushed a commit to branch abderrahim/digest-environment
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit 698b9809bcb3a92c3ede42e8fa71d42a9763d1b5
Author: Abderrahim Kitouni <[email protected]>
AuthorDate: Tue Jun 17 17:02:10 2025 +0100

    buildelement: Add the digest-environment config property
    
    This allows setting an environment variable inside the sandbox to the CAS
    digest of one or more dependencies.
    
    Co-authored by: Adrien Plazas <[email protected]>
---
 src/buildstream/buildelement.py | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/buildstream/buildelement.py b/src/buildstream/buildelement.py
index 40355f5cf..06ea67f37 100644
--- a/src/buildstream/buildelement.py
+++ b/src/buildstream/buildelement.py
@@ -219,6 +219,7 @@ class BuildElement(Element):
     def configure_dependencies(self, dependencies):
 
         self.__layout = {}  # pylint: disable=attribute-defined-outside-init
+        self.__digest_environment = {}  # pylint: 
disable=attribute-defined-outside-init
 
         # FIXME: Currently this forcefully validates configurations
         #        for all BuildElement subclasses so they are unable to
@@ -227,9 +228,18 @@ class BuildElement(Element):
         for dep in dependencies:
             # Determine the location to stage each element, default is "/"
             location = "/"
+
             if dep.config:
-                dep.config.validate_keys(["location"])
-                location = dep.config.get_str("location")
+                dep.config.validate_keys(["digest-environment", "location"])
+
+                location = dep.config.get_str("location", "/")
+
+                digest_var_name = dep.config.get_str("digest-environment", 
None)
+
+                if digest_var_name is not None:
+                    element_list = 
self.__digest_environment.setdefault(digest_var_name, [])
+                    element_list.append(dep.element)
+
             try:
                 element_list = self.__layout[location]
             except KeyError:
@@ -285,10 +295,17 @@ class BuildElement(Element):
             command_dir = build_root
         sandbox.set_work_directory(command_dir)
 
+    def stage(self, sandbox):
         # Setup environment
-        sandbox.set_environment(self.get_environment())
+        env = self.get_environment()
 
-    def stage(self, sandbox):
+        for digest_variable, element_list in self.__digest_environment.items():
+            subsandbox = sandbox.create_subsandbox()
+            self.stage_dependency_artifacts(subsandbox, element_list)
+            digest = subsandbox.get_virtual_directory()._get_digest()
+            env[digest_variable] = "{}/{}".format(digest.hash, 
digest.size_bytes)
+
+        sandbox.set_environment(env)
 
         # First stage it all
         #

Reply via email to