
    c#b              
       V   d Z ddlmZ ddlmZ ddlmZ ddl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 n# e$ r	 dd	lmZ Y nw xY w	 dd	lmZ n# e$ r	 dd	lmZ Y nw xY wd
ZdZeez   Zd Z edddg          Z edd           edd           edd           edd           edd           edd           edd          fZ G d de          Z G d de          Z  G d de          Z! G d d e          Z" G d! d"e          Z#d# Z$ G d$ d%e          Z%dS )&z3Class representing a command from the project file.    )absolute_import)copy)
namedtupleN)Template)_verbose_logger)	conda_apilogged_subprocess
py2_compat)quote)	descriptionenv_specsupports_http_options	bokeh_appnotebookunixwindowsconda_app_entry	variables)registers_fusion_functionc                  0    t          j                    dk    S )NWindows)platformsystem     Alib/python3.11/site-packages/anaconda_project/project_commands.py_is_windowsr   &   s     O*+r   _ArgSpecoption	has_value--anaconda-project-hostT--anaconda-project-address--anaconda-project-port--anaconda-project-url-prefix--anaconda-project-no-browserF--anaconda-project-iframe-hosts--anaconda-project-use-xheadersc                   $    e Zd ZeZd Zd Zd ZdS )_ArgsTransformerc                 l   |sg S |d         }|dk    r|S | j         D ]v}|j        r5|j        dz   }||j        k    rt          |          dk    s|d                             d          r@||j                                     d           |                     ||dd                    c S ||j                                     |d                    |                     ||dd                    c S |                    |          rU||j                                     |t          |          d                     |                     ||dd                    c S @||j        k    r+dg||j        <   |                     ||dd                    c S x|g|                     ||dd                    z   S )	Nr   --=   -    T)specsr    r   len
startswithappend_parse_args_removing_known)selfresultsargsargspecwith_equalss         r   r5   z+_ArgsTransformer._parse_args_removing_known7   s    	I1g$; 	KJ 	J 	JD~ J"kC/$+% N4yyA~ Ra););C)@)@ R  ,33B777#>>wQRRQQQQQ,33DG<<<#>>wQRRQQQQQ^^K00 NDK(//C4D4D4E4E0FGGG::7DHMMMMMN # J(,v$66wQRRIIIIIJ ut66wQRRIIIIr   c                     d | j         D             }|                     ||          }t          |                                d           }|                     ||          S )Nc                     i | ]
}|j         g S r   r   .0r:   s     r   
<dictcomp>z3_ArgsTransformer.transform_args.<locals>.<dictcomp>Z       :::t4;:::r   c                     | d         S )Nr   r   )xs    r   <lambda>z1_ArgsTransformer.transform_args.<locals>.<lambda>]   s
    QqT r   )key)r1   r5   sorteditemsadd_args)r6   r8   r7   with_removedresults_lists        r   transform_argsz_ArgsTransformer.transform_argsY   s\    ::tz:::66wEEgmmoo>>BBB}}\<888r   c                     |S Nr   r6   r7   r8   s      r   rI   z_ArgsTransformer.add_args`   s    r   N)__name__
__module____qualname__
HTTP_SPECSr1   r5   rL   rI   r   r   r   r)   r)   4   sJ        E J  J  JD9 9 9    r   r)   c                       e Zd Zd ZdS )_BokehArgsTransformerc                    g }|D ]\  }}|dv rD|D ]@}|                     |                    dd                     |                     |           AM|dk    r0|D ],}|                     d           |                     |           -|dk    r|s|                     d           |dk    r"|r|d	         d
u r|                     d           |dk    rt          d          ||z   S )N)r!   r#   r"   anaconda-project-r/   r$   z--prefixr%   z--showr'   r   Tz--use-xheadersr&   z#unhandled http option for bokeh app)r4   replaceRuntimeError)r6   r7   r8   addedr   valuesvs          r   rI   z_BokehArgsTransformer.add_argsf   sL    ' 	J 	JVVmm J $ $ALL0CR!H!HIIILLOOOO$ :: J $ $ALL,,,LLOOOO$ :: 
J +LL***<< J 3fQi4/ 3LL!1222<< J"#HIIIt|r   N)rP   rQ   rR   rI   r   r   r   rU   rU   e   s#            r   rU   c                   $    e Zd ZdZd Zd Zd ZdS )_TemplateArgsTransformerz0ArgsTransformer that supports jinja2 templating.c                     |S rN   r   rO   s      r   rI   z!_TemplateArgsTransformer.add_args   s    r   c                 V    |                     dd                               dd          S )z@Turn a commandline argument into a Python identifier for jinja2.r+   r/   r.   _)rX   )r6   r9   s     r   arg_to_identifierz*_TemplateArgsTransformer.arg_to_identifier   s&    {{4$$,,S#666r   c                      d  j         D             }                     ||           t                                          |          }d |                                D             }i }|                                D ]9\  }}|                    d          r|                    dd          }	|	|vr|||	<   :d|v rG|d         }
t          |
t                    r|
d         n|
|d<   t          |
t                    r|
n|
g|d<   |	                    |           |	                    |            fd|                                D             }t          |                              |          }t          ||          gS )	Nc                     i | ]
}|j         g S r   r>   r?   s     r   rA   z?_TemplateArgsTransformer.parse_and_template.<locals>.<dictcomp>   rB   r   c                 T    i | ]%\  }}||t          |          d k    r|d         n|&S )r-   )r2   )r@   kr\   s      r   rA   z?_TemplateArgsTransformer.parse_and_template.<locals>.<dictcomp>   s;    SSSdaQRSc!ffk0QrUUqSSSr   z--anaconda-project-r/   hostrf   hostsc                 B    i | ]\  }}                     |          |S r   )rb   )r@   rg   r\   r6   s      r   rA   z?_TemplateArgsTransformer.parse_and_template.<locals>.<dictcomp>   s-    MMMtq!d,,Q//MMMr   )r1   r5   r^   rL   rH   r3   rX   
isinstancelistupdater   render"_append_extra_args_to_command_line)r6   commandenviron
extra_argsr7   rH   replacementsrg   r\   replacementrh   
normalizedtemplated_commands   `            r   parse_and_templatez+_TemplateArgsTransformer.parse_and_template   s   ::tz:::''<<<-//>>zJJ
SSgmmooSSSKKMM 	2 	2DAq||122 2ii(=rBBe+ 201L-\! 	O'D/9$/E/E#O4884L ,6tT,B,B$NDDL!\"""WMMMMu{{}}MMM
$W--44Z@@23DjQQRRr   N)rP   rQ   rR   __doc__rI   rb   rw   r   r   r   r^   r^      sL        ::  7 7 7S S S S Sr   r^   c                       e Zd Zd Zd ZdS )_NotebookArgsTransformerc                     || _         d S rN   )rp   )r6   rp   s     r   __init__z!_NotebookArgsTransformer.__init__   s    r   c                 |   g }t           j                            | j        j                  }dt          |          z  }|                    |           |D ]b\  }}|dk    r|dk    rD|D ]@}|                    |                    dd                     |                    |           AW|dk    r0|D ],}|                    d           |                    |           -|dk    r"|r|d	         d
u r|                    d           |dk    r|D ]}|                    d|z              |dk    rMt          |          d	k    r8dd	                    |          z   }	d|	z   dz   }
|                    d|
z              ,|dk    r#|r|d	         d
u r|                    d           Ut          d          ||z   S )Nz'--NotebookApp.default_url=/notebooks/%sr!   r#   rW   r/   r"   z--ipr%   r   Tz--no-browserr$   z--NotebookApp.base_url=r&   z'self'  z;{ 'headers': { 'Content-Security-Policy': "frame-ancestors z" } }z--NotebookApp.tornado_settings=r'   z!--NotebookApp.trust_xheaders=Truez#unhandled http option for notebooks)ospathbasenamerp   r   	url_quoter4   rX   r2   joinrY   )r6   r7   r8   rZ   filenamedefault_url_argr   r[   r\   	full_listpython_dict_literals              r   rI   z!_NotebookArgsTransformer.add_args   sN    7##DL$9::CiPXFYFYY_%%% ' %	J %	JVV22 #J44  J $ $ALL0CR!H!HIIILLOOOO$ 77 J $ $ALL(((LLOOOO$ :: J 1fQi4/ 1LL000:: J @ @A LL!:Q!>????@ << Jv;;? 	Z !*CHHV,<,< <I*k*3+46=+>'LL!BEX!XYYY<< J FfQi4/ FLL!DEEE"#HIIIt|r   N)rP   rQ   rR   r|   rI   r   r   r   rz   rz      s2          4 4 4 4 4r   rz   c                   ~    e Zd ZdZd
dZed             Zed             Zed             Zed             Z	d Z
d	 ZdS )CommandExecInfoz'Class describing an executable command.Nc                 t    || _         || _        || _        || _        |du st	          |          dk    sJ dS dS )zConstruct a CommandExecInfo.Fr-   N)_cwd_args_shell_envr2   )r6   cwdr8   shellenvr   r   s          r   r|   zCommandExecInfo.__init__   sI    	
	~/Ta///////r   c                     | j         S )z(Working directory to run the command in.)r   r6   s    r   r   zCommandExecInfo.cwd        yr   c                     | j         S )zCommand line argument vector to run the command.

        If the ``shell`` property is True, then pass args[0] as a string to Popen,
        rather than this entire list of args.
        )r   r   s    r   r8   zCommandExecInfo.args   s     zr   c                     | j         S )z2Whether the command should be run with shell=True.)r   r   s    r   r   zCommandExecInfo.shell   s     {r   c                     | j         S )z"Environment to run the command in.)r   r   s    r   r   zCommandExecInfo.env   r   r   c                     | j         r(t          | j                  dk    sJ | j        d         }n| j        }t          j        d|t          j        | j                  | j        | j         d|S )zConvenience method runs the command using Popen.

        Args:
            kwargs: passed through to Popen

        Returns:
            Popen instance
        r-   r   )r8   r   r   r   r   )	r   r2   r   r	   Popenr
   env_without_unicoder   r   )r6   kwargsr8   s      r   popenzCommandExecInfo.popen   s     ; 	 tz??a'''':a=DD :D & 1D+5+I$)+T+T+/9-1[1 1 *0	1 1 	1r   c                    | j                             dd          }|rAt          j        |          }|                                D ]\  }}|| j         vr
|| j         |<   t          | j                  }| j        rct          |          dk    sJ t                      r9t          j        |                                                                            nddg|z   }	 t          j                    }t          j        | j                   t          j                                         t          j                                         t+                                          dd                    |                     t          j        |d         || j                   t          j        |           d	S # t          j        |           w xY w)
zConvenience method exec's the command replacing the current process.

        Returns:
            Does not return. May raise an OSError though.
        CONDA_PREFIXFr-   z/bin/shz-cz$ %sr~   r   N)r   getr   get_env_varsrH   r   r   r   r2   r   sysexitr   waitr   getcwdchdirr   stderrflushstdoutr   infor   execvper   )r6   conda_prefixconda_env_varsrg   r\   r8   old_dirs          r   r   zCommandExecInfo.execvpe  s    x||NE:: 	$&3LAAN&,,.. $ $1 DH$ $"#DHQKDJ; 	0t99>!!!}} 
0 **,,---- "4(4/		ikkGHTYJJ""6388D>>:::JtAwdi000 HWBHWs   B>F2 2G)NN)rP   rQ   rR   rx   r|   propertyr   r8   r   r   r   r   r   r   r   r   r      s        110 0 0 0   X   X   X   X1 1 1<* * * * *r   r   c                     |s| S t                      r!ddlm}m}  ||           } |||z             S | }|D ]}|dz   t	          |          z   }|S )Nr   )windows_split_command_linewindows_join_command_liner~   )r   )anaconda_project.internal.windows_cmdliner   r   r   )rp   rr   r   r   r8   new_commandr9   s          r   ro   ro   K  s     == 		^ ^ ^ ^ ^ ^ ^ ^--g66D,,TJ->???!K! = =)C/%**<r   c                   &   e Zd ZdZd Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zed
             Zed             Zd Zed             Zed             ZddZddZd ZdS )ProjectCommandz+Represents a command from the project file.c                 .    d|v sJ || _         || _        dS )zConstruct a command with the given attributes.

        Args:
            name (str): name of the command
            attributes (dict): named attributes of the command
        r   N)_name_attributes)r6   name
attributess      r   r|   zProjectCommand.__init__]  s*     Z''''
%r   c                     | j         S )zGet name of the command.)r   r   s    r   r   zProjectCommand.nameh  s     zr   c                 \    | j         dup| j        du}| j                            d|          S )z=Can accept the --anaconda-project-* options for HTTP servers.Nr   )r   r   r   r   )r6   defaults     r   r   z$ProjectCommand.supports_http_optionsm  s6     =,Jd0J##$;WEEEr   c                 8    | j                             dd          S )z9Notebook filename relative to project directory, or None.r   Nr   r   r   s    r   r   zProjectCommand.notebooks  s     ##J555r   c                 8    | j                             dd          S )z:Bokeh app filename relative to project directory, or None.r   Nr   r   s    r   r   zProjectCommand.bokeh_appx  s     ##K666r   c                 8    | j                             dd          S )zUnix shell command line string, or None.

        This property is here to support displaying the command in
        a UI, but shouldn't be used to execute the command; use
        ``exec_info_for_environment()`` for executing.
        r   Nr   r   s    r   unix_shell_commandlinez%ProjectCommand.unix_shell_commandline}       ##FD111r   c                 8    | j                             dd          S )zcmd.exe command line string, or None.

        This property is here to support displaying the command in
        a UI, but shouldn't be used to execute the command; use
        ``exec_info_for_environment()`` for executing.
        r   Nr   r   s    r   windows_cmd_commandlinez&ProjectCommand.windows_cmd_commandline  s     ##It444r   c                 8    | j                             dd          S )zArgv to exec directly, or None.

        This isn't allowed in the config file but we do generate
        it on the fly when we run stuff that isn't a configured
        project command.
        r8   Nr   r   s    r   r8   zProjectCommand.args  r   r   c                 8    | j                             dd          S )zConda "app entry" style command line.

        This property is here to support displaying the command in
        a UI, but shouldn't be used to execute the command; use
        ``exec_info_for_environment()`` for executing.
        r   Nr   r   s    r   r   zProjectCommand.conda_app_entry  s     ##$5t<<<r   c                 6    | j                             d          S )zTGet the environment spec name used for this command unless user specified otherwise.r   r   r   s    r   default_env_spec_namez$ProjectCommand.default_env_spec_name  s     ##J///r   c                 &    t                      rdS dS )Nr   r   )r   r   s    r   _shell_fieldzProjectCommand._shell_field  s    == 	96r   c                 d   | j                             dd          }|| j        
d| j        z  }|| j        
d| j        z  }|| j                             dd          }|| j                             dd          }|| j                             dd          }|t	          | dd          }|J |S )	z+Helpful string showing what the command is.r   NzBokeh app %szNotebook %sr   r   r   rp   )r   r   r   r   getattr)r6   r   s     r   r   zProjectCommand.description  s     &**=$?? 	>~ >,t~= 	<} <+dm;  	=*..vt<<K 	@*..y$??K 	H*../@$GGK 	9!$	488K&&&r   c                     t                      }| j                                        D ]}|t          v r| j        |         ||<   |S )zDictionary of extra attributes not covered by other properties.

        These are typically 'plugin specific' (only for notebook, only for bokeh,
        etc.)
        )dictr   keysextra_command_attributes)r6   resultrg   s      r   extraszProjectCommand.extras  sP     !&&(( 	0 	0A,, 0 ,Q/q	r   Nc                 H   |t          |t                    sJ d }d}| j        s/| j        s| j        r!t                                          |          }| j        Xt          j        	                    |d         | j                  }d|g}| j        r"t          |                               |          }| j        Xt          j        	                    |d         | j                  }dd|g}| j        r!t                                          |          }| j        | j        }||||z   }|dfS | j                            |                                 d           }|| j        rt!          ||          g}d}n'|r%d}t#                                          |||          }|| j                            dd           }|c|                                }g }|D ]C}	d|	v r(|	                    dt+          j        |                    }	|                    |	           D|||z   }||fS )	NFPROJECT_DIRzjupyter-notebookbokehserveTr   z	${PREFIX})rk   rl   r   r   r   r)   rL   r   r   r   rz   rU   r8   r   r   r   ro   r^   rw   splitrX   r   environ_get_prefixr4   )
r6   rq   rr   r8   r   r   rp   	app_entryparsedr9   s
             r   _choose_args_and_shellz%ProjectCommand._choose_args_and_shell  sp   AZ
D%A%AAAA) 	Gt} 	G 	G)++:::FFJ= 	W7<< 6FFD&-D) W5d;;JJ:VV
> 	P7<< 6GGDWd+D) P244CCJOO
9 	9D 	 )j(;"&&t'8'8':':DAA 	_T%? 	_6w
KKLDEE 	_E+--@@'S]^^D 	-(,,->EEI - #**! % %C"c) ^!kk+y7ST[7\7\]]KK$$$$ -*,D e}r   c                 
   t          j                    }|ddfD ]}||vrt          d|z            |                     ||          \  }}|dS t          j                            |d         |d         g          }|st                      rg dndg}d}	|                    t          j                  D ]y}
|D ]N}t          j	                            |
|d         |z             }t          j	        
                    |          r|}	 nO|	$t          j	                            |	          |d<    nz| j                            di           }|                                D ]\  }}||vr|||<   t          |d         |||	          S )
a(  Get a ``CommandExecInfo`` ready to be executed.

        Args:
            environ (dict): the environment containing a CONDA_PREFIX, PATH, and PROJECT_DIR
            extra_args (list of str): extra args to append to the command line
        Returns:
            argv as list of strings
        PATHr   z6To get a runnable command for the app, %s must be set.N)r/   z.batz.exer/   r   r   )r   r8   r   r   )r   conda_prefix_variable
ValueErrorr   r   pathsepr   r   r   r   existsabspathr   r   rH   r   )r6   rq   rr   	conda_varr   r8   r   r   
extensions
executablepdirextfpathcmd_varsrg   r\   s                   r   exec_info_for_environmentz(ProjectCommand.exec_info_for_environment  s    355	6 	d 	dD7" d !Y]a!bcccd 33GZHHu 	4
 z 6HII
  	
 2=H----RDJJ

2:..  %  CGLLtAw}==Ew~~e,, %*
   gooj99DGE  #''R88NN$$ 	 	DAq 
7=#9'Y^____r   c                     g }d|j         vrJ| j        d|j         vr|                    d           | j        d|j         vr|                    d           |S )zList packages required by this command which are not in the env spec.

        This is used to be sure if you add a notebook command you depend on
        notebook, and if you add a Bokeh command you depend on bokeh, etc.
        anacondaNr   r   )conda_package_names_setr   r4   r   )r6   r   missings      r   missing_packageszProjectCommand.missing_packagesP  sv      X== 	+~ (gX=].] (w'''} +Zx?_-_ +z***r   rN   )rP   rQ   rR   rx   r|   r   r   r   r   r   r   r   r8   r   r   r   r   r   r   r   r   r   r   r   r   r   [  s       55	& 	& 	&   X F F XF
 6 6 X6 7 7 X7 2 2 X2 5 5 X5 2 2 X2 = = X= 0 0 X0     X2 
 
 X
7 7 7 7r>` >` >` >`@    r   r   )&rx   
__future__r   r   collectionsr   r   r   r   jinja2r   anaconda_project.verboser   anaconda_project.internalr   r	   r
   shlexr   ImportErrorpipesurllib.parser   urllibstandard_command_attributesr   all_known_command_attributesr   r   rS   objectr)   rU   r^   rz   r   ro   r   r   r   r   <module>r      s1   : 9 & & & & & &       " " " " " " 				  



       4 4 4 4 4 4 P P P P P P P P P P   */////// * * *))))))))*J : :=UU , , , :j8["9::h0$77B^BF:H :HIQRkmqIrIrh6==xxHgin?o?oh8$??JkmrAsAsu
. . . . .v . . .b    ,   6 S  S  S  S  S/  S  S  SF8 8 8 8 8/ 8 8 8vj j j j jf j j jZ   I I I I IV I I I I Is!   ? AAA A&%A&