o
    i3                     @   s  d dl Z d dlmZ d dlmZmZmZmZmZm	Z	 d dl
Z
d dlZd dlmZ d dlmZ ddlmZmZmZ ddlmZmZmZmZmZ dd	lmZmZmZmZ d
dlmZmZm Z m!Z!m"Z"m#Z# d
dlm$Z$ e$j%ddddde!dde de!e& dddde"ddddde"ddddde"ddddfdej'de(d ed!e)d"e)d#e)fd$d%Z*e dddded&d ede(d'ee(ef d!e)d"e)d(e)d)e)d*e(d+dfd,d-Z+defd edee( d*e(d+dfd.d/Z,e dddfd0ee( d1e)d"e)d(e)d+df
d2d3Z-d0e	e( d4e	e( de(d+dfd5d6Z.d ed7ee(ef d+e)fd8d9Z/d ed7ee(ef d+dfd:d;Z0ed<d ed7ee(ef d*e(d+ee(ef fd=d>Z1d ed?ee( d+eee(ee( f  fd@dAZ2dS )B    N)Path)AnyDictIterableListOptionalSequence)msg)locale_escape   )SimpleFrozenDictSimpleFrozenListcheck_spacy_env_vars)get_checksumget_hashis_cwdjoin_commandload_project_config)parse_config_overridesrun_commandsplit_commandworking_dir   )COMMANDPROJECT_FILEPROJECT_LOCKArgOpt_get_parent_command)apprunT)allow_extra_argsignore_unknown_options)context_settingszName of command defined in the )helpzELocation of project directory. Defaults to current working directory.F)r$   exists	file_okayz--forcez-Fz/Force re-running steps, even if nothing changedz--dryz-Dz+Perform a dry run and don't execute scriptsz--helpz+Show help message and available subcommandsctx
subcommandproject_dirforcedry	show_helpc                 C   sB   t | }|s|st||| dS t| j}t||||||d dS )aL  Run a named command or workflow defined in the project.yml. If a workflow
    name is specified, all commands in the workflow are run, in order. If
    commands define dependencies and/or outputs, they will only be re-run if
    state has changed.

    DOCS: https://github.com/explosion/weasel/tree/main/docs/cli.md#rocket-run
    )	overridesr*   r+   parent_commandN)r   print_run_helpr   argsproject_run)r'   r(   r)   r*   r+   r,   r.   r-    r2   B/home/ubuntu/.local/lib/python3.10/site-packages/weasel/cli/run.pyproject_run_cli   s   

r4   )r-   r*   r+   captureskip_requirements_checkr.   r-   r5   r6   r.   returnc             	   C   s  t | |d}dd |dg D }	|di }
tt|	 t|
 | ||
v rEtd| d |
| D ]}t| |||||d q6dS |	| }|d	g D ] }| |  sod
| d| }d}|sednd}tj	|||d qOt
  t| C}t| t||}|s|std|d  d n t|d ||d |st|| W d   dS W d   dS W d   dS 1 sw   Y  dS )a  Run a named script defined in the project.yml. If the script is part
    of the default pipeline (defined in the "run" section), DVC is used to
    execute the command, so it can determine whether to rerun it. It then
    calls into "exec" to execute it.

    project_dir (Path): Path to project directory.
    subcommand (str): Name of command to run.
    overrides (Dict[str, Any]): Optional config overrides.
    force (bool): Force re-running, even if nothing changed.
    dry (bool): Perform a dry run and don't execute commands.
    capture (bool): Whether to capture the output and errors of individual commands.
        If False, the stdout and stderr will not be redirected, and if there's an error,
        sys.exit will be called with the return code. You should use capture=False
        when you want to turn over execution to the command, and capture=True
        when you want to run the command more like a function.
    skip_requirements_check (bool): No longer used, deprecated.
    )r-   c                 S      i | ]}|d  |qS namer2   .0cmdr2   r2   r3   
<dictcomp>Q       zproject_run.<locals>.<dictcomp>commands	workflowszRunning workflow '')r-   r*   r+   r5   depsz)Missing dependency specified by command 'z': zGMaybe you forgot to run the 'weasel assets' command or a previous step?r   Nexitsz
Skipping 'r:   z': nothing changedscript)r+   r5   )r   getvalidate_subcommandlistkeysr	   infor1   r%   failr   r   dividercheck_rerunrun_commandsupdate_lockfile)r)   r(   r-   r*   r+   r5   r6   r.   configr@   rA   r=   deperrerr_help	err_exitscurrent_dirrerunr2   r2   r3   r1   4   sL   



"r1   c                    s  t | }|dg }dd |D  |di }t| rdn| }|rtt  t| | td| d| d|  | v rU | d	}|rStd
| d
 dS dS ||v r|| }tdt| d  fddt|D }	t	
|	 | d| d}
td|
  dS dS td |d}|rtt| d
 |rtdt  td| d|  t	
dd |D  |rtdt  td| d|  t	
dd | D  dS dS )aJ  Simulate a CLI help prompt using the info available in the project.yml.

    project_dir (Path): The project directory.
    subcommand (Optional[str]): The subcommand or None. If a subcommand is
        provided, the subcommand help is shown. Otherwise, the top-level help
        and a list of available commands is printed.
    r@   c                 S   r8   r9   r2   r;   r2   r2   r3   r>      r?   z"print_run_help.<locals>.<dictcomp>rA    zUsage:  run  r$   
z
Workflow consisting of z
 commands:c                    s2   g | ]\}}|d   d|  |  ddfqS )r   . r$   rX   rG   )r<   istepr@   r2   r3   
<listcomp>   s     z"print_run_help.<locals>.<listcomp>z run [COMMAND] z --helpzFor command details, run: titlezAvailable commands in c                 S   s    g | ]}|d  | ddfqS )r:   r$   rX   r]   r;   r2   r2   r3   ra      s     zAvailable workflows in z run [WORKFLOW] c                 S   s   g | ]\}}|d  |fqS )z -> )join)r<   r:   stepsr2   r2   r3   ra      s    N)r   rG   r   rH   rI   rJ   printlen	enumerater	   tabler
   r   items)r)   r(   r.   rQ   config_commandsrA   project_loc	help_textrd   
steps_datahelp_cmdrb   r2   r`   r3   r/   t   sH   



r/   r@   silentc                 C   s   | D ]?}t |}t|r|d dv rtj|d< nt|r.|d dv r.tjddg|dd }|s9tdt|  |sAt||d	 qdS )
ab  Run a sequence of commands in a subprocess, in order.

    commands (List[str]): The string commands.
    silent (bool): Don't print the commands.
    dry (bool): Perform a dry run and don't execut anything.
    capture (bool): Whether to capture the output and errors of individual commands.
        If False, the stdout and stderr will not be redirected, and if there's an error,
        sys.exit will be called with the return code. You should use capture=False
        when you want to turn over execution to the command, and capture=True
        when you want to run the command more like a function.
    r   )pythonpython3)pippip3z-mrr   r   NzRunning command: )r5   )r   rf   sys
executablere   r   r   )r@   ro   r+   r5   ccommandr2   r2   r3   rO      s   rO   rA   c                 C   s   | s|st jdt dd || vrN||vrPg }|dv r!|d | r.|dd|   |r;|dd|  t jd	| d
t d|dd dS dS dS )zCheck that a subcommand is valid and defined. Raises an error otherwise.

    commands (Sequence[str]): The available commands.
    subcommand (str): The subcommand.
    z$No commands or workflows defined in r   rD   )assetsassetz-Did you mean to run: python -m weasel assets?zAvailable commands: z, zAvailable workflows: z Can't find command or workflow 'z' in r\   N)r	   rL   r   appendrc   )r@   rA   r(   help_msgr2   r2   r3   rH      s    

rH   rw   c                 C   st   | ddrdS | t }| sdS t|}|d |vrdS ||d  }| dg s-dS t| |}t|t|kS )a+  Check if a command should be rerun because its settings or inputs/outputs
    changed.

    project_dir (Path): The current project directory.
    command (Dict[str, Any]): The command, as defined in the project.yml.
    strict_version (bool):
    RETURNS (bool): Whether to re-run the command.
    no_skipFTr:   outs)rG   r   r%   srsly	read_yamlget_lock_entryr   )r)   rw   	lock_pathdataentry
lock_entryr2   r2   r3   rN      s   

rN   c                 C   sN   | t  }| st|i  i }nt|}t| |||d < t|| dS )a>  Update the lockfile after running a command. Will create a lockfile if
    it doesn't yet exist and will add an entry for the current command, its
    script and dependencies/outputs.

    project_dir (Path): The current project directory.
    command (Dict[str, Any]): The command, as defined in the project.yml.
    r:   N)r   r%   r~   
write_yamlr   r   )r)   rw   r   r   r2   r2   r3   rP      s   
rP   )r.   c                C   s^   t | |dg }t | |dg }t | |dg }| d|d  |d |g ||dS )a  Get a lockfile entry for a given command. An entry includes the command,
    the script (command steps) and a list of dependencies and outputs with
    their paths and file hashes, if available. The format is based on the
    dvc.lock files, to keep things consistent.

    project_dir (Path): The current project directory.
    command (Dict[str, Any]): The command, as defined in the project.yml.
    RETURNS (Dict[str, Any]): The lockfile entry.
    rC   outputsoutputs_no_cacherY   r:   rF   )r=   rF   rC   r}   )get_fileinforG   )r)   rw   r.   rC   r}   outs_ncr2   r2   r3   r     s   
r   pathsc                 C   s>   g }|D ]}| | }|  rt|nd}|||d q|S )a%  Generate the file information for a list of paths (dependencies, outputs).
    Includes the file path and the file's checksum.

    project_dir (Path): The current project directory.
    paths (List[str]): The file paths.
    RETURNS (List[Dict[str, str]]): The lockfile entry for a file.
    N)pathmd5)r%   r   rz   )r)   r   r   r   	file_pathr   r2   r2   r3   r   )  s   r   )3rt   pathlibr   typingr   r   r   r   r   r   r~   typerwasabir	   wasabi.utilr
   utilr   r   r   r   r   r   r   r   r   r   r   r   mainr   r   r   r   r   r   r   rw   cwdContextstrboolr4   r1   r/   rO   rH   rN   rP   r   r   r2   r2   r2   r3   <module>   s      
$
	

A
0
$


 


.