
                         >   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Zddl	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 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Zddlm Z   G d dejB                        Z! G d de!      Z" G d de!      Z# G d de!      Z$ G d de!      Z%d Z&	 	 d:dZ'd;dZ(d Z)d<dZ* G d  d! ejV                  ejX                  e-            Z. G d" d#e-      Z/ G d$ d%e.      Z0 G d& d'e-      Z1 G d( d)e.      Z2 G d* d+e-      Z3 G d, d-e.      Z4 G d. d/ ejV                  ejX                  e-            Z5 G d0 d1e5      Z6 G d2 d3e5      Z7 G d4 d5e.      Z8e0e2e4d6Z9	 	 	 d=d7Z:	 	 	 	 	 	 	 	 d>d8Z;	 	 d?d9Z<y)@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                       e Zd ZdZy)ErrorzExceptions for this module.N__name__
__module____qualname____doc__     9lib/googlecloudsdk/command_lib/meta/generate_cli_trees.pyr   r   :   s    #r   r   c                       e Zd ZdZy)CommandInvocationErrorzCommand could not be invoked.Nr   r   r   r   r   r   >   s    %r   r   c                       e Zd ZdZy)NoCliTreeForCommandErrorz!Command does not have a CLI tree.Nr   r   r   r   r   r   B   s    )r   r   c                       e Zd ZdZy)CliTreeGenerationErrorz'CLI tree generation failed for command.Nr   r   r   r   r    r    F   s    /r   r    c                       e Zd ZdZy)NoManPageTextForCommandErrorz(Could not get man page text for command.Nr   r   r   r   r"   r"   J   s    0r   r"   c                 t    t        j                  ddt        j                  |             j	                  d      S )zEReturns text dedented and multiple non-indent spaces replaced by one.z
([^ ])   *z\1 
)resubtextwrapdedentstrip)texts    r   _NormalizeSpacer+   N   s)    	fhood&;	<	B	B4	HHr   c	                    t         j                  i t         j                  |t         j                  |t         j                  t        |      t         j                  dt         j                  |t         j                  dt         j                  |t         j                  | t         j                  |xs	 |dk(  rdndt         j                  |t         j                  |iS )z)Initializes and returns a flag dict node. Fbool01)r   LOOKUP_ATTRLOOKUP_CATEGORYLOOKUP_DEFAULTLOOKUP_DESCRIPTIONr+   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   _FlagrF   S   s     Bw!!?;#?R!!;DUGevos3UE
 r   c                     t         j                  |t         j                  t        |      t         j                  | t         j
                  |iS )z/Initializes and returns a positional dict node.)r   r3   r4   r+   r9   r:   )r=   r>   r@   rE   s       r   _PositionalrH   f   s?     w!!?;#?DU	
 r   c                 F   t         j                  dt         j                  i t         j                  i t         j                  i t         j
                  dt         j                  dt         j                  | t         j                  g t         j                  dt         j                  i i
S )z2Initializes and returns a command/group dict node.r-   FGA)r   LOOKUP_CAPSULELOOKUP_COMMANDSLOOKUP_FLAGSLOOKUP_GROUPSLOOKUP_IS_GROUPr7   LOOKUP_PATHLOOKUP_POSITIONALSLOOKUP_RELEASELOOKUP_SECTIONSpaths    r   _CommandrV   p   s|     rRbD!!2t
 r   c                 X   g }| r|j                  |        |S 	 |j                  t        j                                |j                  t        j                                |S # t        j                  $ r5}|s t	        j
                  t        j                  |             Y d}~hd}~ww xY w)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   _GetDirectoriesrc      s     +y! 
$,,./
 x0023	 (( $	kk#--"##$s   #A! !B)4+B$$B)c                       e Zd ZdZdZed        Zed        Zed        ZddZ	d Z
d Zej                  d	        Zd
 ZddZ	 	 	 ddZy)CliTreeGeneratorzcBase CLI tree generator.

  Attributes:
    command_name: str, The name of the CLI tree command.
  Nc                 6    |rt               | _        yd| _        y)zCMemoizes failed attempts and doesn't repeat them if enable is True.N)set	_FAILURES)clsenables     r   MemoizeFailuresz CliTreeGenerator.MemoizeFailures   s     $CECMCMr   c                 :    | j                   r|| j                   v S dS )z<Returns True if man page request for command already failed.F)rh   ri   commands     r   AlreadyFailedzCliTreeGenerator.AlreadyFailed   s     (+}}7cmm#?%?r   c                 T    | j                   | j                   j                  |       yy)z1Add command to the set of failed man generations.N)rh   addrm   s     r   
AddFailurezCliTreeGenerator.AddFailure   s%     }} 	mm  !r   c                 6   |rUt        j                  t        j                        5 }	 t	        j
                  ||||      j                          	 ddd       || _	        |xs |g| _
        d| _        y# t        $ r}t        |      d}~ww xY w# 1 sw Y   @xY w)a  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_argsry   rb   s        r   __init__zCliTreeGenerator.__init__   s     BJJ'7	* 

,GG")++6;= ( %D/AL>DD  	*&q)
)	* ('s(   B'A33	B<BBBBc                 l    t        j                  t        j                  | j                  |z               S )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   Decoderz   check_outputr   )r   cmds     r   RunzCliTreeGenerator.Run   s0     ?? 7 7# =>@ @r   c                     | j                   s5	 | j                  dg      j                         d   | _         | j                   S | j                   S #  t        j                  | _         Y | j                   S xY w)Returns the CLI_VERSION string.version)r   r   splitr   CLI_VERSION_UNKNOWNr   s    r   
GetVersionzCliTreeGenerator.GetVersion   sk    9 HHi[1779"= 49$88s   (A A2c                      y)z(Generates and returns the CLI tree dict.Nr   r   s    r   GeneratezCliTreeGenerator.Generate        r   c                     |xs t        d      D ]L  }t        j                  j                  |xs d| j                        dz   }	 |t        j                  |      fc S  dfS # t
        j                  $ r Y hw xY w)zEReturns (path,f) open for read for the first CLI tree in directories.T)r`   ..jsonN)rc   rx   rU   joinr~   r   
FileReaderr   )r   ra   r_   rU   s       r   FindTreeFilezCliTreeGenerator.FindTreeFile   s}     LOt$LL	WW\\)*sD,=,=>HdU%%d+++ M : [[ s   A$$A:9A:c                    |j                  t        j                        }|t        j                  k(  }|j                  t        j                        }|t        j
                  k7  r|dfS | j                         }|rn|t        j                  k(  rn	||k7  r|dfS |r9t        j                  j                  dj                  | j                  |             |dfS )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   
IsUpToDatezCliTreeGenerator.IsUpToDate   s     ("="=>!X%B%BBH ((8#:#:;h...u_  ??,
	!=!=	=
	3	3u_ 	jjBII


/1 2T>r   c                     d}	  j                  |      \  }|rwd}		 t        j                  |      }
|
r] j	                  |
|      \  }}	|r|
|r|j                          S S |	r|s,|
|r|j                          S S |r	 |r|j                          yy|r|j                          	  fd}|s$t        dj                   j                              |s |       S t        j                  dj                  |rdnd j                              5   |       cddd       S # t        $ r d}
Y w xY w# |r|j                          w w xY w# 1 sw Y   yxY w)	zFLoads the CLI tree or generates it if necessary, and returns the tree.NF)r   c                  (   j                         } | r;	 t        j                        }|5  t        j                  | d|       ddd       | S | S # t        j                  $ r}t        j
                  j                        \  }}	 t        j                  |       t        j                        }nH# t        j                  $ r2 s t        j                  t        j                  |             Y Y d}~yw xY wY d}~d}~ww xY w# 1 sw Y   | S xY w)z>Helper that generates a CLI tree and writes it to a JSON file.Njson)print_formatout)r   r   rw   r   rx   rU   r   MakeDirr
   r[   r\   r]   r   r   )r   frb   r_   _rU   r   r`   s        r   	_Generatez2CliTreeGenerator.LoadOrGenerate.<locals>._Generate'  s    ]]_d		t$! 

 
 F
B kTk {{ 
	t,,)QMM)$  &A %KKa()	 
	 ksF   A DD$"C?*B21C?2>C70C?6C77C??DDzNo CLI tree for [{}].z{} the [{}] CLI treeUpdating
Generating)r   r   load
ValueErrorr   closer   r   r~   r   ProgressTracker)r   ra   forcer   ignore_out_of_datetarballr   r`   r   
up_to_dater   r   r   rU   s   `      `     @r   LoadOrGeneratezCliTreeGenerator.LoadOrGenerate  sa    	A!!+.gdA	

	1$ !%w!G
(J 
		 
  
		 
 "	
		 
		4 $
!
(
():):
;= =[		)	)%%Jt/@/@	B
C [
C 
C_  	$	 
		 
D
C 
CsE   D2 D! D2 $D2 =D2 E
!D/,D2 .D//D2 2E
ENF)NFTFFFF)r   r   r   r   rh   classmethodrk   ro   rr   r   r   r   abcabstractmethodr   r   r   r   r   r   r   re   re      s     ). . @ @ ! !
4	@  8 DHFK(-=r   re   c                       e Zd ZdZd ZddZy)_BqCollectorz#bq help document section collector.c                 Z    |j                  d      | _        d| _        d | _        d| _        y Nr$   DESCRIPTIONFr   r*   heading	lookaheadignore_trailerr   r*   s     r   r   z_BqCollector.__init__O  (    

4 DI DLDNDr   c                    g }| j                   r$|s|j                  | j                          d| _         | j                  }d| _        | j                  rf| j                  j	                  d      }|j                  d      s|s+| j                  s|j                  |j                                | j                  rf|r|d   s|j	                  d       |r|d   s|r|d   s|j	                          |r|d   sd| _        ||fS )0Returns the heading and content lines from text.Nr    r   T)r   rX   r   r*   pop
startswithr   rstripr   strip_headingscontentr   lines        r   Collectz_BqCollector.CollectU  s    G~~t~~&dnllGDL
))YY]]1d		^D<O<Ot{{}% )) '!*kk!n '!*
'"+kkm '"+DGr   Nr   r   r   r   r   r   r   r   r   r   r   r   L  s    + r   r   c                   *    e Zd ZdZd ZddZd Zd Zy)BqCliTreeGeneratorzbq CLI tree generator.c                 
   	 t        j                  | j                  |z         }t        j                  |      j                  dd      S # t         j                  $ r&}|j                  dk7  r |j
                  }Y d}~Yd}~ww xY w)r      Nzbq.pybq)	rz   r   r   CalledProcessError
returncodeoutputr   r   replacer   r   r   rb   s       r   r   zBqCliTreeGenerator.Runm  sp    &&t'>'>'DEf ??6"**7D99 (( 	
	xxf	s   "A	 	BA==Bc           	      X   |r'|j                  d      }|j                         j                  dd      \  }}|j                         g}d}|rr|d   j                  d      s^|j                  d      j                         }|j                  d      r|dd }n|j	                  |       |r|d   j                  d      s^d	j                  |      j                         }|j                  d
      rd|dd z   }d}	d}
nd}
d}	t        |||	|
|d|      |t        j                     |<   |r&yy)'Adds flags in content lines to command.r   :r   r-   z  --z
(default: 
   r   r   z--[no]--   Nr.   VALUEstringFr=   r>   rA   r?   r@   rD   rC   )	r   r)   r   r   rX   r   rF   r   rM   )r   rn   r   rC   r   r=   r>   	paragraphr@   rA   r?   s              r   AddFlagszBqCliTreeGenerator.AddFlags~  s&   
[[^d**,,,S!4dK$$&'igGAJ11&9{{1~##%??<(BK'


4
  GAJ11&9 HHY'--/k		"d12h-2!.gh##$T*' r   c                 &   t        |      }d|t        j                  <   | j                  dg|dd z         }|j	                  d      }|rF|j                  d      }|r|d   j                         s*|j	                  dd      \  }}|j                         g}g }g }	|}
|rz|d   r|d   d   j                         s_|j                  d      j                         }|dk(  r|	}
n|d	k(  r|}
n|
j                  |       |r|d   sH|d   d   j                         s_t        ||gz         }||t        j                     |<   |r%dj                  |      |t        j                     d
<   |r%dj                  |      |t        j                     d<   |rF|S )5Generates and returns the CLI subtree rooted at path.Thelpr   Nr$   r   r   z
Arguments:z	Examples:r   EXAMPLES)rV   r   rO   r   r   r   islowerr)   rX   rL   r   rS   )r   rU   rn   r*   r   r   r=   r>   examples	argumentsr   
subcommands               r   SubTreezBqCliTreeGenerator.SubTree  s   tnG(,GH$$%88VHtABx'(D jjG
[[^da*::c1%jdDZZ\NkhiiWQZwqz!}/D/D/F{{1~##%<)[ )


4
  WQZwqz!}/D/D/F DD6M*j0:gh&&'-	>Bii?
8++,];	;?99<
8++,Z8/ 4 Nr   c                 N   | j                  | j                  g      }| j                  dg      }t        |      }|j	                  d      \  }}| j                  ||d       | j                         |t        j                  <   t        j                  |t        j                  <   |S )?Generates and returns the CLI tree rooted at self.command_name.--helpTr   rC   )r   r~   r   r   r   r   r   r   r   r   r   r   r   r*   	collectorr   r   s         r   r   zBqCliTreeGenerator.Generate  s     <<**+,D 88XJDT"I""$"7JAwMM$4M0 )-(9D	$	$%$,$4$4D	 	 !Kr   Nr   )r   r   r   r   r   r   r   r   r   r   r   r   r   j  s    :">#Jr   r   c                   D    e Zd ZdZ e ed            \  ZZZZ	d Z
ddZy)_GsutilCollectorz'gsutil help document section collector.   c                 `    |j                  d      | _        d| _        | j                  | _        y )Nr$   CAPSULE)r   r*   r   UNKNOWN	page_typer   s     r   r   z_GsutilCollector.__init__  s$    

4 DIDL\\DNr   c                 Z   g }| j                   }d| _         | j                  r| j                  j                  d      }| j                  | j                  k(  rO|j                  d      r| j                  | _        d|dk(  r| j                  | _        d}}|j                  d      s| j                  | j                  k(  r(|dk(  rd}|d	k(  r	d
| _         n|j                  d      sČ| j                  | j                  k(  rt|dk(  r| j                  | _        d| _         n|dk(  r|| _         n|j                  d      rd| _         n|rd|d   j                         rQ|j                  dd      d   | _         nv| j                  | j                  k(  r|r|d   j                         r|| _         n@|j                  d      s|s|j                  |j                                | j                  r|r|d   s|j                  d       |r|d   s|r|d   s|j                          |r|d   s||fS )r   Nr   Usage:NAMEr  r   zAvailable commands:COMMANDSzAdditional help topics:TOPICSOVERVIEWr   SYNOPSISOPTIONSFLAGSr   r   )r   r*   r   r  r  r   ROOTMANTOPICendswithisupperr   rX   r   r   s        r   r   z_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 '"+Gr   Nr   )r   r   r   r   listr   r  r  r  r  r   r   r   r   r   r  r    s%    /"58n'4e"
6r   r  c                   <     e Zd ZdZ fdZd ZddZd Zd Z xZ	S )GsutilCliTreeGeneratorzgsutil CLI tree generator.c                 :    t        t        | 
  |i | g | _        y r   )superr  r   topics)r   argskwargs	__class__s      r   r   zGsutilCliTreeGenerator.__init__  s    	
 $0$A&ADKr   c                     	 t        j                  | j                  |z         }t        j                  |      S # t         j                  $ r&}|j                  dk7  r |j
                  }Y d}~Id}~ww xY w)r   r   N)rz   r   r   r   r   r   r   r   r   s       r   r   zGsutilCliTreeGenerator.Run  sc    &&t'>'>'DEf ??6"" (( 	
	xxf	s   "9 A2A--A2c                    fd}t        j                  d      }d}g }|D ]  }|j                  d      r]|r ||dj                  |             |j	                  |      }	|	j                  d      }|	j                  d      j                         g}qt        |      dkD  s|j                  |dd j                                 |r ||dj                  |             yy)	r   c           	      ^    d}d}d}t        | ||||d      t        j                     | <   y )Nr-   r.   Fr   )rF   r   rM   )r=   r>   r?   rA   r@   rn   rC   s        r   _Addz-GsutilCliTreeGenerator.AddFlags.<locals>._Add3  sA    eeg-2!.gh##$T*r   z *((-[^ ]*,)* *(-[^ ]*) *)(.*)Nz  -r$      r     )	r%   compiler   r   matchgroupr   lenrX   )
r   rn   r   rC   r!  parser=   r>   r   r%  s
    ` `      r   r   zGsutilCliTreeGenerator.AddFlags0  s     JJ78EDK		
tTYY{+
,D!{{1~{{1~,,./t9r>49++-.  
4;'( r   c                 ~   t        |      }t        |      dkD  xr |d   dk(  }|r|dd }n	|dd dgz   }| j                  |      }t        |      }	 |j	                         \  }}|s	 |S |dk(  r<|r'|d   j                  dd      d   j                         |t        j                  <   n|d	k(  rb|rb|D ]X  }		 |	j                         d   }
|
d
k(  rd|t        j                  <   | j                  ||
gz         |t        j                     |
<   Z n|dk(  r| j                  ||       nn|dk(  rg }|D ]x  }	|	s nt|	j                         }t        |      t        |      k  r/|dt        |       |k(  sA|t        |         }
|
d   j                         sc|
dvsh|j                  |
       z t        |      dkD  rd|t        j                  <   |D ]+  }
| j                  ||
gz         |t        j                     |
<   - n|dk(  r5|D ]/  }		 | j                   j                  |	j                         d          1 n^|j#                         rN|j%                         |d   k(  rd}dj'                  |D 	cg c]  }	|	dd 	 c}	      |t        j(                     |<   I# t        $ r Y w xY w# t        $ r Y w xY wc c}	w )r   r   r   Nr   Tr  r   -r
  updater  r  )offonfalsetruer  r   r   r$      )rV   r'  r   r  r   r   r)   r   rK   
IndexErrorrO   r   rL   r   r   rX   r  r  lowerr   rS   )r   rU   rn   is_help_commandr   r*   r   r   r   r   r=   commandss               r   r   zGsutilCliTreeGenerator.SubTreeP  s   tnG$i!m9Q6(9OHcHz!c88C=D &I
"**,gw^ N] i-4QZ-=-=c1-Ea-H-N-N-P'())
*j 
D::<?D X.2'(**
+48LL4O'(**
+D
1  ggw'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")*'$T!"X'*6,(()'2_   :   +s*   9J,J+2J:	J('J(+	J76J7c                 H   | j                  | j                  g      }| j                  ddg      }t        |      }	 |j	                         \  }}|sn|dk(  r| j                  ||d       0|t        j                     d   }d|t        j                  <   | j                  D ]<  }| j                  |t        j                     |gz         |t        j                     |<   > | j                         |t        j                  <   t        j                  |t        j                  <   |S )r   r   optionsTr  r   )r   r~   r   r  r   r   r   rL   rO   r  rP   r   r   r   r   )r   r   r*   r   r   r   help_commandtopics           r   r   zGsutilCliTreeGenerator.Generate  s   <<**+,D 88VY'(D &I
"**,gw	G	dGt4  001&9L-1L))*6:ll
x++
,w
678l8++,U3 
 )-(9D	$	$%$,$4$4D	 	 !Kr   r   )
r   r   r   r   r   r   r   r   r   __classcell__r  s   @r   r  r    s!    "#")@=~r   r  c                       e Zd ZdZd ZddZy)_KubectlCollectorz(Kubectl help document section collector.c                 Z    |j                  d      | _        d| _        d | _        d| _        y r   r   r   s     r   r   z_KubectlCollector.__init__  r   r   c                    g }| j                   r$|s|j                  | j                          d| _         | j                  }d| _        | j                  r| j                  j	                  d      }d}|j                  |      r-|t        |      d j                         }|r|| _         d| _        n|j                  d      r$d|v rd| _        ncd|v rd	| _        nWd
|v rd| _        nK|j                  d      s|s+| j                  s|j                  |j                                | j                  r|r|d   s|j	                  d       |r|d   s|r|d   s|j	                          |r|d   sd| _	        ||fS )r   Nr   r  USAGEr   Commandsr
  Examplesr   Optionsr  r   r   T)r   rX   r   r*   r   r   r'  r)   r  r   r   )r   r   r   r   r   usages         r   r   z_KubectlCollector.Collect  sS   G~~t~~&dnllGDL
))YY]]1de		CJK &&($.	s	#$,
#$,
 $,
		^D<O<Ot{{}%) ))* '!*kk!n '!*
'"+kkm '"+DGr   Nr   r   r   r   r   r<  r<    s    0 #r   r<  c                   *    e Zd ZdZddZd Zd Zd Zy)KubectlCliTreeGeneratorzkubectl CLI tree generator.c           
         |D ]  }|j                         j                  dd      \  }}|j                  d      d   }|j                  d      \  }}	|	dv rd}	d}
nd	}	d
}
d}t        |||
|	|d|      |t        j                     |<    y)r   r   r   , r   =)r/  r.  r-   r.   r   r   Fr   N)r)   r   rF   r   rM   )r   rn   r   rC   r   flagsr>   flagr=   r?   rA   r@   s               r   r   z KubectlCliTreeGenerator.AddFlags  s    ::<--c15e[[[r"dJJsOkdE	#	#g-2!.gh##$T* r   c                    t        |      }| j                  |dd dgz         }t        |      }	 |j                         \  }}|s	 |S |dk(  rX|D ]R  }	 |j	                         d   }d|t        j                  <   | j                  ||gz         |t        j                     |<   T nA|dv r&dj                  |      |t        j                     |<   n|d	k(  r| j                  ||       # t
        $ r Y w xY w)
r   r   Nr   Tr
  r   )r   r   r$   r  )rV   r   r<  r   r   r1  r   rO   r   rL   r   rS   r   )	r   rU   rn   r*   r   r   r   r   r=   s	            r   r   zKubectlCliTreeGenerator.SubTree  s	   tnG88DHz)*D!$'I
"**,gw N j D::<?D /3'(**
+48LL4O'(**
+D
1  1159YYw5G(()'2ggw'   s   C''	C32C3c                    | j                   sL	 | j                  ddg      }t        j                  d|      }|j	                  d      | _         | j                   S | j                   S #  t
        j                  | _         Y | j                   S xY w)r   r   z--clientzGitVersion:"([^"]*)"r   )r   r   r%   searchr&  r   r   )r   verbose_versionr%  s      r   r   z"KubectlCliTreeGenerator.GetVersion  s~    9((Iz#:;		0/B!KKN 49$88s   ?A% %B	c                 p   | j                  | j                  g      }| j                  dg      }t        |      }|j	                  d      \  }}|j                  d       | j                  ||d       | j                         |t        j                  <   t        j                  |t        j                  <   |S )r   r6  Tr   z*  --help=true: List detailed command help.r   )r   r~   r   r<  r   rX   r   r   r   r   r   r   r   s         r   r   z KubectlCliTreeGenerator.Generate  s     <<**+,D 88YK D!$'I""$"7JAwNN?@MM$4M0 )-(9D	$	$%$,$4$4D	 	 !Kr   Nr   )r   r   r   r   r   r   r   r   r   r   r   rE  rE    s    #.0	r   rE  c                       e Zd ZdZd Zeej                  d               Zej                  d        Z	ej                  d        Z
d Zy)_ManPageCollectora~  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                 v    || _         d | _        d | _        | j                         j	                  d      | _        y )Nr$   )r~   content_indentr   GetManPageTextr   r*   )r   r~   s     r   r   z_ManPageCollector.__init__7  s4    $DDDL##%++D1DIr   c                      y)r   Nr   ri   s    r   r   z_ManPageCollector.GetVersion=  s     r   c                      y)Returns the raw man page text.Nr   r   s    r   _GetRawManPageTextz$_ManPageCollector._GetRawManPageTextC  r   r   c                      y)'Returns the preprocessed man page text.Nr   r   s    r   rT  z _ManPageCollector.GetManPageTextH  r   r   c                    g }| j                   }d| _         | j                  r| j                  j                  d      }|s|dk(  r|}2|sn?|d   dk(  r| j                  st	        j
                  dd|      | _        t        |      t        | j                        kD  r|t        | j                           }|j                  | j                        sd|j                         z   }n|dk(  r)|d	k(  r$| j                  j                  d|       d
| _         n|d
k(  rt|dvrp| j                  j                  d|       d| _         ny|dv r|| _         nmd
|v sd|v rd
| _         n]|r.|d   j                         r|j                  dd      d   | _         n-|j                  |j                                | j                  r|r|d   s|j                  d       |r|d   s|r|d   s|j                          |r|d   s||fS )r   Nr   r	  r   z[^ ].*r-   ### r   r*  r  )r   r*  )r  r   zEXIT STATUSzSEE ALSOr  r   r   )r   r*   r   rS  r%   r&   r'  r   r)   insertr  r   rX   r   )r   r   r   r   indented_chars        r   r   z_ManPageCollector.CollectM  s   GllGDL
))YY]]1d6>'7c>"" "xT :$
t9s4..//s4#6#678-!4!4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 '"+Gr   N)r   r   r   r   r   r   r   r   r   rY  rT  r   r   r   r   rQ  rQ  +  sg    	2       -r   rQ  c                   0    e Zd ZdZdZed        Zd Zd Zy)_ManCommandCollectorz,man command help document section collector.zman-0.1c                     | j                   S r   _CLI_VERSIONrV  s    r   r   z_ManCommandCollector.GetVersion      r   c                 d   	 t        j                  t        j                        5 }t	        j
                  t        j                  d| j                  g|            cddd       S # 1 sw Y   yxY w# t        t        j                  f$ r% t        dj                  | j                              w xY w)rX  man)rv   Nz1Cannot get man(1) command man page text for [{}].)r   rw   rx   ry   r   r   rz   r   r~   r}   r   r"   r   )r   r   s     r   rY  z'_ManCommandCollector._GetRawManPageText  s    "BJJ'1z66D%%&q 2 3 ('' Z223 "(
=
D
D!" ""s(   #A1 6A%	A1 %A.*A1 .A1 1>B/c           	      z    | j                         }t        j                  ddt        j                  dd|            S )r[  z.r-   u   (‐|\u2010)
 *)rY  r%   r&   r   s     r   rT  z#_ManCommandCollector.GetManPageText  s8    ""$D66r266"B./ /r   N	r   r   r   r   rd  r   r   rY  rT  r   r   r   ra  ra  }  s&    4, 	"/r   ra  c                   0    e Zd ZdZdZed        Zd Zd Zy)_ManUrlCollectorz(man URL help document section collector.zman7.org-0.1c                     | j                   S r   rc  rV  s    r   r   z_ManUrlCollector.GetVersion  re  r   c                     t        j                         }dj                  | j                        }|j	                  |      }|j
                  dk7  r$t        dj                  | j                              |j                  S )rX  z.http://man7.org/linux/man-pages/man1/{}.1.html   z&Cannot get URL man page text for [{}].)r   
GetSessionr   r~   r   status_coder"   r*   )r   sessionurlresponses       r   rY  z#_ManUrlCollector._GetRawManPageText  sr    !!#G
:
A
AC{{3Hs"(
2
9
9$:K:K
LN N==r   c                    | j                         }dD ],  \  }}t        j                  |||t        j                        }. g }d}d}d}|j	                  d      D ]  }|r	|dk(  rd}g }|j                  d      rzd}|rd}|j                  d	       d
|dd v rD|dd j	                  d
d      \  }	}
t        j                  dd	|	      }	d
j                  d|	|
g      }n]t        j                  dd	|      }nE|rC|sd}|j                  d      sd}d}n(|r&|d   j                         s|j                  d       d}|j                  |        dj                  |      S )z;Returns the text man page for self.command_name from a URL.))z<span class="footline">.*r-   )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>r-   )z<a href="([^"]*)">([^
]*)</a>z[\1](\2))z&amp;z\&)z&gt;>)z&lt;<)z&#39;')rI  r	  Fr$   Nz	       *-Tr-   z     r   z\*z     z       r   +)	rY  r%   r&   DOTALLr   r   rX   r   r2  )r   r*   patternreplacementlinestoprI  r   r   headtails              r   rT  z_ManUrlCollector.GetManPageText  s_   ""$D
!  VVG[$bii@d!!& E
CEI

4 			%)
,,r
48ABx~~dA.*$r4($GT401$r4($)
+%)aLL)ll4= !> 99Ur   Nri  r   r   r   rk  rk    s&    0, 	=r   rk  c                   L     e Zd ZdZed        Z fdZd ZddZd Z	d Z
 xZS )	ManPageCliTreeGeneratorzman page CLI tree generator.c                 D    t        j                  d      rt        S t        S )z$Returns the man page collector type.rg  )r   FindExecutableOnPathra  rk  rV  s    r   _GetManPageCollectorTypez0ManPageCliTreeGenerator._GetManPageCollectorType  s     !!%(!!r   c                 V    t         t        |   |       | j                         | _        y r   )r  r  r   r  collector_type)r   r~   r  s     r   r   z ManPageCliTreeGenerator.__init__  s$    	
!41,?779Dr   c                 n    | j                   st        j                  S | j                   j                         S )r   )r  r   r   r   r   s    r   r   z"ManPageCliTreeGenerator.GetVersion  s,    )))))++r   c                   
 ddfd

fd}g }g }d}|D ]  }|j                         j                  d      r ||||       |j                         }|j                         j                  dd      j	                  d      }d	|d
   v r-|d
   j	                  d	d      \  |d
<   }	|	j                         g}g }|j                  d      r|dd }|j                  |         ||||       y)r   Nc                    | j                  d      rDd| v r| j                  dd      \  } }| d   dk(  r| dd } |j                  d      r|dd }d}nd	}d
}nct        |       dkD  rU| dd }|d   j	                         r|dd }|j                  d      r|dd }|j                  d      r|dd }d}nd	}| dd } d
}|||d}d}d}| |||fS )z=Returns the (name, type, value-metavar, nargs) for flag name.r   rH  r   r   [N]?r0   r   r0  r   r.   r-   r/   )r   r   r  r'  isspace)r=   rA   r?   rE   s       r   _NameTypeValueNargsz=ManPageCliTreeGenerator.AddFlags.<locals>._NameTypeValueNargs  s   		$;

3*+$"X_9D~~c"CRjeEE%t9q=QR8)%C )%^^C #2JE%%BQx	%-5=E5%((r   c                      | |||      \  } }}}d}t        | dj                  |      |||||d		      t        j                     | <   y)zAdds a flag.r-   r$   F)	r=   r>   rA   r?   rE   rB   r@   rD   rC   N)rF   r   r   rM   )
r=   r>   rB   rA   r?   rE   r@   r  rn   rC   s
          r   r!  z.ManPageCliTreeGenerator.AddFlags.<locals>._Add&  s_    "5dE5%"PdE5%g-2ii,
.gh##$T*r   c           	      T    | r% 	| d         \  }}}}| D ]  } ||||||        yy)zAdd a flag name list.r   Nr   )
namesr>   rB   r   rA   r?   rE   r=   r!  r  s
           r   	_AddNamesz3ManPageCliTreeGenerator.AddFlags.<locals>._AddNames6  s>    	!4U2Y!?5%D
t[(E5%
@  
r   r-   r*  z, -z, --r   r   r   r]  r  )NNN)lstripr   r)   r   r   rX   )r   rn   r   rC   r  r  r>   rB   r   r*   r!  r  s    ` `      @@r   r   z ManPageCliTreeGenerator.AddFlags  s     )D A EKH		!	!#	&%h/{{}

$$UF399%@%)!"IOOC3/%)T++??6"84   e[(+r   c           	      :   t        |      }	 |j                         \  }}|s	 |S |dk(  r>|rnt        j                  dd|d         j	                         |t
        j                  <   n3|dk(  r| j                  ||       n|dvrg }d}d}|t        |      k  rr||   j                  d      rJ||k  r,|j                  t        dj                  |||                    |j                  ||          |d	z   }|d	z  }|t        |      k  rr||k  r,|j                  t        dj                  |||                    dj                  |      }	||t
        j                     v r"|t
        j                     |xx   d
|	z   z  cc<   n|	|t
        j                     |<   )r   r	  z.* -+  *r-   r   r  )BUGSCOLOPHONCOMPATIBILITYHISTORY	STANDARDSr  z###r$   r   z

)rV   r   r%   r&   r)   r   rK   r   r'  r   rX   r+   r   rS   )
r   rU   r   rn   r   r   blocksbeginendr*   s
             r   r   z!ManPageCliTreeGenerator._GenerateP  s   tnG
"**,gw4 N3 f-/VV"gaj.**/%' ())
*ggw' 4 4CL S\$$U+s{mmODIIgeC6H,IJKMM'#,'!GE
(# CL  3;
--		'%2D(EF
Gyy gh6677
(**
+G
4
E
47;'(**
+G
49 r   c                     | j                   sy| j                  | j                        }|sy| j                  | j                  g|      }|sy| j                         |t        j
                  <   t        j                  |t        j                  <   |S )r   N)r  r~   r   r   r   r   r   r   )r   r   r   s      r   r   z 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   r9  r:  s   @r   r  r    s4    $ :,M,^!Fr   r  )r   gsutilkubectlc                     t        j                         }|dd |d   c}	t        j                  j                  |	      \  }
}|j	                  d      r|dd }|j	                  d      r|dd }t
        j                  |      r1r.t        j                  j                  dj                  |             y fd}|t        j                  k(  rt        j                         }nv|rkt        j                         5 }t!        j"                  |      }|j%                  |        |t        j                  j'                  ||	      |      }ddd       n	 ||	|      }st
        j)                  |       |S # 1 sw Y   "xY w)	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                 6   |t         v r| gz   }	 t        |   ||      }nt        |      }	 |j                  	
      S # t        $ r;}r/t        j                  j	                  dj                  |             Y d}~yd}~ww xY w# t        $ r Y yw xY w)zHelper.)r   z%Command [{}] could not be invoked:
{}N)ra   r   r   r   r   r`   )	
GENERATORSr   r
   r   r   r   r  r   r"   )command_pathr~   command_args	generatorrb   rn   command_executable_argsra   r   r   r   r   r`   s        r   _LoadOrGeneratez'LoadOrGenerate.<locals>._LoadOrGenerate  s     z!,~=l|,L:	 *,7i	%%!// & 1 1 $ 
**

CJJq 	" ( 
s(   A B 	B	1BB		BB)shlexr   rx   rU   r  re   ro   r
   r   r   r   r   DEFAULT_CLI_NAMELoadr   TemporaryDirectorytarfileopen
extractallr   rr   )rn   ra   r   r   r   r   r   r`   partscommand_relative_pathr   r~   r  r   tmptarr  s   `` `````        @r   r   r     sW   < ++g
%38":uRy00GGMM"78/!\ 7#$L5!$L ##L1	jjVL)+  > X...==?D		!	!	#sLL!c	nnS
'',,s1
2
d 
$	# 0,?D	-	+ 
$	#s   AF  F	c           	         t        ||      }|s8t        t        j                  gt	        t
        j                               z         }g }	t        |      D ]D  }
|
t        j                  k7  r&t        |
|||||      }|r+|	j                  |
       =| s@d }t        j                  |d         }t        j                  | |||       |rxt        j                  |d         } ||      } ||      }|s|r||k  rit        j                  t         j"                  j%                  |             t        j&                  |      5 }t)        j*                  | |       d	d	d	       |st,        j.                  j1                  d
j3                  |
             G |	rLdj3                  dj5                  t        |	                  }|st7        |      t-        j8                  |       y	y	# 1 sw Y   xY w)aP  (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.
  r_   r`   )ra   r   r   r   r`   c                 `    	 t         j                  j                  |       S # t        $ r Y yw xY w)Nr   )rx   rU   getmtimer}   rT   s    r   _MtimezUpdateCliTrees.<locals>._Mtime   s.    	!!$'
' 		s   ! 	--r   )r_   )clirU   r   r   r   )r   Nz.[{}] static completion CLI tree is up to date.z$CLI tree generation failed for [{}].rG  )rc   rg   r   r  r  r  keyssortedr   rX   CliTreeConfigPathr  r   CompletionCliTreePathr   r   rx   rU   dirnamerw   generate_staticListCompletionTreer
   r   r   r   r   r    r[   )r  r4  r_   r   r   r   r`   skip_completionsra   failedrn   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-ugN
 %;;!!n &. &'; <.!N2
--(<=
> 45..s: 65
**

>EEgNPI "N 4;;		&.!#G"7++KK  65s   G,,G6	c           	         |t        j                  d      }t         j                  |t         j                     vr7	 t        j                         |t         j                     t         j                  <   t        | |      }t         j                  dh}|D ]  } | rt        j                  j                  |       s&t        j                  t        j                  |             D ]  \  }}}t        |      D ]  }	t        j                  j                  |	      \  }
}|dk7  r+|
|v r0|j                  |
       |
t         j                  k(  r4t        j                  t        j                  j!                  ||	            }nt#        |
|g||      }|s||t         j                     |
<       |S # t         j
                  $ r Y Sw xY w)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.)r>   r  r   r   )ra   r   r`   )r   Noder  rL   r  CliTreeLoadErrorrc   rx   rU   existswalkr\   r]   r  splitextrq   r   r   )r_   r   rootr`   ra   loadeddirpathr   	filenamesfilenamern   	extensionr   s                r   LoadAllr  C  s   $ 
\==%9:D d8+C+C&DD
--/ 8##$X%>%>?  .@B+ %%z2&iBGGNN95#%773==+C#D!YY'(WW--h7
f


7h///rww||GX>?$-4I3E3EG$ 48$x''
(
1! ($ ' $E 0 
+E $$ 
s   6F0 0GG)r-   NNr   r-   FFN)r-   Nr/   )NF)NNFTFFF)NNNNFFFF)NFNT)=r   
__future__r   r   r   r   r   rx   r%   r  rz   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   r\   	six.movesr   r   r   r   r    r"   r+   rF   rH   rV   rc   with_metaclassABCMetaobjectre   r   r   r  r  r<  rE  rQ  ra  rk  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   