o
    i!                     @   s  d Z ddlZddlmZ ddlmZmZmZmZ ddl	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mZmZ d
ZdZdZde de de de Zeeee ddddedde ddedddddedddddedddd dfd!ed"ee d#ed$ed%ef
d&d'Z 	d5dddd(d!ed"ee d#ed$ed%ed)dfd*d+Z!				d6d,ed-eeef d"ee d#ed$ed%ed)efd.d/Z"d5d0ee d"ee d)dfd1d2Z#d!ed)dfd3d4Z$dS )7ztThis module contains helpers and subcommands for integrating Weasel
with Data Version Control (DVC). https://dvc.org    N)Path)AnyDictListOptional)msg   )get_hashjoin_commandload_project_configrun_commandworking_dir   )COMMANDNAMEPROJECT_FILEArgOptappzdvc.yaml.dvcdvcz6# This file is auto-generated by Weasel based on your z. If you've
# edited your z-, you can regenerate this file by running:
#  zELocation of project directory. Defaults to current working directory.TF)helpexists	file_okayzName of workflow defined in z(. Defaults to first workflow if not set.)r   	--verbosez-VzPrint more info--quietz-qzPrint less infoz--forcez-FzForce update DVC configproject_dirworkflowverbosequietforcec                 C   s   t | ||||d dS )a  Auto-generate Data Version Control (DVC) config. A DVC
    project can only define one pipeline, so you need to specify one workflow
    defined in the project.yml. If no workflow is specified, the first defined
    workflow is used. The DVC config will only be updated if the project.yml
    changed.

    DOCS: https://github.com/explosion/weasel/tree/main/docs/cli.md#repeat-dvc
    r   r    r!   N)project_update_dvc)r   r   r   r    r!    r$   B/home/ubuntu/.local/lib/python3.10/site-packages/weasel/cli/dvc.pyproject_update_dvc_cli   s   r&   r"   returnc                C   sR   t | }t| |||||d}d}|rtdt | dS tdt d| dS )a  Update the auto-generated Data Version Control (DVC) config file. A DVC
    project can only define one pipeline, so you need to specify one workflow
    defined in the project.yml. Will only update the file if the checksum changed.

    project_dir (Path): The project directory.
    workflow (Optional[str]): Optional name of workflow defined in project.yml.
        If not set, the first workflow will be used.
    verbose (bool): Print more info.
    quiet (bool): Print less info.
    force (bool): Force update DVC config.
    r"   z0To execute the workflow with DVC, run: dvc reprozUpdated DVC config from zNo changes found in z, no update neededN)r   update_dvc_configr   goodr   info)r   r   r   r    r!   configupdatedhelp_msgr$   r$   r%   r#   *   s   r#   pathr+   c              	   C   s  t |  |di }t| }t|| |s|d }t|}|  } | t }	|	 rX|	j	ddd}
|

  dd}W d   n1 sGw   Y  ||krT|sTd	S |	  g }d
d |dg D }g }|rn|d |ru|d || D ]y}|| }|dg }|dg }|dg }|s|s|sqyddtdd|g}dd dd |D D }dd dd |D D }dd dd |D D }dg|d|dt| d}|d r|d! g |||||}|t| qy|stjd"d#d$ t|  |D ]}d%| }t| qW d   n	1 sw   Y  |	j	d&dd!}
|
 }|
dd |
d| d't d'|  W d   d(S 1 sKw   Y  d(S ))a~  Re-run the DVC commands in dry mode and update dvc.yaml file in the
    project directory. The file is auto-generated based on the config. The
    first line of the auto-generated file specifies the hash of the config
    dict, so if any of the config values change, the DVC config is regenerated.

    path (Path): The path to the project directory.
    config (Dict[str, Any]): The loaded project.yml.
    verbose (bool): Whether to print additional info (via DVC).
    quiet (bool): Don't output anything (via DVC).
    force (bool): Force update, even if hashes match.
    RETURNS (bool): Whether the DVC config file was updated.
    	workflowsr   rutf8)encodingz#  NFc                 S   s   i | ]}|d  |qS )namer$   ).0cmdr$   r$   r%   
<dictcomp>m   s    z%update_dvc_config.<locals>.<dictcomp>commandsr   r   depsoutputsoutputs_no_cachepythonz-mprojectrunc                 S      g | ]	}|D ]}|qqS r$   r$   r5   clcr$   r$   r%   
<listcomp>       z%update_dvc_config.<locals>.<listcomp>c                 S      g | ]}d |gqS )z-dr$   r5   pr$   r$   r%   rC          c                 S   r?   r$   r$   r@   r$   r$   r%   rC      rD   c                 S   rE   )z-or$   rF   r$   r$   r%   rC      rH   c                 S   r?   r$   r$   r@   r$   r$   r%   rC      rD   c                 S   rE   )z-Or$   rF   r$   r$   r%   rC      rH   z-nz-wz	--no-execno_skipz--always-changedzhNo usable commands for DVC found. This can happen if none of your commands have dependencies or outputs.r   exitszdvc zr+
T)
ensure_dvcgetlistkeyscheck_workflowsr	   resolve
DVC_CONFIGr   openreadlinestripreplaceunlinkappendr   strr
   r   failr   r   readseekwriteDVC_CONFIG_COMMENT)r.   r+   r   r   r    r!   r/   workflow_namesconfig_hashdvc_config_pathfref_hashdvc_commandsconfig_commandsflagsr4   commandr9   r:   r;   project_cmddeps_cmdoutputs_cmdoutputs_nc_cmddvc_cmdfull_cmdrB   dvc_commandcontentr$   r$   r%   r(   H   st   


"



r(   r/   c              	   C   sx   | st jdt ddd |dur(|| vr(t jd| dt dd	|  dd |s:t d
t d| d  d dS dS )zValidate workflows provided in project.yml and check that a given
    workflow can be used to generate a DVC config.

    workflows (List[str]): Names of the available workflows.
    workflow (Optional[str]): The name of the workflow to convert.
    zNo workflows defined in zA. To generate a DVC config, define at least one list of commands.r   rJ   Nz
Workflow 'z' not defined in z. Available workflows: z, zLNo workflow specified for DVC pipeline. Using the first workflow defined in z: 'r   ')r   r[   r   joinwarn)r/   r   r$   r$   r%   rQ      s*   
rQ   c                 C   s`   zt jddgt jd W n ty   tjdddd Y nw | d  s.tjd	d
dd dS dS )zyEnsure that the "dvc" command is available and that the current project
    directory is an initialized DVC project.
    r   z	--version)stdoutztTo use Weasel with DVC (Data Version Control), DVC needs to be installed and the 'dvc' command needs to be availablezYou can install the Python package from pip (pip install dvc) or conda (conda install -c conda-forge dvc). For more details, see the documentation: https://dvc.org/doc/installr   rJ   r   z(Project not initialized as a DVC projectzTo initialize a DVC project, you can run 'dvc init' in the project directory. For more details, see the documentation: https://dvc.org/doc/command-reference/initN)
subprocessr>   DEVNULL	Exceptionr   r[   r   )r   r$   r$   r%   rM      s    
	
rM   )N)NFFF)%__doc__ru   pathlibr   typingr   r   r   r   wasabir   utilr	   r
   r   r   r   mainr   r   r   r   r   r   rS   DVC_DIRUPDATE_COMMANDr_   rh   cwdrZ   boolr&   r#   r(   rQ   rM   r$   r$   r$   r%   <module>   s     
!

 V