
                            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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ZddiZdZdZdZdZdZdZdZ ej:                  ej<                  j>                        ej@                   G d dejB                                      Z" ej:                  ej<                  jF                  ej<                  jH                        ej@                   G d de"                    Z%y)z@Command to list all PyPI modules installed in an Airflow worker.    )absolute_import)division)unicode_literalsN)environments_util)base)image_versions_util)resource_args)util)logEXAMPLESaG            The following command:

          $ {command} myenv

          runs the "python -m pip list" command on a worker and returns the output.

          The following command:

          $ {command} myenv --tree

          runs the "python -m pipdeptree --warn" command on a worker and returns the
          output.
      workerzairflow-worker   
      g      ?g      ?c                   <    e Zd ZdZeZed        Zd Zd Z	d Z
d Zy)Runz5List all PyPI modules installed in an Airflow worker.c                 Z    t        j                  |d       |j                  dd dd       y )Nzin which to list PyPI modulesz--tree
store_truez        List PyPI packages, their versions and a dependency tree, as displayed by the "python -m pipdeptree --warn" command.
        )defaultactionhelp)r	   AddEnvironmentResourceArgadd_argument)clsparsers     2lib/surface/composer/environments/list_packages.pyArgszRun.ArgsD   s9    ++F,KM 	      c                     ~|S N )selferrorenv_objs      r   ConvertKubectlErrorzRun.ConvertKubectlErrorQ   s
    Lr   c                 `   |j                   j                  }t        j                  |      }d|v}t        j                  ||d       5  	 |j                   j
                  j                  }t        j                  |      }t        j                  t        |      }t        j                  j                  dj                  |             d|dg}	|r|	j                  d       |	j                  dt         dg       |j"                  r|	j                  g d	       n|	j                  g d
       t        j$                  t        j&                  ||	      t        j(                  j                         	 d d d        y # t        j*                  $ r}
| j-                  |
|      d }
~
ww xY w# 1 sw Y   y xY w)Nzno-tty)
pod_substrkubectl_namespacez?Executing within the following Kubernetes cluster namespace: {}execz--stdinz--ttyz--containerz--)python-m
pipdeptree--warn)r*   r+   piplist)out_func)config
gkeClustercommand_utilExtractGkeClusterLocationIdTemporaryKubeconfigsoftwareConfigimageVersionFetchKubectlNamespace	GetGkePodWORKER_POD_SUBSTRr   statusPrintformatappendextendWORKER_CONTAINERtreeRunKubectlCommandAddKubectlNamespaceoutKubectlErrorr%   )r"   argsr$   
cluster_idcluster_location_idttyimage_version
kubectl_nspodkubectl_argses              r   _RunKubectlzRun._RunKubectlU   sg   **J&BB7K
$
C		)	)Z
355BB!77F
$$(JH 	


#	% Y/


g
&],<dCD99


F
G


=
>&&,,ZFWW]]	$/
 
4 && 3&&q'2235
 
s+   F$	D$E77F!
FF!!F$$F-c           	      ~   g }|j                   rd}|j                  d       nd}t        j                  d|g || j	                               }|j
                  st        j                  d      d}d}t        }d }	d	}
|s4d }	 t        j                  |t        j                  t         t              z          t        j                  |j
                  |j                  |j                   ||| j	                         
      }	d	}
|	j"                  }|	j$                  }|j'                  d        |
t(        k(  rt        j                  d      |st+        |t,        z  t.              }nLt        }|D ]/  }t1        j2                  |j4                  r|j4                  nd       1 |d   j6                  dz   }|s4|	r||	j8                  ro|	j8                  j:                  rXt1        j<                  dj?                  |	j8                  j<                               tA        |	j8                  j:                         y y y y #  |
dz  }
Y xY w)Nr,   r-   zpip listzlist-packages)command
subcommand
parametersenvironment_refrelease_trackzBCannot execute subcommand for environment. Got empty execution Id.F   r   )execution_idpod_namepod_namespacenext_line_numberrT   rU   c                     | j                   S r    )
lineNumber)lines    r   <lambda>zRun._RunApi.<locals>.<lambda>   s    DOOr   )keyz*Cannot fetch list-packages command status. zCommand exit code: {})!rA   r>   environments_api_utilExecuteAirflowCommandReleaseTrackexecutionIdr3   ErrorDEFAULT_POLL_TIME_SECONDStimesleeprandomuniformPOLL_JITTER_SECONDSPollAirflowCommandrL   podNamespace	outputEndoutputsortMAX_CONSECUTIVE_POLL_ERRORSminEXP_BACKOFF_MULTIPLIERMAX_POLL_TIME_SECONDSr   r<   contentr\   exitInfoexitCoder#   r=   exit)r"   rF   env_ref
cmd_paramsrR   execute_result
output_end	next_linewait_time_secondspoll_resultcur_consequetive_poll_errorslinesr]   s                r   _RunApizRun._RunApix   s   Jyyj!j*@@'')N %%
N  JI1K#$ e*

nn113FGH	
 ,>>'33#''(55&#++-
 ()$ **
""

3
4 
&)D	D  !MNN 668M
 6D
))DLLDLLb
9 "I((1,	C F {++0D0D0M0M	ii'..{/C/C/I/IJK
;(() 1N+{!*$)$s   B)H2 2H<c                 H   |j                   j                  j                         }t        j                  || j                               }t        j                  |j                  j                  j                        r| j                  ||       y | j                  ||       y )N)rU   )rJ   )CONCEPTSenvironmentParserb   Getrd   image_versions_command_util%IsVersionAirflowCommandsApiCompatibler1   r6   r7   r   rO   )r"   rF   rz   r$   s       r   r   zRun.Run   sy    mm''--/G#''t0024G"HHnn33@@ ll4!
tW%r   N)__name__
__module____qualname____doc__DETAILED_HELPdetailed_helpclassmethodr   r%   rO   r   r   r!   r   r   r   r   =   s4     >-
 
!3F?*B	&r   r   c                       e Zd ZdZd Zy)RunBetaa[  List all PyPI modules installed in an Airflow worker.

  ## EXAMPLES

    The following command:

    {command} myenv

    runs the "python -m pip list" command on a worker and returns the output.

    The following command:

    {command} myenv --tree

    runs the "python -m pipdeptree --warn" command on a worker and returns the
    output.
  c                     |j                   j                  xr  |j                   j                  j                  }|r+t        j                  t        j                  |      dz         S |S )Nz Make sure you have followed https://cloud.google.com/composer/docs/how-to/accessing/airflow-cli#running_commands_on_a_private_ip_environment to enable access to your private Cloud Composer environment from your machine.)r1   privateEnvironmentConfigenablePrivateEnvironmentr3   rf   six	text_type)r"   r#   r$   
is_privates       r   r%   zRunBeta.ConvertKubectlError   sb    // 	I//HH  
--
 
 Lr   N)r   r   r   r   r%   r!   r   r   r   r      s    $
r   r   )&r   
__future__r   r   r   rj   rh   googlecloudsdk.api_lib.composerr   rb   googlecloudsdk.callioper   #googlecloudsdk.command_lib.composerr   r   r	   r
   r3   googlecloudsdk.corer   r   r   r:   r@   rg   rr   ru   rt   rl   ReleaseTracksrd   GADefaultUniverseOnlyCommandr   BETAALPHAr   r!   r   r   <module>r      s   G &  '   V ( b = D # 
 	
$  #        D%%(()C&$,, C&  *C&L D%%**D,=,=,C,CDc   Er   