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

Method _generate_local_env_setup

ms_agent/skill/container.py:771–817  ·  view source on GitHub ↗

Generate Python code to setup environment for local execution.

(self, input_spec: ExecutionInput)

Source from the content-addressed store, hash-verified

769 raise
770
771 def _generate_local_env_setup(self, input_spec: ExecutionInput) -> str:
772 """Generate Python code to setup environment for local execution."""
773 lines = [
774 'import os',
775 'import sys',
776 '',
777 '# Setup environment for local execution',
778 f"os.environ['SKILL_OUTPUT_DIR'] = {repr(str(self.output_dir))}",
779 f"os.environ['SKILL_LOGS_DIR'] = {repr(str(self.logs_dir))}",
780 '',
781 '# Helper functions for I/O paths',
782 'def get_output_path(filename):',
783 ' """Get the full path for an output file. ALL outputs should use this."""',
784 " return os.path.join(os.environ['SKILL_OUTPUT_DIR'], filename)",
785 '',
786 f'SKILL_OUTPUT_DIR = {repr(str(self.output_dir))}',
787 f'SKILL_LOGS_DIR = {repr(str(self.logs_dir))}',
788 ]
789
790 # Add working directory to sys.path for imports and change to it
791 if input_spec.working_dir:
792 work_dir = str(input_spec.working_dir)
793 lines.extend([
794 '',
795 '# Setup working directory for resource access (READ-ONLY for resources)',
796 f'_skill_dir = {repr(work_dir)}',
797 "os.environ['SKILL_DIR'] = _skill_dir",
798 'SKILL_DIR = _skill_dir',
799 'if _skill_dir not in sys.path:',
800 ' sys.path.insert(0, _skill_dir)',
801 'os.chdir(_skill_dir)',
802 ])
803
804 # Add custom env vars
805 for key, value in input_spec.env_vars.items():
806 lines.append(f'os.environ[{repr(key)}] = {repr(value)}')
807
808 # Add args
809 if input_spec.args:
810 lines.append('')
811 lines.append('# Command line arguments')
812 args_str = repr(input_spec.args)
813 lines.append(f'ARGS = {args_str}')
814 lines.append('sys.argv = ["script.py"] + [str(a) for a in ARGS]')
815
816 lines.append('')
817 return '\n'.join(lines)
818
819 def _generate_local_js_env_setup(self, input_spec: ExecutionInput) -> str:
820 """Generate JavaScript code to setup environment for local execution."""

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected