a
   h(G                	   @   s  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 ddlmZ ddl	m
Z
 dd	l	mZ dd
l	mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ e eZh dZd)ddZdd Zdd Zd*ddZdd Zdd Zd+dd Zd!d" Zd#d$ Z d%d& Z!d,d'd(Z"dS )-    N   )errors   )config)parallel)Environment)LABEL_CONFIG_FILES)LABEL_ENVIRONMENT_FILE)LABEL_WORKING_DIR)Project)
get_client)load_context)make_context)	UserError>   ZpspausetopstopZeventsstartZrmZlogsexecZrestartkillZunpausec           	      C   s   |pi }t |}|d}t|p$| |}|dd tv |_t| d }|dd }|rrt|}|srtd	||dd }|d ur|rtd|
d}t|||}t| t|||d|d	||||d
 |t||d
S )N
--env-fileZCOMMANDz	--contextzContext '{}' not foundz--hostzL-H, --host and -c, --context are mutually exclusive. Only one should be set.=z--project-namez	--verbose--no-interpolate)project_nameverbosecontextenvironmentoverride_dirinterpolateenvironment_fileenabled_profiles)get_project_dirgetr   from_env_fileSILENT_COMMANDSZsilentset_parallel_limitr   r   formatlstripr   get_projectget_config_path_from_optionsget_profiles_from_options)	project_diroptionsadditional_optionsr   r   r   r   Zcontext_namehost r/   7/usr/lib/python3.9/site-packages/compose/cli/command.pyproject_from_options$   s@    


r1   c              
   C   sf   |  d}|rbzt|}W n( tyB   td|  dY n0 |dkrVtdtj| d S )NZCOMPOSE_PARALLEL_LIMITz7COMPOSE_PARALLEL_LIMIT must be an integer (found: "{}")r   z-COMPOSE_PARALLEL_LIMIT can not be less than 2)	r"   int
ValueErrorr   r   r&   r   ZGlobalLimitZset_global_limit)r   Zparallel_limitr/   r/   r0   r%   J   s    


r%   c                 C   sB   d }t | tj}|r4|d dkr$dS tj|d }| dp@|S )Nr   -.z--project-directory)r)   osenvironpathdirnamer"   )r,   r   filesr/   r/   r0   r!   Z   s    r!   c                 C   sT   |pi }t |}|d}t|p$| |}t||}tt| ||||d S )Nr   r   )r!   r"   r   r#   r)   r   loadfind)base_dirr,   r-   r   r   r   config_pathr/   r/   r0   get_config_from_optionsd   s    


r?   c                 C   sL   dd }|  d}|r||S | d}|rH| dtj}|||S d S )Nc                 S   s   dd | D S )Nc                 S   s$   g | ]}t |tr|d n|qS )zutf-8)
isinstancebytesdecode).0pr/   r/   r0   
<listcomp>r       zGget_config_path_from_options.<locals>.unicode_paths.<locals>.<listcomp>r/   )pathsr/   r/   r0   unicode_pathsq   s    z3get_config_path_from_options.<locals>.unicode_pathsz--fileZCOMPOSE_FILEZCOMPOSE_PATH_SEPARATOR)r"   r6   pathsepsplit)r,   r   rH   Zfile_optionconfig_filesrI   r/   r/   r0   r)   p   s    

r)   c                 C   s.   |  d}|r|S | d}|r*|dS g S )Nz	--profileZCOMPOSE_PROFILE,)r"   rJ   )r,   r   Zprofile_optionZprofilesr/   r/   r0   r*      s    


r*   FTc
              
   C   s   |st | }t| |||}
t|
j||}t|
|}|d}t||||d}t	
|0 t||||dt|
||	W  d    S 1 s0    Y  d S )NZCOMPOSE_API_VERSION)r   versionr   r   ZDOCKER_DEFAULT_PLATFORM)r   r#   r   r<   get_project_nameworking_dirr;   r"   r   r   Zhandle_connection_errorsr   Zfrom_configexecution_context_labels)r+   r>   r   r   r   r   r   r   r   r    config_detailsZconfig_dataapi_versionZclientr/   r/   r0   r(      s(    

r(   c                 C   s\   d ttj| jg}t| s6|d tt	|  |d urX|d t
tj| |S )Nz{}={})r&   r
   r6   r8   abspathrO   use_config_from_stdinappendr   config_files_labelr	   normpath)rQ   r   Zextra_labelsr/   r/   r0   rP      s    
rP   c                 C   s   | j D ]}|js dS qdS )NTF)rK   filename)rQ   cr/   r/   r0   rT      s    
rT   c                 C   s   d dd | jD S )NrL   c                 s   s   | ]}t j|jV  qd S )N)r6   r8   rW   rX   )rC   rY   r/   r/   r0   	<genexpr>   s   z%config_files_label.<locals>.<genexpr>)joinrK   )rQ   r/   r/   r0   rV      s    
rV   c                 C   sT   dd }|st | }|p"|d}|r0||S tjtj| }|rP||S dS )Nc                 S   s   t dd|  S )Nz[^-_a-z0-9] )resublower)namer/   r/   r0   normalize_name   s    z(get_project_name.<locals>.normalize_nameZCOMPOSE_PROJECT_NAMEdefault)r   r#   r"   r6   r8   basenamerS   )rO   r   r   ra   projectr/   r/   r0   rN      s    
rN   )N)N)	NNFNNNTNN)NN)#Zloggingr6   r]   r\   r   r   r   Zconfig.environmentr   constr   r	   r
   rd   r   Zdocker_clientr   r   r   r   Z	getLogger__name__logr$   r1   r%   r!   r?   r)   r*   r(   rP   rT   rV   rN   r/   r/   r/   r0   <module>   s<   

&

   
