
                         J   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKJ r    " S S\RB                  5      r! " S S\!5      r" " S S\!5      r# " S S\!5      r$ " S S\!5      r%S r&  S:S jr'S;S jr(S r)S<S jr* " S  S!\RV                  " \RX                  \-5      5      r. " S" S#\-5      r/ " S$ S%\.5      r0 " S& S'\-5      r1 " S( S)\.5      r2 " S* S+\-5      r3 " S, S-\.5      r4 " S. S/\RV                  " \RX                  \-5      5      r5 " S0 S1\55      r6 " S2 S3\55      r7 " S4 S5\.5      r8\0\2\4S6.r9   S=S7 jr:        S>S8 jr;  S?S9 jr<g)@a1  gcloud CLI tree generators for non-gcloud CLIs.

A CLI tree for a supported command is generated by using the root command plus
`help` or `--help` arguments to do a DFS traversal. Each node is generated
from a man-ish style runtime document.

Supported CLI commands have their own runtime help document quirks, so each is
handled by an ad-hoc parser. The parsers rely on consistency within commands
and between command releases.

The CLI tree for an unsupported command is generated from the output of
`man the-command` and contains only the command root node.
    )absolute_import)division)unicode_literalsN)cli_tree)generate)lookup)
exceptions)log)requests)progress_tracker)resource_printer)encoding)files)rangec                       \ rS rSrSrSrg)Error:   zExceptions for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       9lib/googlecloudsdk/command_lib/meta/generate_cli_trees.pyr   r   :   s    #r   r   c                       \ rS rSrSrSrg)CommandInvocationError>   zCommand could not be invoked.r   Nr   r   r   r   r   r   >   s    %r   r   c                       \ rS rSrSrSrg)NoCliTreeForCommandErrorB   z!Command does not have a CLI tree.r   Nr   r   r   r   r"   r"   B   s    )r   r"   c                       \ rS rSrSrSrg)CliTreeGenerationErrorF   z'CLI tree generation failed for command.r   Nr   r   r   r   r%   r%   F   s    /r   r%   c                       \ rS rSrSrSrg)NoManPageTextForCommandErrorJ   z(Could not get man page text for command.r   Nr   r   r   r   r(   r(   J   s    0r   r(   c                 x    [         R                  " SS[        R                  " U 5      5      R	                  S5      $ )zEReturns text dedented and multiple non-indent spaces replaced by one.z
([^ ])   *z\1 
)resubtextwrapdedentstrip)texts    r   _NormalizeSpacer2   N   s)    	fhood&;	<	B	B4	HHr   c	                    [         R                  0 [         R                  U[         R                  U[         R                  [        U5      [         R                  S[         R                  U[         R                  S[         R                  U[         R                  U [         R                  U=(       d
    US:X  a  SOS[         R                  U[         R                  U0$ )z)Initializes and returns a flag dict node. Fbool01)r   LOOKUP_ATTRLOOKUP_CATEGORYLOOKUP_DEFAULTLOOKUP_DESCRIPTIONr2   LOOKUP_GROUPLOOKUP_IS_GLOBALLOOKUP_IS_HIDDENLOOKUP_IS_REQUIREDLOOKUP_NAMELOOKUP_NARGSLOOKUP_VALUELOOKUP_TYPE)	namedescriptionvaluedefaulttype_category	is_globalis_requirednargss	            r   _FlagrM   S   s     Bw!!?;#?R!!;DUGevos3UE
 r   c                     [         R                  U[         R                  [        U5      [         R                  U [         R
                  U0$ )z/Initializes and returns a positional dict node.)r   r:   r;   r2   r@   rA   )rD   rE   rG   rL   s       r   _PositionalrO   f   s?     w!!?;#?DU	
 r   c                 F   [         R                  S[         R                  0 [         R                  0 [         R                  0 [         R
                  S[         R                  S[         R                  U [         R                  / [         R                  S[         R                  0 0
$ )z2Initializes and returns a command/group dict node.r4   FGA)r   LOOKUP_CAPSULELOOKUP_COMMANDSLOOKUP_FLAGSLOOKUP_GROUPSLOOKUP_IS_GROUPr>   LOOKUP_PATHLOOKUP_POSITIONALSLOOKUP_RELEASELOOKUP_SECTIONSpaths    r   _Commandr]   p   s|     rRbD!!2t
 r   c                 v   / nU (       a  UR                  U 5        U$  UR                  [        R                  " 5       5        UR                  [        R                  " 5       5        U$ ! [        R                   a<  nU(       d  e [        R
                  " [        R                  " U5      5         SnANqSnAff = f)a  Returns the list of directories to search for CLI trees.

Args:
  directory: The directory containing the CLI tree JSON files. If None
    then the default installation and config directories are used.
  warn_on_exceptions: Emits warning messages in lieu of exceptions.
N)	appendr   
CliTreeDirSdkRootNotFoundErrorr
   warningsix	text_typeCliTreeConfigDir)	directorywarn_on_exceptionsdirectorieses       r   _GetDirectoriesrj      s     +y! 
$,,./
 x0023	 (( $	kk#--"##$s   $A( (B8<2B33B8c                       \ rS rSrSrSr\S 5       r\S 5       r\S 5       r	SS jr
S rS	 r\R                  S
 5       rS rSS jr   SS jrSrg)CliTreeGenerator   z]Base CLI tree generator.

Attributes:
  command_name: str, The name of the CLI tree command.
Nc                 @    U(       a  [        5       U l        gSU l        g)zCMemoizes failed attempts and doesn't repeat them if enable is True.N)set	_FAILURES)clsenables     r   MemoizeFailures CliTreeGenerator.MemoizeFailures   s     $CECMCMr   c                 D    U R                   (       a  XR                   ;   $ S$ )z<Returns True if man page request for command already failed.F)rp   rq   commands     r   AlreadyFailedCliTreeGenerator.AlreadyFailed   s     (+}}7mm#?%?r   c                 V    U R                   b  U R                   R                  U5        gg)z1Add command to the set of failed man generations.N)rp   addrv   s     r   
AddFailureCliTreeGenerator.AddFailure   s%     }} 	mm  !r   c                 P   U(       aS  [         R                  " [        R                  5       n [        R
                  " X#UUS9R                  5          SSS5        Xl	        U=(       d    U/U l
        SU l        g! [         a  n[        U5      eSnAff = f! , (       d  f       NJ= f)ai  Initializes the CLI tree generator.

Args:
  command_name: str, The name of the CLI tree command (e.g. 'gsutil').
  root_command_args: [str], The argument list to invoke the root CLI tree
    command. Examples:
    * ['gcloud']
    * ['python', '/tmp/tarball_dir/gsutil/gsutil']
Raises:
  CommandInvocationError: If the provided root command cannot be invoked.
)stdinstdoutstderrN)r   
FileWriterosdevnull
subprocessPopencommunicateOSErrorr   command_name_root_command_args_cli_version)selfr   root_command_argsr   ri   s        r   __init__CliTreeGenerator.__init__   s     BJJ'7	* 

,G")++6;= ( %/AL>DD  	*&q)
)	* ('s(   B$A::
BBBB
B%c                 p    [         R                  " [        R                  " U R                  U-   5      5      $ )Runs the root command with args given by cmd and returns the output.

Args:
  cmd: [str], List of arguments to the root command.
Returns:
  str, Output of the given command.
)r   Decoder   check_outputr   )r   cmds     r   RunCliTreeGenerator.Run   s0     ?? 7 7# =>@ @r   c                     U R                   (       d5   U R                  S/5      R                  5       S   U l         U R                   $ U R                   $ !   [        R                  U l          U R                   $ = f)Returns the CLI_VERSION string.version)r   r   splitr   CLI_VERSION_UNKNOWNr   s    r   
GetVersionCliTreeGenerator.GetVersion   sk    9 HHi[1779"= 49$88s   (A A7c                     g)z(Generates and returns the CLI tree dict.Nr   r   s    r   GenerateCliTreeGenerator.Generate        r   c                    U=(       d	    [        SS9 HR  n[        R                  R                  U=(       d    SU R                  5      S-   n U[
        R                  " U5      4s  $    WS4$ ! [
        R                   a     Mq  f = f)zEReturns (path,f) open for read for the first CLI tree in directories.T)rg   ..jsonN)rj   r   r\   joinr   r   
FileReaderr   )r   rh   rf   r\   s       r   FindTreeFileCliTreeGenerator.FindTreeFile   s}     LOt$LL	WW\\)*sD,=,=>HdU%%d+++ M : [[ s   A--BBc                    UR                  [        R                  5      nU[        R                  :H  nUR                  [        R                  5      nU[        R
                  :w  a  US4$ U R                  5       nU(       a  OU[        R                  :X  a  O	X6:w  a  US4$ U(       a9  [        R                  R                  SR                  U R                  U5      5        US4$ )z,Returns a bool tuple (readonly, up_to_date).Fz)[{}] CLI tree version [{}] is up to date.T)getr   LOOKUP_CLI_VERSIONCLI_VERSION_READONLYLOOKUP_VERSIONVERSIONr   r   r
   statusPrintformatr   )r   treeverboseactual_cli_versionreadonlyactual_tree_versionexpected_cli_versions          r   
IsUpToDateCliTreeGenerator.IsUpToDate   s     ("="=>!X%B%BBH ((8#:#:;h...u_  ??,
	!=!=	=
		3u_ 	jjBII


/1 2T>r   c                 B  ^ ^^ Sn T R                  U5      u  mnU(       a  Sn	 [        R                  " U5      n
U
(       a|  T R	                  XS9u  pU(       a  U
U(       a  UR                  5         $ $ U	(       a"  U(       d  U
U(       a  UR                  5         $ $ O!U(       a   U(       a  UR                  5         ggU(       a  UR                  5         UU U4S jnU(       d$  [        SR                  T R                  5      5      eU(       d  U" 5       $ [        R                  " SR                  U(       a  SOST R                  5      5         U" 5       sSSS5        $ ! [         a    Sn
 GN4f = f! U(       a  UR                  5         f f = f! , (       d  f       g= f)	zFLoads the CLI tree or generates it if necessary, and returns the tree.NF)r   c                  L  > TR                  5       n U (       a:   [        R                  " T5      nU   [        R                  " U SUS9  SSS5        U $ U $ ! [        R                   a  n[        R
                  R                  T5      u  p4 [        R                  " U5        [        R                  " T5      n SnAN! [        R                   a9    T(       d  e [        R                  " [        R                  " U5      5          SnAgf = fSnAff = f! , (       d  f       U $ = f)z>Helper that generates a CLI tree and writes it to a JSON file.Njson)print_formatout)r   r   r   r   r   r\   r   MakeDirr
   rb   rc   rd   r   r   )r   fri   rf   _r\   r   rg   s        r   	_Generate2CliTreeGenerator.LoadOrGenerate.<locals>._Generate'  s    ]]_d		t$! 

 
 F
B kTk {{ 
	t,,)MM)$  &A %KKa()	
	 QksA   A DD)!D,B<<AD	DD		DD
D#zNo CLI tree for [{}].z{} the [{}] CLI treeUpdating
Generating)r   r   load
ValueErrorr   closer"   r   r   r   ProgressTracker)r   rh   forcer   ignore_out_of_datetarballr   rg   r   
up_to_dater   r   r   r\   s   `      `     @r   LoadOrGenerateCliTreeGenerator.LoadOrGenerate  s`    	A!!+.gdA	

	1$ !%!G
( 
		 
  
		 
 !	
		 
		4 $
!
(
():):
;= =[		)	)%%Jt/@/@	B
C [
C 
C_  	$	 
		 
D
C 
CsE   E3 E   E3 4E3 E3 F E0,E3 /E00E3 3F
F)r   r   r   NF)NFTFFFF)r   r   r   r   r   rp   classmethodrs   rx   r|   r   r   r   abcabstractmethodr   r   r   r   r   r   r   r   rl   rl      s     ). . @ @ ! !
4	@  8 DHFK(-=r   rl   c                   (    \ rS rSrSrS rSS jrSrg)_BqCollectoriL  z#bq help document section collector.c                 Z    UR                  S5      U l        SU l        S U l        SU l        g Nr+   DESCRIPTIONFr   r1   heading	lookaheadignore_trailerr   r1   s     r   r   _BqCollector.__init__O  (    

4 DI DLDNDr   c                    / nU R                   (       a)  U(       d  UR                  U R                   5        SU l         U R                  nSU l        U R                  (       a{  U R                  R	                  S5      nUR                  S5      (       d  U(       d0  U R                  (       d  UR                  UR                  5       5        U R                  (       a  M{  U(       a.  US   (       d$  UR	                  S5        U(       a  US   (       d  M$  U(       a-  US   (       d#  UR	                  5         U(       a  US   (       d  M#  SU l        X24$ )0Returns the heading and content lines from text.Nr    r   T)r   r_   r   r1   pop
startswithr   rstripr   strip_headingscontentr   lines        r   Collect_BqCollector.CollectU  s    G~~t~~&dnllGDL
))YY]]1d			^D<O<Ot{{}% ))) '!*kk!n '!**
'"+kkm '"++Dr   r   r   r   r1   Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   L  s    + r   r   c                   4    \ rS rSrSrS rS	S jrS rS rSr	g)
BqCliTreeGeneratorij  zbq CLI tree generator.c                     [         R                  " U R                  U-   5      n[        R                  " U5      R                  SS5      $ ! [         R                   a'  nUR                  S:w  a  e UR
                  n SnAN\SnAff = f)r      Nzbq.pybq)	r   r   r   CalledProcessError
returncodeoutputr   r   replacer   r   r   ri   s       r   r   BqCliTreeGenerator.Runm  sp    &&t'>'>'DEf ??6"**7D99 (( 	
	xxf	s   #A BBBc           
         U(       GaH  UR                  S5      nUR                  5       R                  SS5      u  pVUR                  5       /nSnU(       a  US   R                  S5      (       dn  UR                  S5      R                  5       nUR                  S5      (       a  USS nOUR	                  U5        U(       a  US   R                  S5      (       d  Mn  S	R                  U5      R                  5       nUR                  S
5      (       a  SUSS -   nSn	Sn
OSn
Sn	[        UUU	U
USUS9U[        R                     U'   U(       a  GMG  gg)'Adds flags in content lines to command.r   :r   r4   z  --z
(default: 
   r   r   z--[no]--   Nr5   VALUEstringFrD   rE   rH   rF   rG   rK   rJ   )	r   r0   r   r   r_   r   rM   r   rT   )r   rw   r   rJ   r   rD   rE   	paragraphrG   rH   rF   s              r   AddFlagsBqCliTreeGenerator.AddFlags~  s2   
[[^d**,,,S!4d$$&'igGAJ11&99{{1~##%??<((BK'


4
  GAJ11&99 HHY'--/k		"	"d12h-2!.gh##$T*' 'r   c                    [        U5      nSU[        R                  '   U R                  S/USS -   5      nUR	                  S5      nU(       Ga  UR                  S5      nU(       a  US   R                  5       (       d  M:  UR	                  SS5      u  pcUR                  5       /n/ n/ n	Un
U(       a  US   (       a  US   S   R                  5       (       dr  UR                  S5      R                  5       nUS:X  a  U	n
OUS	:X  a  Un
OU
R                  U5        U(       a)  US   (       d  MU  US   S   R                  5       (       d  Mr  [        X/-   5      nX[        R                     U'   U(       a%  SR                  U5      U[        R                     S
'   U(       a%  SR                  U5      U[        R                     S'   U(       a  GM  U$ )5Generates and returns the CLI subtree rooted at path.Thelpr   Nr+   r   r   z
Arguments:z	Examples:r   EXAMPLES)r]   r   rV   r   r   r   islowerr0   r_   rS   r   rZ   )r   r\   rw   r1   r   r   rD   rE   examples	argumentsr  
subcommands               r   SubTreeBqCliTreeGenerator.SubTree  s   tnG(,GH$$%88VHtABx'(D jjG
[[^da**::c1%jdZZ\NkhiiWQZwqz!}/D/D/F/F{{1~##%<)[ )


4
  WQZZwqz!}/D/D/F/F D6M*j0:h&&'-	>Bii?
8++,];	;?99<
8++,Z8/ '4 Nr   c                 >   U R                  U R                  /5      nU R                  S/5      n[        U5      nUR	                  SS9u  pEU R                  XSS9  U R                  5       U[        R                  '   [        R                  U[        R                  '   U$ )?Generates and returns the CLI tree rooted at self.command_name.--helpTr   rJ   )r  r   r   r   r   r  r   r   r   r   r   r   r   r1   	collectorr   r   s         r   r   BqCliTreeGenerator.Generate  s     <<**+,D 88XJDT"I""$"7JAMM$4M0 )-(9D	$	$%$,$4$4D	 	 !Kr   r   Nr   )
r   r   r   r   r   r   r  r  r   r   r   r   r   r   r   j  s    :">#Jr   r   c                   N    \ rS rSrSr\" \" S5      5      u  rrr	r
S rSS jrSrg)	_GsutilCollectori  z'gsutil help document section collector.   c                 `    UR                  S5      U l        SU l        U R                  U l        g )Nr+   CAPSULE)r   r1   r   UNKNOWN	page_typer   s     r   r   _GsutilCollector.__init__  s$    

4 DIDL\\DNr   c                 D   / nU R                   nSU l         U R                  (       Ga  U R                  R                  S5      nU R                  U R                  :X  a^  UR                  S5      (       a  U R                  U l        Mp  US:X  a  U R                  U l        SnM  UR                  S5      (       d  M  GO+U R                  U R                  :X  a2  US:X  a  SnM  US	:X  a	  S
U l         GOHUR                  S5      (       d  M  OU R                  U R                  :X  a  US:X  a  U R                  U l        SU l         OUS:X  a  X@l         OUR                  S5      (       a  SU l         OU(       a3  US   R                  5       (       a  UR                  SS5      S   U l         OO@U R                  U R                  :X  a&  U(       a  US   R                  5       (       a  X@l         OPUR                  S5      (       d  U(       d  UR                  UR                  5       5        U R                  (       a  GM  U(       a.  US   (       d$  UR                  S5        U(       a  US   (       d  M$  U(       a-  US   (       d#  UR                  5         U(       a  US   (       d  M#  X24$ )r   Nr   Usage:NAMEr  r   zAvailable commands:COMMANDSzAdditional help topics:TOPICSOVERVIEWr   SYNOPSISOPTIONSFLAGSr   r   )r   r1   r   r   r  r   ROOTMANTOPICendswithisupperr   r_   r   r   s        r   r   _GsutilCollector.Collect  s   GllGDL
)))YY]]1d	4<<	'??8$$99$.
V^88$.'
%%
 &>>TYY&(('
..!$,
%%
 &>>TXX%:::$.&$,
Z,
]]9%% $,
d1goo''C+B/$,
>>TZZ'DGOO%%,
			^t{{}%Y )))Z '!*kk!n '!**
'"+kkm '"++r   )r   r   r1   Nr   )r   r   r   r   r   listr   r  r+  r,  r-  r   r   r   r   r   r   r  r    s%    /"58n'4e"
6r   r  c                   H   ^  \ rS rSrSrU 4S jrS rS	S jrS rS r	Sr
U =r$ )
GsutilCliTreeGeneratori  zgsutil CLI tree generator.c                 <   > [         [        U ]
  " U0 UD6  / U l        g r   )superr3  r   topics)r   argskwargs	__class__s      r   r   GsutilCliTreeGenerator.__init__  s    	
 $0$A&ADKr   c                      [         R                  " U R                  U-   5      n[        R                  " U5      $ ! [         R                   a'  nUR                  S:w  a  e UR
                  n SnANLSnAff = f)r   r   N)r   r   r   r   r   r   r   r   r   s       r   r   GsutilCliTreeGenerator.Run  sc    &&t'>'>'DEf ??6"" (( 	
	xxf	s   #; A6A11A6c                   ^^ UU4S jn[         R                  " S5      nSn/ nU H  nUR                  S5      (       ac  U(       a  U" USR                  U5      5        UR	                  U5      n	U	R                  S5      nU	R                  S5      R                  5       /nM|  [        U5      S:  d  M  UR                  USS R                  5       5        M     U(       a  U" USR                  U5      5        gg)	r   c           
      X   > SnSnSn[        U UUUUSTS9T[        R                     U '   g )Nr4   r5   Fr  )rM   r   rT   )rD   rE   rF   rH   rG   rw   rJ   s        r   _Add-GsutilCliTreeGenerator.AddFlags.<locals>._Add3  sA    eeg-2!.gh##$T*r   z *((-[^ ]*,)* *(-[^ ]*) *)(.*)Nz  -r+      r     )	r,   compiler   r   matchgroupr   lenr_   )
r   rw   r   rJ   r?  parserD   rE   r   rD  s
    ` `      r   r  GsutilCliTreeGenerator.AddFlags0  s     JJ78EDK			
tTYY{+
,D!{{1~{{1~,,./t9r>49++-.  
4;'( r   c                    [        U5      n[        U5      S:  =(       a    US   S:H  nU(       a  USS nO	USS S/-   nU R                  U5      n[        U5      n UR	                  5       u  pxU(       d   U$ US:X  a@  U(       a7  US   R                  SS5      S   R                  5       U[        R                  '   GOUS	:X  ak  U(       a  Mr  U HZ  n	 U	R                  5       S   n
U
S
:X  a  M  SU[        R                  '   U R                  X/-   5      U[        R                     U
'   M\     GOUS:X  a  U R                  X(5        GOUS:X  a  / nU H  n	U	(       d    OU	R                  5       n[        U5      [        U5      ::  a  M6  US[        U5       U:X  d  MJ  U[        U5         n
U
S   R                  5       (       d  Mr  U
S;  d  Mz  UR                  U
5        M     [        U5      S:  aD  SU[        R                  '   U H+  n
U R                  X/-   5      U[        R                     U
'   M-     OUS:X  a7  U H0  n	 U R                   R                  U	R                  5       S   5        M2     OfUR#                  5       (       aQ  UR%                  5       US   :X  a  SnSR'                  U V	s/ s H  oSS PM	     sn	5      U[        R(                     U'   GM  ! [         a     GM  f = f! [         a     M  f = fs  sn	f )r	  r   r
  Nr  Tr  r   -r%  updater*  r(  )offonfalsetruer&  r   r   r+      )r]   rF  r   r  r   r   r0   r   rR   
IndexErrorrV   r  rS   r  r  r_   r6  r/  lowerr   rZ   )r   r\   rw   is_help_commandr   r1   r  r   r   r   rD   commandss               r   r  GsutilCliTreeGenerator.SubTreeP  s   tnG$i!m9Q6(9OHcHz!c88C=D &I
"**,g^ N] i-4QZ-=-=c1-Ea-H-N-N-P'())
*j 
D::<?D X.2'(**
+48LL4O'(**
+D
1  gg'j D

#XT"#d)_$s4y>DAw  T1O%Oood#  x=1.2'(**
+d6:llv7GH,,-d3  hDKKtzz|A/ 
 ??==?d2h&!'59YY")*'$!"X'*6,(()'2_   :   +s*   K,K-/K>
K*)K*-
K;:K;c                 P   U R                  U R                  /5      nU R                  SS/5      n[        U5      n UR	                  5       u  pEU(       d  OUS:X  a  U R                  XSS9  M2  U[        R                     S   nSU[        R                  '   U R                   H=  nU R                  U[        R                     U/-   5      U[        R                     U'   M?     U R                  5       U[        R                  '   [        R                  U[        R                  '   U$ )r  r
  optionsTr*  r  )r  r   r   r  r   r  r   rS   rV   r6  rW   r   r   r   r   )r   r   r1   r  r   r   help_commandtopics           r   r   GsutilCliTreeGenerator.Generate  s   <<**+,D 88VY'(D &I
"**,g	G	dt4  001&9L-1L))*6:ll
x++
,w
678l8++,U3 
 )-(9D	$	$%$,$4$4D	 	 !Kr   )r6  r   )r   r   r   r   r   r   r   r  r  r   r   __classcell__r9  s   @r   r3  r3    s&    "#")@=~ r   r3  c                   (    \ rS rSrSrS rSS jrSrg)_KubectlCollectori  z(Kubectl help document section collector.c                 Z    UR                  S5      U l        SU l        S U l        SU l        g r   r   r   s     r   r   _KubectlCollector.__init__  r   r   c                    / nU R                   (       a)  U(       d  UR                  U R                   5        SU l         U R                  nSU l        U R                  (       Ga  U R                  R	                  S5      nSnUR                  U5      (       a1  U[        U5      S R                  5       nU(       a  X@l         SU l        OUR                  S5      (       a*  SU;   a  SU l        O}SU;   a  S	U l        OoS
U;   a  SU l        OaUR                  S5      (       d  U(       d0  U R                  (       d  UR                  UR                  5       5        U R                  (       a  GM  U(       a.  US   (       d$  UR	                  S5        U(       a  US   (       d  M$  U(       a-  US   (       d#  UR	                  5         U(       a  US   (       d  M#  SU l	        X24$ )r   Nr   r#  USAGEr   Commandsr%  Examplesr  Optionsr*  r   r   T)r   r_   r   r1   r   r   rF  r0   r.  r   r   )r   r   r   r   r   usages         r   r   _KubectlCollector.Collect  s`   G~~t~~&dnllGDL
)))YY]]1de			CJK &&(.	s		#$,
#$,
 $,
			^D<O<Ot{{}%) )))* '!*kk!n '!**
'"+kkm '"++Dr   r   Nr   r   r   r   r   r^  r^    s    0 #r   r^  c                   4    \ rS rSrSrS	S jrS rS rS rSr	g)
KubectlCliTreeGeneratori  zkubectl CLI tree generator.c                    U H  nUR                  5       R                  SS5      u  pVUR                  S5      S   nUR                  S5      u  pU	S;   a  Sn	Sn
OS	n	S
n
Sn[        UUU
U	USUS9U[        R                     U'   M     g)r   r   r   , r   =)rO  rN  r4   r5   r  r  Fr  N)r0   r   rM   r   rT   )r   rw   r   rJ   r   flagsrE   flagrD   rF   rH   rG   s               r   r   KubectlCliTreeGenerator.AddFlags  s    ::<--c15e[[r"dJJsOkd	#	#g-2!.gh##$T* r   c                    [        U5      nU R                  USS S/-   5      n[        U5      n UR                  5       u  pVU(       d   U$ US:X  aY  U HR  n UR	                  5       S   nSU[        R                  '   U R                  X/-   5      U[        R                     U'   MT     OCUS;   a&  SR                  U5      U[        R                     U'   OUS	:X  a  U R                  X&5        M  ! [
         a     M  f = f)
r	  r   Nr  Tr%  r   )r   r  r+   r*  )r]   r   r^  r   r   rQ  r   rV   r  rS   r   rZ   r  )	r   r\   rw   r1   r  r   r   r   rD   s	            r   r  KubectlCliTreeGenerator.SubTree  s   tnG88DHz)*D!$'I
"**,g N j D::<?D /3'(**
+48LL4O'(**
+D
1  1159YYw5G(()'2gg'   s   C00
C>=C>c                 $   U R                   (       dM   U R                  SS/5      n[        R                  " SU5      nUR	                  S5      U l         U R                   $ U R                   $ !   [
        R                  U l          U R                   $ = f)r   r   z--clientzGitVersion:"([^"]*)"r   )r   r   r,   searchrE  r   r   )r   verbose_versionrD  s      r   r   "KubectlCliTreeGenerator.GetVersion  s~    9((Iz#:;		0/B!KKN 49$88s   A A+ +Bc                 `   U R                  U R                  /5      nU R                  S/5      n[        U5      nUR	                  SS9u  pEUR                  S5        U R                  XSS9  U R                  5       U[        R                  '   [        R                  U[        R                  '   U$ )r  rW  Tr  z*  --help=true: List detailed command help.r  )r  r   r   r^  r   r_   r  r   r   r   r   r   r  s         r   r    KubectlCliTreeGenerator.Generate  s     <<**+,D 88YK D!$'I""$"7JANN?@MM$4M0 )-(9D	$	$%$,$4$4D	 	 !Kr   )r   Nr   )
r   r   r   r   r   r  r  r   r   r   r   r   r   ri  ri    s    #.0	r   ri  c                       \ rS rSrSrS r\\R                  S 5       5       r	\R                  S 5       r
\R                  S 5       rS rSrg	)
_ManPageCollectori+  an  man page help document section collector base class.

Attributes:
  command_name: The man page command name.
  content_indent: A string of space characters representing the indent of
    the first line of content for any section.
  heading: The heading for the next call to Collect().
  text: The list of man page lines.
  version: The collector CLI_VERSION string.
c                 t    Xl         S U l        S U l        U R                  5       R	                  S5      U l        g )Nr+   )r   content_indentr   GetManPageTextr   r1   )r   r   s     r   r   _ManPageCollector.__init__7  s2    $DDL##%++D1DIr   c                     g)r   Nr   rq   s    r   r   _ManPageCollector.GetVersion=  s     r   c                     g)Returns the raw man page text.Nr   r   s    r   _GetRawManPageText$_ManPageCollector._GetRawManPageTextC  r   r   c                     g)'Returns the preprocessed man page text.Nr   r   s    r   r|   _ManPageCollector.GetManPageTextH  r   r   c                    / nU R                   nSU l         U R                  (       Ga  U R                  R                  S5      nU(       d
  US:X  a  UnM>  U(       d  GOaUS   S:X  a  U R                  (       d  [        R
                  " SSU5      U l        [        U5      [        U R                  5      :  a  U[        U R                  5         nUR                  U R                  5      (       d  SUR                  5       -   nOUS:X  a*  US	:X  a$  U R                  R                  SU5        S
U l         OUS
:X  a*  US;  a$  U R                  R                  SU5        SU l         OO[US;   a  X0l         OS
U;   d  SU;   a  S
U l         OmU(       a3  US   R                  5       (       a  UR                  SS5      S   U l         O3UR                  UR                  5       5        U R                  (       a  GM  U(       a.  US   (       d$  UR                  S5        U(       a  US   (       d  M$  U(       a-  US   (       d#  UR                  5         U(       a  US   (       d  M#  X!4$ )r   Nr   r$  r   z[^ ].*r4   ### r   rJ  r*  )r   rJ  )r(  r   zEXIT STATUSzSEE ALSOr)  r   r   )r   r1   r   r{  r,   r-   rF  r   r0   insertr/  r   r_   r   )r   r   r   r   indented_chars        r   r   _ManPageCollector.CollectM  s   GllGDL
)))YY]]1d6>'7c>"" "xT :$
t9s4..//s4#6#678-!4!455DJJL(D-'MS,@IIQ%"DL'!m:&EIIQ%(DLIId?i4/DGOO%%zz#q)"-nnT[[]#G )))H '!*kk!n '!**
'"+kkm '"++r   )r   r{  r   r1   N)r   r   r   r   r   r   r   r   r   r   r  r|  r   r   r   r   r   ry  ry  +  sg    	2       -r   ry  c                   8    \ rS rSrSrSr\S 5       rS rS r	Sr
g)	_ManCommandCollectori}  z,man command help document section collector.zman-0.1c                     U R                   $ r   _CLI_VERSIONr  s    r   r   _ManCommandCollector.GetVersion      r   c                 p    [         R                  " [        R                  5       n[        R
                  " [        R                  " SU R                  /US95      sSSS5        $ ! , (       d  f       g= f! [        [        R                  4 a%    [        SR                  U R                  5      5      ef = f)r  man)r   Nz1Cannot get man(1) command man page text for [{}].)r   r   r   r   r   r   r   r   r   r   r   r(   r   )r   r   s     r   r  '_ManCommandCollector._GetRawManPageText  s    "BJJ'1z66D%%&q 2 3 ('' Z223 "(
=
D
D!" ""s(   $A6 5A%	A6 %
A3/A6 3A6 6?B5c           	      ~    U R                  5       n[        R                  " SS[        R                  " SSU5      5      $ )r  z.r4   u   (‐|\u2010)
 *)r  r,   r-   r   s     r   r|  #_ManCommandCollector.GetManPageText  s8    ""$D66r266"B./ /r   r   Nr   r   r   r   r   r  r   r   r  r|  r   r   r   r   r  r  }  s&    4, 	"/r   r  c                   8    \ rS rSrSrSr\S 5       rS rS r	Sr
g)	_ManUrlCollectori  z(man URL help document section collector.zman7.org-0.1c                     U R                   $ r   r  r  s    r   r   _ManUrlCollector.GetVersion  r  r   c                    [         R                  " 5       nSR                  U R                  5      nUR	                  U5      nUR
                  S:w  a$  [        SR                  U R                  5      5      eUR                  $ )r  z.http://man7.org/linux/man-pages/man1/{}.1.html   z&Cannot get URL man page text for [{}].)r   
GetSessionr   r   r   status_coder(   r1   )r   sessionurlresponses       r   r  #_ManUrlCollector._GetRawManPageText  sr    !!#G
:
A
AC{{3Hs"(
2
9
9$:K:K
LN N==r   c                    U R                  5       nS H)  u  p#[        R                  " X#U[        R                  S9nM+     / nSnSnSnUR	                  S5       GH  nU(       a
  US:X  a  Sn/ nUR                  S5      (       a  SnU(       a  SnUR                  S	5        S
USS ;   aC  USS R	                  S
S5      u  p[        R                  " SS	U	5      n	S
R                  SX/5      nOx[        R                  " SS	U5      nO_U(       aX  U(       d  SnM  UR                  S5      (       d  SnSnO2U(       a+  US   R                  5       (       d  UR                  S5        SnUR                  U5        GM     SR                  U5      $ )z;Returns the text man page for self.command_name from a URL.))z<span class="footline">.*r4   )z<h2><a id="([^"]*)"[^
]*
z\1
)z<b>( +)z\1*)z( +)</b>z*\1)z<i>( +)z\1_)z( +)</i>z_\1)z</?b>*)z</?i>r   )z</pre>r4   )z<a href="([^"]*)">([^
]*)</a>z[\1](\2))z&amp;z\&)z&gt;>)z&lt;<)z&#39;')rm  r$  Fr+   Nz	       *-Tr4   z     r   z\*z     z       r   +)	r  r,   r-   DOTALLr   r   r_   r   rR  )r   r1   patternreplacementlinestoprm  r  r   headtails              r   r|  _ManUrlCollector.GetManPageText  s_   ""$D
!  VVG$bii@d!!& E
CEI

4 			%	%)
,,r
48ABx~~dA.*$r4($GT01$r4($)
++%)aLL)ll4= !> 99Ur   r   Nr  r   r   r   r  r    s&    0, 	=r   r  c                   X   ^  \ rS rSrSr\S 5       rU 4S jrS rS
S jr	S r
S rS	rU =r$ )ManPageCliTreeGeneratori  zman page CLI tree generator.c                 P    [         R                  " S5      (       a  [        $ [        $ )z$Returns the man page collector type.r  )r   FindExecutableOnPathr  r  r  s    r   _GetManPageCollectorType0ManPageCliTreeGenerator._GetManPageCollectorType  s!     !!%((!!r   c                 V   > [         [        U ]  U5        U R                  5       U l        g r   )r5  r  r   r  collector_type)r   r   r9  s     r   r    ManPageCliTreeGenerator.__init__  s$    	
!41,?779Dr   c                 x    U R                   (       d  [        R                  $ U R                   R                  5       $ )r   )r  r   r   r   r   s    r   r   "ManPageCliTreeGenerator.GetVersion  s,    )))))++r   c                   ^^^
^ SS jmUUU4S jm
U
U4S jn/ n/ nSnU H  nUR                  5       R                  S5      (       a  U" XVU5        UR                  5       nUR                  5       R                  SS5      R	                  S5      nS	US
   ;   a.  US
   R	                  S	S5      u  US
'   n	U	R                  5       /nM  / nM  UR                  S5      (       a  USS nM  UR                  U5        M     U" XVU5        g)r   Nc                    U R                  S5      (       aK  SU ;   aD  U R                  SS5      u  pU S   S:X  a#  U SS n UR                  S5      (       a  USS nSnOS	nS
nOs[        U 5      S:  ad  U SS nUS   R	                  5       (       a  USS nUR                  S5      (       a#  USS nUR                  S5      (       a  USS nSnOS	nU SS n S
nUb  Ub  Uc  SnSnSnXX#4$ )z=Returns the (name, type, value-metavar, nargs) for flag name.r   rl  r   r   [N]?r7   r  rP  r   r5   r4   r6   )r   r   r.  rF  isspace)rD   rH   rF   rL   s       r   _NameTypeValueNargs=ManPageCliTreeGenerator.AddFlags.<locals>._NameTypeValueNargs  s   			$;

3*+$"X_9D~~c""CRjeEE%t9q=QR8)%C  )%^^C  #2JE%%BQx	%-5=5((r   c                    > T" XXE5      u  ppESn[        U SR                  U5      UUUUUST	S9	T[        R                     U '   g)zAdds a flag.r4   r+   F)	rD   rE   rH   rF   rL   rI   rG   rK   rJ   N)rM   r   r   rT   )
rD   rE   rI   rH   rF   rL   rG   r  rw   rJ   s
          r   r?  .ManPageCliTreeGenerator.AddFlags.<locals>._Add&  sW    "5d5"Pd5g-2ii,
.gh##$T*r   c           	      X   > U (       a"  T	" U S   5      u  p4pVU  H  nT" XqX$XV5        M     gg)zAdd a flag name list.r   Nr   )
namesrE   rI   r   rH   rF   rL   rD   r?  r  s
           r   	_AddNames3ManPageCliTreeGenerator.AddFlags.<locals>._AddNames6  s4    	!4U2Y!?%D
t(5
@  
r   r4   rJ  z, -z, --r   r   r   r  r  )NNN)lstripr   r0   r   r   r_   )r   rw   r   rJ   r  r  rE   rI   r   r1   r?  r  s    ` `      @@r   r   ManPageCliTreeGenerator.AddFlags  s     )D A EKH		!	!#	&	&%h/{{}

$$UF399%@%)!"IOOC3/%)T++??6""84   e(+r   c           	      \   [        U5      n UR                  5       u  pEU(       d   U$ US:X  aC  U(       a:  [        R                  " SSUS   5      R	                  5       U[
        R                  '   GO9US:X  a  U R                  X55        GO US;  Ga  / nSnSnU[        U5      :  av  XX   R                  S5      (       aH  Xx:  a+  UR                  [        SR                  XWU 5      5      5        UR                  XX   5        US	-   nUS	-  nU[        U5      :  a  Mv  Xx:  a+  UR                  [        SR                  XWU 5      5      5        SR                  U5      n	XC[
        R                     ;   a"  U[
        R                     U==   S
U	-   -  ss'   OX[
        R                     U'   GM  )r	  r$  z.* -+  *r4   r   r*  )BUGSCOLOPHONCOMPATIBILITYHISTORY	STANDARDSr(  z###r+   r   z

)r]   r   r,   r-   r0   r   rR   r  rF  r   r_   r2   r   rZ   )
r   r\   r  rw   r   r   blocksbeginendr1   s
             r   r   !ManPageCliTreeGenerator._GenerateP  s   tnG
"**,g4 N3 f-/VV"gaj.**/%' ())
*gg' 4 4CL \$$U++{mmODIIgC6H,IJKMM','!GE
(# CL  ;
--		'2D(EF
Gyy h6677
(**
+G
4
E
47;(**
+G
49 r   c                 >   U R                   (       d  gU R                  U R                  5      nU(       d  gU R                  U R                  /U5      nU(       d  gU R                  5       U[        R
                  '   [        R                  U[        R                  '   U$ )r  N)r  r   r   r   r   r   r   r   )r   r  r   s      r   r    ManPageCliTreeGenerator.Generates  s}    ##D$5$56I>>4,,-y9D )-(9D	$	$%$,$4$4D	 	 !Kr   )r  r   )r   r   r   r   r   r   r  r   r   r  r   r   r   r[  r\  s   @r   r  r    s9    $ :,M,^!F r   r  )r   gsutilkubectlc                 d  ^ ^^^^^^^ [         R                  " T 5      nUSS US   smn	[        R                  R                  U	5      u  pUR	                  S5      (       a  USS nUR	                  S5      (       a  USS n[
        R                  U5      (       a6  T(       a.  [        R                  R                  SR                  U5      5        gU UUUUUUU4S jnU[        R                  :X  a  [        R                  " 5       nO{U(       al  [        R                  " 5        n[         R"                  " U5      nUR%                  U5        U" [        R                  R'                  X5      U5      nSSS5        OU" X5      nW(       d  [
        R)                  U5        U$ ! , (       d  f       N,= f)	a  Returns the CLI tree for command, generating it if it does not exist.

Args:
  command: The CLI root command. This has the form:
    [executable] [tarball_path/]command_name
    where [executable] and [tarball_path/] are used only for packaging CLI
    trees and may be empty. Examples:
    * gcloud
    * kubectl/kubectl
    * python gsutil/gsutil
  directories: The list of directories containing the CLI tree JSON files.
    If None then the default installation directories are used.
  tarball: For packaging CLI trees. --commands specifies one command that is
    a relative path in this tarball. The tarball is extracted to a temporary
    directory and the command path is adjusted to point to the temporary
    directory.
  force: Update all exitsing trees by forcing them to be out of date if True.
  generate: Generate the tree if it is out of date or does not exist.
  ignore_out_of_date: Ignore out of date trees instead of regenerating.
  verbose: Display a status line for up to date CLI trees if True.
  warn_on_exceptions: Emits warning messages in lieu of generator exceptions.
    Used during installation.

Returns:
  The CLI tree for command or None if command not found or there is no
    generator.
Nr   _litez.pyz>Skipping CLI tree generation for [{}] due to previous failure.c           	      <  > U[         ;   a  TU /-   n [         U   " XS9nO[        U5      n UR                  TTT	T
TTS9$ ! [         a@  nT(       a/  [        R                  R	                  SR                  TU5      5         SnAgSnAff = f! [         a     gf = f)zHelper.)r   z%Command [{}] could not be invoked:
{}N)rh   r   r   r   r   rg   )	
GENERATORSr   r
   r   r   r   r  r   r(   )command_pathr   command_args	generatorri   rw   command_executable_argsrh   r   r   r   r   rg   s        r   _LoadOrGenerate'LoadOrGenerate.<locals>._LoadOrGenerate  s     z!,~=l|,:	 *,7i	%%!// & 1 1 $ 
**

CJJq 	" ( 
s(   A B 
B6BB
BB)shlexr   r   r\   r.  rl   rx   r
   r   r   r   r   DEFAULT_CLI_NAMELoadr   TemporaryDirectorytarfileopen
extractallr   r|   )rw   rh   r   r   r   r   r   rg   partscommand_relative_pathr   r   r  r   tmptarr  s   `` `````        @r   r   r     sY   < ++g
%38":uRy00GGMM"78/! 7##$L5!!$L ##L11	jjVL)+  > X...==?D		!	!	#sLL!c	nnS
'',,s
2
d 
$	# 0?D	-	+ 
$	#s   $AF!!
F/c           
      P   [        X&S9nU(       d8  [        [        R                  /[	        [
        R                  5       5      -   5      n/ n	[        U5       GH`  n
U
[        R                  :w  a*  [        U
UUUUUS9nU(       d  U	R                  U
5        M@  MB  U (       d  MK  S n[        R                  " US   S9n[        R                  " XXES9  U(       a  M  [        R                  " US   S9nU" U5      nU" U5      nU(       d  U(       a  UU:  aj  [        R                  " [         R"                  R%                  U5      5        [        R&                  " U5       n[(        R*                  " U US9  S	S	S	5        GM(  U(       d  GM2  [,        R.                  R1                  S
R3                  U
5      5        GMc     U	(       aR  SR3                  SR5                  [        U	5      5      5      nU(       d  [7        U5      e[,        R8                  " U5        g	g	! , (       d  f       GM  = f)a(  (re)generates the CLI trees in directory if non-existent or out of date.

This function uses the progress tracker because some of the updates can
take ~minutes.

Args:
  cli: The default CLI. If not None then the default CLI is also updated.
  commands: Update only the commands in this list.
  directory: The directory containing the CLI tree JSON files. If None then
    the default installation directories are used.
  tarball: For packaging CLI trees. --commands specifies one command that is a
    relative path in this tarball. The tarball is extracted to a temporary
    directory and the command path is adjusted to point to the temporary
    directory.
  force: Update all exitsing trees by forcing them to be out of date if True.
  verbose: Display a status line for up to date CLI trees if True.
  warn_on_exceptions: Emits warning messages in lieu of exceptions. Used
    during installation.
  skip_completions: Skip updating the static completion CLI tree.

Raises:
  CliTreeGenerationError: CLI tree generation failed for a command in
    commands.
rf   rg   )rh   r   r   r   rg   c                 b     [         R                  R                  U 5      $ ! [         a     gf = f)Nr   )r   r\   getmtimer   r[   s    r   _MtimeUpdateCliTrees.<locals>._Mtime   s.    	!!$'
' 		s   ! 
..r   )rf   )clir\   r   r   r   )r   Nz.[{}] static completion CLI tree is up to date.z$CLI tree generation failed for [{}].rk  )rj   ro   r   r  r1  r  keyssortedr   r_   CliTreeConfigPathr  r   CompletionCliTreePathr   r   r   r\   dirnamer   generate_staticListCompletionTreer
   r   r   r   r   r%   rb   )r  rT  rf   r   r   r   rg   skip_completionsrh   failedrw   r   r  cli_tree_pathcompletion_tree_pathcli_tree_mtimecompletion_tree_mtimer   messages                      r   UpdateCliTreesr
    s   D  B+	H--.joo6G1HHIH&!g(+++G(3$+"'$+/ACd g 	 00;r?KmmmuN
 %;;!!n &. &'; <.!N2
--(<=
> 45..s: 65W
**

>EEgNPI "N 4;;		&.!#G"7++KK  65s   H
H%	c           
         Uc  [         R                  " SS9n[         R                  U[         R                     ;  a8   [         R                  " 5       U[         R                     [         R                  '   [        XS9n[         R                  S1nU GH-  n U (       a$  [        R                  R                  U 5      (       d  M1  [        R                  " [        R                  " U 5      5       H  u  pgn[        U5       H  n	[        R                  R                  U	5      u  pUS:w  a  M,  X;   a  M3  UR                  U
5        U
[         R                  :X  a4  [         R                  " [        R                  R!                  Xi5      5      nO[#        U
U/UUS9nU(       d  M  X[         R                     U
'   M       GM+     GM0     U$ ! [         R
                   a     GNhf = f)a  Loads all CLI trees in directory and adds them to tree.

Args:
  directory: The config directory containing the CLI tree modules.
  ignore_out_of_date: Ignore out of date trees instead of regenerating.
  root: dict, The CLI root to update. A new root is created if None.
  warn_on_exceptions: Warn on exceptions instead of raising if True.

Raises:
  CliTreeVersionError: loaded tree version mismatch
  ImportModuleError: import errors

Returns:
  The CLI tree.
zThe CLI tree root.)rE   r  r   r   )rh   r   rg   )r   Noder  rS   r  CliTreeLoadErrorrj   r   r\   existswalkrc   rd   r  splitextr{   r   r   )rf   r   rootrg   rh   loadeddirpathr   	filenamesfilenamerw   	extensionr   s                r   LoadAllr  C  s   $ 
\==%9:D d8+C+C&DD
--/ 8##$X%>%>?  B+ %%z2&iBGGNN955#%773==+C#DYY'(WW--h7



7h///rww||G>?$-4I3E3EG$ 448x''
(
1! ($ ' $E 0 
+E $$ 
s   7G GG)r4   NNr  r4   FFN)r4   Nr6   )NF)NNFTFFF)NNNNFFFF)NFNT)=r   
__future__r   r   r   r   r   r   r,   r  r   r  r.   googlecloudsdk.callioper   ,googlecloudsdk.command_lib.static_completionr   r  r   googlecloudsdk.corer	   r
   r   googlecloudsdk.core.consoler   googlecloudsdk.core.resourcer   googlecloudsdk.core.utilr   r   rc   	six.movesr   r   r   r"   r%   r(   r2   rM   rO   r]   rj   with_metaclassABCMetaobjectrl   r   r   r  r3  r^  ri  ry  r  r  r  r  r   r
  r  r   r   r   <module>r#     s    '  ' 
  	 	     , T ? * # ( 8 9 - * 
 $J $&U &*u *0U 015 1I
 AIAE& 4os))#++v> od6 <h) hV@v @FP- Pf, ,^N. NbO**3;;? Od/, /8Q( QhU. Ur $&
 CHDI&+]B 	
Tn <@#<r   