o
    ̳i                     @   sf   d dl Z d dlZd dlZd dlmZ d dlZd dlmZ d dlm	Z	 eej
jjZG dd deZdS )    N)Path)
Subcommand)get_all_recipesc                       s:   e Zd ZdZ fddZdddZdejfd	d
Z  Z	S )Copyz1Holds all the logic for the `tune cp` subcommand.c              	      sH   t    |jdddddtdtjd| _|   | jj	| j
d d S )Ncpztune cpz-tune cp <recipe|config> destination [OPTIONS]z1Copy a built-in recipe or config to a local path.a              examples:
                $ tune cp lora_finetune_distributed .
                Copied file to ./lora_finetune_distributed.py

                $ tune cp llama2/7B_full ./new_dir/my_custom_lora.yaml --make-parents
                Copyied file to ./new_dir/my_custom_lora.yaml

            Need to see all possible recipes/configs to copy? Try running `tune ls`.
            )progusagehelpdescriptionepilogformatter_class)func)super__init__
add_parsertextwrapdedentargparseRawTextHelpFormatter_parser_add_argumentsset_defaults_cp_cmd)self
subparsers	__class__ E/home/ubuntu/.local/lib/python3.10/site-packages/torchtune/_cli/cp.pyr      s   
zCopy.__init__returnNc                 C   sR   | j jdtdd | j jdtdd | j jdddd	d
d | j jdddd
d dS )zAdd arguments to the parser.filezHRecipe/config to copy. For a list of all possible options, run `tune ls`)typer	   destinationzLocation to copy the file toz-nz--no-clobber
store_truez1Do not overwrite destination if it already existsF)actionr	   defaultz--make-parentszCreate parent directories for destination if they do not exist. If not set to True, will error if parent directories do not existN)r   add_argumentstrr   )r   r   r   r   r   .   s.   
zCopy._add_argumentsargsc                 C   s.  |j }d}t D ],}|j|jkrtd |j }d} n|jD ]}|j|jkr3td d |j }d} nqq|du rD| jd|j d |jdkrS|j	|krS|
|}z/|jrh| rhtd	|  d
 W dS |jrs|jjddd t||}td|  W dS  ty   | jd| d Y dS w )z*Copy a recipe or config to a new location.Nrecipesz.pyconfigsz.yamlzInvalid file name: z3. Try `tune ls` to see all available files to copy. zFile already exists at z, not overwriting.T)parentsexist_okzCopied file to zCannot create regular file: 'z'. No such file or directory. If the specified destination's parent directory does not exist and you would like to create it on-the-fly, use the --make-parents flag.)r"   r   namer    ROOT	file_pathr*   r   errorsuffixwith_suffix
no_clobberexistsprintabsolutemake_parentsparentmkdirshutilcopyFileNotFoundError)r   r(   r"   srcrecipeproper_suffixconfigoutputr   r   r   r   I   sD   





zCopy._cp_cmd)r   N)
__name__
__module____qualname____doc__r   r   r   	Namespacer   __classcell__r   r   r   r   r      s
    
r   )r   r;   r   pathlibr   	torchtunetorchtune._cli.subcommandr   torchtune._recipe_registryr   __file__r9   r/   r   r   r   r   r   <module>   s   