11import asyncio
22import os
33import uuid
4- from typing import Callable , Optional
4+ from typing import Callable , Dict , Optional
55from uuid import uuid4
66
77import pytest
@@ -101,14 +101,38 @@ def _build(
101101 skip_cache : bool = False ,
102102 on_build_logs : Optional [Callable [[LogEntry ], None ]] = None ,
103103 ):
104- return Template .build (
105- template ,
106- name or f"e2b-test-{ uuid4 ()} " ,
107- cpu_count = 1 ,
108- memory_mb = 1024 ,
109- skip_cache = skip_cache ,
110- on_build_logs = on_build_logs ,
111- )
104+ build_name = name or f"e2b-test-{ uuid4 ()} "
105+ build_info : Dict [str , Optional [str ]] = {"template_id" : None , "build_id" : None }
106+
107+ def capture_logs (log : LogEntry ):
108+ import re
109+
110+ if "Template created with ID:" in log .message :
111+ match = re .search (
112+ r"Template created with ID: ([^,]+), Build ID: (.+)" , log .message
113+ )
114+ if match :
115+ build_info ["template_id" ] = match .group (1 )
116+ build_info ["build_id" ] = match .group (2 )
117+ if on_build_logs :
118+ on_build_logs (log )
119+
120+ try :
121+ return Template .build (
122+ template ,
123+ build_name ,
124+ cpu_count = 1 ,
125+ memory_mb = 1024 ,
126+ skip_cache = skip_cache ,
127+ on_build_logs = capture_logs ,
128+ )
129+ except Exception as e :
130+ print (
131+ f"\n [BUILD FAILED] name={ build_name } , "
132+ f"template_id={ build_info ['template_id' ]} , "
133+ f"build_id={ build_info ['build_id' ]} , error={ e } "
134+ )
135+ raise
112136
113137 return _build
114138
@@ -121,14 +145,38 @@ async def _async_build(
121145 skip_cache : bool = False ,
122146 on_build_logs : Optional [Callable [[LogEntry ], None ]] = None ,
123147 ):
124- return await AsyncTemplate .build (
125- template ,
126- name or f"e2b-test-{ uuid4 ()} " ,
127- cpu_count = 1 ,
128- memory_mb = 1024 ,
129- skip_cache = skip_cache ,
130- on_build_logs = on_build_logs ,
131- )
148+ build_name = name or f"e2b-test-{ uuid4 ()} "
149+ build_info : Dict [str , Optional [str ]] = {"template_id" : None , "build_id" : None }
150+
151+ def capture_logs (log : LogEntry ):
152+ import re
153+
154+ if "Template created with ID:" in log .message :
155+ match = re .search (
156+ r"Template created with ID: ([^,]+), Build ID: (.+)" , log .message
157+ )
158+ if match :
159+ build_info ["template_id" ] = match .group (1 )
160+ build_info ["build_id" ] = match .group (2 )
161+ if on_build_logs :
162+ on_build_logs (log )
163+
164+ try :
165+ return await AsyncTemplate .build (
166+ template ,
167+ build_name ,
168+ cpu_count = 1 ,
169+ memory_mb = 1024 ,
170+ skip_cache = skip_cache ,
171+ on_build_logs = capture_logs ,
172+ )
173+ except Exception as e :
174+ print (
175+ f"\n [BUILD FAILED] name={ build_name } , "
176+ f"template_id={ build_info ['template_id' ]} , "
177+ f"build_id={ build_info ['build_id' ]} , error={ e } "
178+ )
179+ raise
132180
133181 return _async_build
134182
0 commit comments