
    Fd$              	          d Z ddlZddlZddlmZ ddlZddlmZmZ ddl	m
Z ddlmZ ddlmZ ddlmZ dd	lmZ d
ZdZ eddddddddddd          dedededefd            Z edddddddi          dededefd            Zdededefd Z ed!d"d#dd$ddid% d&'          d#ededefd(            Z ed)d"d*dd+ddid, d-          dedefd.            Zdefd/ZdS )0z"Execute code in a Docker container    N)Path)DockerExceptionImageNotFound)	Container)Agent)command)Config)logger	allowlistdenylistexecute_python_codez%Creates a Python file and executes itstringzThe Python code to runT)typedescriptionrequiredz%A name to be given to the python file)codenamer   r   agentreturnc                 4   |j         }|j                            t          |d                    }t	          j        |d           |                    d          s|dz   }|j                            ||z            }|                    |          sdS 	 t          |dd          5 }|	                    |            d	d	d	           n# 1 swxY w Y   t          t          |          |          S # t          $ r}d
t          |           cY d	}~S d	}~ww xY w)ai  Create and execute a Python file in a Docker container and return the STDOUT of the
    executed code. If there is any data that needs to be captured use a print statement

    Args:
        code (str): The Python code to run
        name (str): A name to be given to the Python file

    Returns:
        str: The STDOUT captured from the code when it ran
    executed_codeT)exist_ok.pyzDError: 'name' argument resulted in path traversal, operation abortedzw+utf-8)encodingNError: )ai_name	workspaceget_pathr   osmakedirsendswithis_relative_toopenwriteexecute_python_filestr	Exception)r   r   r   r   code_dir	file_pathfes           QC:\Users\Administrator\Downloads\Auto-GPT-master\autogpt\commands\execute_code.pyr   r      si   6 mG''Wo(F(FGGHK4((((== e| ((D99I##H-- VUU")TG444 	GGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 #3y>>5999 " " "!Q!!!!!!!!"sB   C1 'C	=C1 	CC1 CC1 1
D;DDDr&   z Executes an existing Python filefilenamezThe name of te file to executec           	         t          j        d|  d|j        j         d           |                     d          sdS t          |           }|                                sd|  dS t                      rMt          j	        dt          |          gd	d
|j        j                  }|j        dk    r|j        S d|j         S 	 t          j                    }d}	 |j                            |           t          j        d| d           n# t&          $ r t          j        d| d           t          j                    }|                    |d	d	          D ]`}|                    d          }|                    d          }	|r|	rt          j        | d|	            J|rt          j        |           aY nw xY w|j        	                    |dt          |                    |j        j                            g|j        j        dddidd	d	d	          }
|
                                 |
                                                    d          }|
                                 |S # t<          $ r0}t          j        d           dt          |           cY d}~S d}~wt>          $ r}dt          |           cY d}~S d}~ww xY w)zExecute a Python file in a Docker container and return the output

    Args:
        filename (str): The name of the file to execute

    Returns:
        str: The output of the file
    zExecuting python file '' in working directory ''r   z5Error: Invalid file type. Only .py files are allowed.zpython: can't open file 'z&': [Errno 2] No such file or directorypythonTutf8)capture_outputr   cwdr   r   zpython:3-alpinezImage 'z' found locallyz,' not found locally, pulling from Docker Hub)streamdecodestatusprogressz: z
/workspacero)bindmode)volumesworking_dirstderrstdoutdetachr   zzCould not run the script in a container. If you haven't already, please install Docker https://docs.docker.com/get-docker/N) r
   infoconfigworkspace_pathr"   r   is_file$we_are_running_in_a_docker_container
subprocessrunr'   
returncoder@   r?   dockerfrom_envimagesgetwarnr   	APIClientpull
containersrelative_tor   rootwaitlogsr7   remover   r(   )r.   r   r*   resultclient
image_namelow_level_clientliner8   r9   	containerrU   r,   s                r-   r&   r&   D   sz   ( Kb(bbELD_bbb   U## GFFXI 
 YXXX	
 ,-- 
-s9~~&+	
 
 
 !!= ,V],,,7""" '
	(Mj)))K=*===>>>> 	( 	( 	(KR*RRR    &/11(--jd-SS ( ((++88J// (h (K6 7 7X 7 78888 (K'''( (	( &,%6%:%:s9001EFFGGH+( . . % &; &
 &
	 	~~&&w//
  " " " I	
 	
 	
 "Q!!!!!!!! " " "!Q!!!!!!!!"sV   I, 2D 
I, B.F<9I, ;F<<B/I, ,
K
6%J!K
!K
.K?K
K
r   rC   c                 x    | sdS  | j                     d         }|j        t          k    r	||j        v S ||j        vS )zValidate a command to ensure it is allowed

    Args:
        command (str): The command to validate
        config (Config): The config to use to validate the command

    Returns:
        bool: True if the command is allowed, False otherwise
    Fr   )splitshell_command_controlALLOWLIST_CONTROLshell_allowlistshell_denylist)r   rC   command_names      r-   validate_commandrd      sN      u 7=??1%L#'888v5556#888    execute_shellz7Executes a Shell Command, non-interactive commands onlycommand_linezThe command line to executec                     | j         S Nexecute_local_commandsrC   s    r-   <lambda>rm      s	    68 re   zYou are not allowed to run local shell commands. To execute shell commands, EXECUTE_LOCAL_COMMANDS must be set to 'True' in your config file: .env - do not attempt to bypass the restriction.)enableddisabled_reasonc                    t          | |j                  st          j        d|  d           dS t	          j                    }|                    |j        j                  st          j	        |j        j                   t          j        d|  dt          j
                     d           t          j        | dd          }d	|j         d
|j         }t          j	        |           |S )zExecute a shell command and return the output

    Args:
        command_line (str): The command line to execute

    Returns:
        str: The output of the command
    	Command '' not allowed)Error: This Shell Command is not allowed.Executing command 'r0   r1   T)r4   shellzSTDOUT:
z	
STDERR:
)rd   rC   r
   rB   r   r5   r#   rD   r    chdirgetcwdrG   rH   r@   r?   )rg   r   current_dirrW   outputs        r-   rf   rf      s    0 L%,77 ;;;;;<<<::(**K%%el&ABB .
,---
KRlRRBIKKRRR   ^LTJJJFBBB6=BBF H[Mre   execute_shell_popenqueryzThe search queryc                     | j         S ri   rj   rl   s    r-   rm   rm      s	    60 re   zYou are not allowed to run local shell commands. To execute shell commands, EXECUTE_LOCAL_COMMANDS must be set to 'True' in your config. Do not attempt to bypass the restriction.c                    t          | |j                  st          j        d|  d           dS t	          j                    }|j        j        |vrt	          j        |j        j                   t          j        d|  dt	          j                     d           t          j	        }t          j
        | d||          }t	          j        |           d	t          |j                   dS )
a  Execute a shell command with Popen and returns an english description
    of the event and the process id

    Args:
        command_line (str): The command line to execute

    Returns:
        str: Description of the fact that the process started and its id
    rq   rr   rs   rt   r0   r1   T)ru   r@   r?   zSubprocess started with PID:')rd   rC   r
   rB   r    rw   rD   rv   rG   DEVNULLPopenr'   pid)rg   r   rx   do_not_show_outputprocesss        r-   rz   rz      s    2 L%,77 ;;;;;<<<::)++K|"+55
,---
KRlRRBIKKRRR   $+D);DV  G H[>3w{+;+;>>>>re   c                  @    t           j                            d          S )zCheck if we are running in a Docker container

    Returns:
        bool: True if we are running in a Docker container, False otherwise
    z/.dockerenv)r    pathexists re   r-   rF   rF   !  s     7>>-(((re   )__doc__r    rG   pathlibr   rJ   docker.errorsr   r   docker.models.containersr   DockerContainerautogpt.agent.agentr   autogpt.command_decoratorr   autogpt.configr	   autogpt.logsr
   r`   DENYLIST_CONTROLr'   r   r&   boolrd   rf   rz   rF   r   re   r-   <module>r      s   ( ( 				            8 8 8 8 8 8 8 8 A A A A A A % % % % % % - - - - - - ! ! ! ! ! !         	+ 3
 
 B
 
   "c " "U "s " " "!  "B 	&;
 

 
Z"# Z"e Z" Z" Z" Z"
 
Z"z9c 96 9d 9 9 9 9* 	=8
 
 98L   E c    > 	=-
 
 10@  ?U  ?s  ?  ?  ?  ?F)d ) ) ) ) ) )re   