o
    xi                     @  s   d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
 ddlmZmZ ddlZddlmZ ddlmZ dd	lmZ eeZed
 ZG dd dZG dd de	ZG dd de	ZdS )zImplementation of the abstract runner class.

This class defines the interface that the W&B launch runner uses to manage the lifecycle
of runs launched in different environments (e.g. runs launched locally or in a cluster).
    )annotationsN)ABCabstractmethod)AnyLiteral)Api)runid   )LaunchProject)unknownstartingrunningfailedfinishedstoppingstopped	preemptedc                   @  s@   e Zd ZddddZdd
dZdddZdddZdddZdS )Statusr   NstateStatemessages	list[str]c                 C  s   || _ |pg | _d S N)r   r   )selfr   r    r   T/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/launch/runner/abstract.py__init__'   s   zStatus.__init__returnc                 C     | j S r   r   r   r   r   r   __repr__+      zStatus.__repr__strc                 C  r   r   r   r    r   r   r   __str__.   r"   zStatus.__str___Status__valueobjectboolc                 C  s    t |tr| j|jkS | j|kS r   )
isinstancer   r   )r   r%   r   r   r   __eq__1   s   

zStatus.__eq__intc                 C  s
   t | jS r   )hashr   r    r   r   r   __hash__7   s   
zStatus.__hash__)r   N)r   r   r   r   )r   r   )r   r#   )r%   r&   r   r'   )r   r*   )__name__
__module____qualname__r   r!   r$   r)   r,   r   r   r   r   r   &   s    


r   c                   @  s   e Zd ZdZdddZedddZed d
dZ	d!d"ddZ	ed#ddZ
edddZedddZeed ddZdS )$AbstractRuna&  Wrapper around a W&B launch run.

    A launched run is a subprocess running an entry point
    command, that exposes methods for waiting on and cancelling the run.
    This class defines the interface that the W&B launch runner uses to manage the lifecycle
    of runs launched in different environments (e.g. runs launched locally or in a cluster).
    ``AbstractRun`` is not thread-safe. That is, concurrent calls to wait() / cancel()
    from multiple threads may inadvertently kill resources (e.g. local processes) unrelated to the
    run.
    r   Nonec                 C  s   t  | _d S r   )r   _statusr    r   r   r   r   G   s   zAbstractRun.__init__r   c                 C  r   r   )r2   r    r   r   r   statusJ   s   zAbstractRun.status
str | Nonec                      dS )z(Return the logs associated with the run.Nr   r    r   r   r   get_logsN       zAbstractRun.get_logsFcmdr   output_onlybool | None&subprocess.Popen[bytes] | bytes | Nonec              
   C  sz   z t j}tj||tjd}|r|  |jdur|j W S |W S  tjy< } zt	
d|  W Y d}~dS d}~ww )zRun the command and returns a popen object or the stdout of the command.

        Arguments:
        cmd: The command to run
        output_only: If true just return the stdout bytes
        )envstdoutNzCommand failed: )osenviron
subprocessPopenPIPEwaitr=   readCalledProcessErrorwandb	termerror)r   r8   r9   r<   popener   r   r   _run_cmdR   s   	
zAbstractRun._run_cmdr'   c                   r5   )zWait for the run to finish, returning True if the run succeeded and false otherwise.

        Note that in some cases, we may wait until the remote job completes rather than until the W&B run completes.
        Nr   r    r   r   r   rC   g   r7   zAbstractRun.waitc                   r5   )zGet status of the run.Nr   r    r   r   r   
get_statusn   r7   zAbstractRun.get_statusc                   r5   )zCancel the run (interrupts the command subprocess, cancels the run, etc).

        Cancels the run and waits for it to terminate. The W&B run status may not be
        set correctly upon run cancellation.
        Nr   r    r   r   r   cancelr   r7   zAbstractRun.cancelc                 C  s   d S r   r   r    r   r   r   idz   s   zAbstractRun.idN)r   r1   )r   r   )r   r4   )F)r8   r   r9   r:   r   r;   r   r'   )r-   r.   r/   __doc__r   propertyr3   r   r6   rJ   rC   rK   rL   rM   r   r   r   r   r0   ;   s$    
r0   c                   @  sT   e Zd ZU dZded< dd
dZdddZedddZd ddZ	e
d!ddZdS )"AbstractRunneraD  Abstract plugin class defining the interface needed to execute W&B Launches.

    You can define subclasses of ``AbstractRunner`` and expose them as third-party
    plugins to enable running W&B projects against custom execution backends
    (e.g. to run projects against your team's in-house cluster or job scheduler).
    r#   _typeapir   backend_configdict[str, Any]r   r1   c                 C  s$   || _ || _t | _t | _d S r   )_apirT   r>   getcwd_cwdr   generate_id
_namespace)r   rS   rT   r   r   r   r      s   
zAbstractRunner.__init__r8   r4   c                 C  s
   t |S )z>Cross platform utility for checking if a program is available.)shutilwhich)r   r8   r   r   r   find_executable   s   
zAbstractRunner.find_executabler   c                 C  s   | j jS r   )rV   api_keyr    r   r   r   r^      s   zAbstractRunner.api_keyr'   c                 C  s$   | j jdu rtd td dS )zThis is called on first boot to verify the needed commands, and permissions are available.

        For now just call `wandb.termerror` and `sys.exit(1)`
        Nz?Couldn't find W&B api key, run wandb login or set WANDB_API_KEY   T)rV   r^   rF   rG   sysexitr    r   r   r   verify   s   
zAbstractRunner.verifylaunch_projectr
   	image_uriAbstractRun | Nonec                   r5   )a  Submit an LaunchProject to be run.

        Returns a SubmittedRun object to track the execution
        Arguments:
        launch_project: Object of _project_spec.LaunchProject class representing a wandb launch project

        Returns:
            A :py:class:`wandb.sdk.launch.runners.SubmittedRun`. This function is expected to run
            the project asynchronously, i.e. it should trigger project execution and then
            immediately return a `SubmittedRun` to track execution status.
        Nr   )r   rc   rd   r   r   r   run   r7   zAbstractRunner.runN)rS   r   rT   rU   r   r1   )r8   r#   r   r4   )r   r   rN   )rc   r
   rd   r#   r   re   )r-   r.   r/   rO   __annotations__r   r]   rP   r^   rb   r   rf   r   r   r   r   rQ      s   
 



rQ   )rO   
__future__r   loggingr>   r[   r@   r`   abcr   r   typingr   r   rF   wandb.apis.internalr   wandb.sdk.libr   _project_specr
   	getLoggerr-   _loggerr   r   r0   rQ   r   r   r   r   <module>   s(    
E