4141import java .util .concurrent .ConcurrentHashMap ;
4242
4343import javax .script .Bindings ;
44- import javax .script .ScriptContext ;
4544import javax .script .ScriptEngine ;
4645import javax .script .ScriptException ;
4746
8584 */
8685public class ApposePythonScriptEngine extends AbstractScriptEngine {
8786
88- /** Cache of built environments, keyed by resolved env file absolute path. */
89- private static final Map <String , Environment > ENV_CACHE =
90- new ConcurrentHashMap <>();
91-
9287 @ Parameter
9388 private ConvertService convertService ;
9489
@@ -235,9 +230,8 @@ public Bindings createBindings() {
235230 // -- Helper methods --
236231
237232 /**
238- * Lazily builds (or fetches from cache) the Appose {@link Environment}
239- * described by the {@code env} and {@code scheme} attributes of the script's
240- * {@code #@script} directive.
233+ * Lazily builds the Appose {@link Environment} described by the {@code env}
234+ * and {@code scheme} attributes of the script's {@code #@script} directive.
241235 */
242236 private Environment buildEnvironment (final ScriptInfo info )
243237 throws ScriptException
@@ -247,46 +241,21 @@ private Environment buildEnvironment(final ScriptInfo info)
247241 if (envRef == null ) return null ;
248242
249243 final File envFile = resolveEnvFile (envRef , info .getPath ());
250- final String cacheKey = envFile .getAbsolutePath ();
251244 final String envName = sanitizeEnvName (info .getPath ());
252245
246+ log .info ("Building Appose environment: " + envName );
253247 try {
254- return ENV_CACHE .computeIfAbsent (cacheKey , key -> {
255- log .info ("Building Appose environment: " + key );
256- try {
257- return selectBuilder (key , info .get ("scheme" )).name (envName ).build ();
258- }
259- catch (final BuildException e ) {
260- throw new RuntimeException (
261- "Failed to build Appose environment: " + e .getMessage (), e );
262- }
263- });
264- }
265- catch (final RuntimeException e ) {
266- if (e .getCause () instanceof BuildException ) {
267- throw new ScriptException (e .getMessage ());
268- }
269- throw e ;
270- }
271- }
248+ String scheme = info .get ("scheme" );
249+ Builder <?> builder = scheme == null ?
250+ Appose .file (envFile ) : Appose .file (envFile ).scheme (scheme );
272251
273- /**
274- * Selects the appropriate Appose builder based on the {@code scheme} hint.
275- * Falls back to auto-detection ({@link Appose#file}) if unspecified or
276- * unrecognized.
277- */
278- private Builder <?> selectBuilder (final String envFilePath ,
279- final String scheme ) throws BuildException
280- {
281- if (scheme == null || scheme .isEmpty ()) return Appose .file (envFilePath );
282- switch (scheme .toLowerCase ()) {
283- case "pixi.toml" : return Appose .pixi (envFilePath );
284- case "environment.yml" : return Appose .mamba (envFilePath );
285- case "requirements.txt" :
286- case "pyproject.toml" : return Appose .uv (envFilePath );
287- default :
288- log .warn ("Unrecognized scheme '" + scheme + "'; using auto-detection." );
289- return Appose .file (envFilePath );
252+ return builder .name (envName ).build ();
253+ }
254+ catch (final BuildException e ) {
255+ ScriptException se = new ScriptException (
256+ "Failed to build Appose environment" );
257+ se .initCause (e );
258+ throw se ;
290259 }
291260 }
292261
@@ -308,9 +277,9 @@ private static File resolveEnvFile(final String envRef,
308277 * all non-alphanumeric characters (except {@code -}) with {@code _}.
309278 */
310279 private static String sanitizeEnvName (final String scriptPath ) {
311- if (scriptPath == null ) return "appose-python" ;
280+ if (scriptPath == null ) return "scripting- appose-python" ;
312281 return new File (scriptPath ).getAbsolutePath ()
313- .replaceAll ("[^a-zA-Z0-9_- ]" , "_ " );
282+ .replaceAll ("[^a-zA-Z0-9_]" , "- " );
314283 }
315284
316285 /**
0 commit comments