
                         8   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
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ZddlmZ dZej@                   G d dejB                               Z"ddZ#y)z6A command that regenerates existing or new gcloud API.    )absolute_import)division)unicode_literalsN)third_party)generate)arg_parsers)base)parser_errors)regen)log)encoding)files)mapz([a-z0-9_]+)/([a-z0-9_]+)c                   &    e Zd ZdZed        Zd Zy)Regenz"Regenerate given API(s) in gcloud.c                     | j                  dt        j                         d       | j                  dd       | j                  dd       | j                  d	d
       y )Napia.  The APIs to regenerate in api_name/api_version format. These can be filename glob expressions to regenerate multiple apis. For example */* to regegenerate all apis and versions, or */*beta* to only regenerate existing beta apis. Note that if discovery doc is supplied this cannot contain any wildcards.)typehelpz--api-discovery-docz_Path to json file describing the api. If not specified, an existing discovery doc will be used.)r   z--configzRRegeneration config yaml filename. If not specified canonical config will be used.z
--base-dirzaDirectory where generated code will be written. By default googlecloudsdk/generated_clients/apis.)add_argumentr   ArgList)parsers    lib/surface/meta/apis/regen.pyArgsz
Regen.Args2   s    
  "&  ' 7  8
 
O  P <  =    c                    t        |j                        }|d   }d}|j                  rot        j                  j                  |j                        s.t        j                  dj                  |j                              t        |j                        dk7  rt        j                  d      t        j                  t        |j                  d         }|s(t        j                  dj                  t                    |j!                  d      |j!                  d      }}||d	   vr%t#        j$                  d
|       |ddii|d	   |<   d}n/||d	   |   vr%t#        j$                  d|       ddi|d	   |   |<   d}|d	   j'                  |      j'                  |i       }|dz   |z   dz   }	t        j                  j)                  |j                        }
t        j                  j)                  t        j                  j+                  |j,                  ||	            }|
|k7  rDt"        j.                  j1                  dj                  |
             t3        j4                  |
|       |d   |	k7  rd}|	|d<   |j!                  d      |j!                  d      |fg}ndj+                  t7        t8        j:                  |j                              }t=        j>                  |d	         D cg c]C  \  }}t=        j>                  |      D ]&  \  }}t        j                  ||dz   |z         r|||f( E }}}}}|sIt        j@                  dj                  dj+                  |j                        |j                              |j,                  xsJ t        j                  jC                  t        j                  jC                  tD        jF                              }tI        |      D ]  \  }}}t"        j.                  j1                  dj                  ||t        j                  j+                  ||d                      t        j                  j+                  |||d         }	t        j                  j+                  ||      }|jK                  dd      }tM        jN                  |	|||||       tM        jP                  |	||||j'                  di               t        j                  j+                  ||d      }tS        jT                  tV              }|d	   jY                         D ]$  \  }}|D ]  }|jK                  dd      ||   |<    & tM        jZ                  |||d	          |rit#        j$                  d|j                         t]        j^                  |j                        5 }t`        jb                  je                  ||       d d d        y y c c}}}}w # 1 sw Y   y xY w)Nroot_dirFzFile not found {}   z8Can only specify one api when discovery doc is provided.r   z>Api name must match {} pattern when discovery doc is specified   apisz#No such api %s in config, adding...discovery_doc Tz+No such api version %s in config, adding..._z.jsonzCopying in {}|/zqapi [{api_name}] not found in "apis" section of {config_file}. Use [gcloud meta apis list] to see available apis.,)api_nameconfig_filezGenerating {} {} from {}.	resourceszapis_map.pyz
Updated %s)3_LoadConfigconfigapi_discovery_docospathisfileregen_utilsDiscoveryDocErrorformatlenr   r
   ArgumentErrorrematch
_API_REGEXgroupr   warninggetrealpathjoinbase_dirstatusPrintshutilcopyfiler   fnmatch	translatesix	iteritems
UnknownApidirnamegooglecloudsdk__file__sortedreplacer   GenerateApitoolsApiGenerateApitoolsResourceModulecollectionsdefaultdictdictitemsGenerateApiMapr   
FileWriterruamelyamlround_trip_dump)selfargsr,   r   changed_configr7   r'   api_versionapi_version_configr!   new_discovery_docold_discovery_docregenerate_listregex_patern
api_configr>   
output_dirroot_packageapis_map_filepackage_mapversions_configversionstreams                          r   Runz	Regen.RunL   s   %Fj!HNWW^^D223++&&t'='=>@ 	@	TXX!	))FH 	H hhz488A;/e++!6*-/ 	/ $kk!nekk!nh		'98D$//21F#Gvx fVnX66A;O1@"0Evx -!&>--h7;;KLn{2W<m''**4+A+AB''**
'',,t}}h
>@ 
/	/

//0ABC)+<=	O	,	=.;?+ ;;q>5;;q>+=
>o XXc'"3"3TXX>?l /2mmF6N.K.K*h*),7I)J%k:XXlHsN[$@A [*
-)J ..K   ""N6388DHH-"kk  +, , }} 2
//0!2H-3O-D)+z	jj
$6('',,xO)DEGH
 ggll
Hj9;m77<<(3j%%c3/l""
\8[
 --




..b
) .E, GGLL8]CM))$/K%+F^%9%9%;!/$')1)9)9#s)CHg& % &< M;vG 	kk,,DKK(F##FF3 )( _b )(s   )AW
&!WW"N)__name__
__module____qualname____doc__staticmethodr   ri    r   r   r   r   .   s    *= =2b4r   r   c                 6   | xs^ t         j                  j                  t         j                  j                  t	        j
                  t        j                              d      } t         j                  j                  |       s$t        j                  dj                  |             t        j                  |       5 }t        j                  j!                  |      }ddd       rd|vr$t        j                  dj                  |             |S # 1 sw Y   5xY w)z'Loads regen config from given filename.zregen_apis_config.yamlz{} Not foundNr   z1{} does not have format of gcloud api config file)r.   r/   r=   rH   r   Decoder   rJ   r0   r1   ConfigFileErrorr3   r   
FileReaderrU   rV   round_trip_load)config_file_namerh   r,   s      r   r+   r+      s    %  ggoohook&:&:;<*  
(	)

%
%n&;&;<L&M
NN()V[[((0F *	:V+

%
%;	 	!# # 
- *)s   ; DD)N)$rm   
__future__r   r   r   rO   rC   r.   r6   rA   rI   r   googlecloudsdk.api_lib.regenr   googlecloudsdk.callioper   r	   r
   googlecloudsdk.command_lib.metar   r1   googlecloudsdk.corer   googlecloudsdk.core.utilr   r   ruamel.yamlrU   rE   	six.movesr   r8   UniverseCompatibleCommandr   r+   ro   r   r   <module>r      s|     = &  '   	 	   & 1 / ( 1 @ # - *  
 (
 4DLL 4 4Dr   