Download code_interpreters/languages/shell.py from aiflows/InterpreterFlowModule: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/aiflows/InterpreterFlowModule/resolve/f5ba9f9afc5a4b5f9cc00ef80230b60bcc8bd9cc/code_interpreters/languages/shell.py
- Command line
-
hf download hf://aiflows/InterpreterFlowModule@f5ba9f9afc5a4b5f9cc00ef80230b60bcc8bd9cc/code_interpreters/languages/shell.py
-
curl -L -o shell.py https://huggingface.co/aiflows/InterpreterFlowModule/resolve/f5ba9f9afc5a4b5f9cc00ef80230b60bcc8bd9cc/code_interpreters/languages/shell.py
1.61 kB
| import platform | |
| from ..subprocess_code_interpreter import SubprocessCodeInterpreter | |
| import os | |
| class Shell(SubprocessCodeInterpreter): | |
| file_extension = "sh" | |
| proper_name = "Shell" | |
| def __init__(self): | |
| super().__init__() | |
| # Determine the start command based on the platform | |
| if platform.system() == 'Windows': | |
| self.start_cmd = 'cmd.exe' | |
| else: | |
| self.start_cmd = os.environ.get('SHELL', 'bash') | |
| def preprocess_code(self, code): | |
| return preprocess_shell(code) | |
| def line_postprocessor(self, line): | |
| return line | |
| def detect_active_line(self, line): | |
| if "## active_line " in line: | |
| return int(line.split("## active_line ")[1].split(" ##")[0]) | |
| return None | |
| def detect_end_of_execution(self, line): | |
| return "## end_of_execution ##" in line | |
| def preprocess_shell(code): | |
| """ | |
| Add active line markers | |
| Wrap in a try except (trap in shell) | |
| Add end of execution marker | |
| """ | |
| # Add commands that tell us what the active line is | |
| code = add_active_line_prints(code) | |
| # Add end command (we'll be listening for this so we know when it ends) | |
| code += '\necho "## end_of_execution ##"' | |
| return code | |
| def add_active_line_prints(code): | |
| """ | |
| Add echo statements indicating line numbers to a shell string. | |
| """ | |
| lines = code.split('\n') | |
| for index, line in enumerate(lines): | |
| # Insert the echo command before the actual line | |
| lines[index] = f'echo "## active_line {index + 1} ##"\n{line}' | |
| return '\n'.join(lines) |