MCPcopy Create free account
hub / github.com/modelscope/ms-agent / _generate_env_setup

Method _generate_env_setup

ms_agent/skill/container.py:1035–1071  ·  view source on GitHub ↗

Generate Python code to setup environment variables and paths.

(self, input_spec: ExecutionInput,
                            sandbox_files: Dict[str, str])

Source from the content-addressed store, hash-verified

1033 return output
1034
1035 def _generate_env_setup(self, input_spec: ExecutionInput,
1036 sandbox_files: Dict[str, str]) -> str:
1037 """Generate Python code to setup environment variables and paths."""
1038 sandbox_logs_dir = f'{self.SANDBOX_ROOT}/logs'
1039 lines = [
1040 'import os',
1041 'import sys',
1042 '',
1043 '# Setup environment',
1044 f"os.environ['SKILL_OUTPUT_DIR'] = '{self.SANDBOX_OUTPUT_DIR}'",
1045 f"os.environ['SKILL_LOGS_DIR'] = '{sandbox_logs_dir}'",
1046 '',
1047 '# Helper functions for I/O paths',
1048 'def get_output_path(filename):',
1049 ' """Get the full path for an output file. ALL outputs should use this."""',
1050 " return os.path.join(os.environ['SKILL_OUTPUT_DIR'], filename)",
1051 '',
1052 f"SKILL_OUTPUT_DIR = '{self.SANDBOX_OUTPUT_DIR}'",
1053 f"SKILL_LOGS_DIR = '{sandbox_logs_dir}'",
1054 ]
1055
1056 # Add custom env vars
1057 for key, value in input_spec.env_vars.items():
1058 # Sanitize value to prevent injection
1059 safe_value = value.replace("'", "\\'")
1060 lines.append(f"os.environ['{key}'] = '{safe_value}'")
1061
1062 # Add args
1063 if input_spec.args:
1064 lines.append('')
1065 lines.append('# Command line arguments')
1066 args_str = repr(input_spec.args)
1067 lines.append(f'ARGS = {args_str}')
1068 lines.append('sys.argv = ["script.py"] + [str(a) for a in ARGS]')
1069
1070 lines.append('')
1071 return '\n'.join(lines)
1072
1073 def execute_python_function(
1074 self,

Callers 2

execute_python_scriptMethod · 0.95
execute_python_codeMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected