
    6                         S r SSKrSSKrSSKrSSKrSSKJr  SrSrSr	Sr
SrS	rS
rS rSS jrS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS r g)z-Helper utilities for fleet packages commands.    N)yamlv1alpha      a  table(info.rolloutStrategyInfo.rollingStrategyInfo.clusters.membership.basename():label=CLUSTER,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.current.version:label=CURRENT_VERSION,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.current.syncState:label=SYNC_STATE,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.desired.version:label=DESIRED_VERSION,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.startTime:label=START_TIME,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.endTime:label=END_TIME,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.state:label=STATE,
                    trim_message():label=MESSAGE)a  table(info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.membership.basename():label=CLUSTER,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.current.version:label=CURRENT_VERSION,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.current.syncState:label=SYNC_STATE,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.desired.version:label=DESIRED_VERSION,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.startTime:label=START_TIME,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.endTime:label=END_TIME,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.state:label=STATE,
                    trim_message():label=MESSAGE)a  table(info.rolloutStrategyInfo.rollingStrategyInfo.clusters.membership.basename():label=CLUSTER,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.current.version:label=CURRENT_VERSION,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.current.syncState:label=SYNC_STATE,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.desired.version:label=DESIRED_VERSION,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.startTime:label=START_TIME,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.endTime:label=END_TIME,
                    info.rolloutStrategyInfo.rollingStrategyInfo.clusters.state:label=STATE,
                    all_messages():label=MESSAGES)a  table(info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.membership.basename():label=CLUSTER,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.current.version:label=CURRENT_VERSION,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.current.syncState:label=SYNC_STATE,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.desired.version:label=DESIRED_VERSION,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.startTime:label=START_TIME,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.endTime:label=END_TIME,
                    info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters.state:label=STATE,
                    all_messages():label=MESSAGES)c                      [         $ N)_DEFAULT_API_VERSION     @lib/googlecloudsdk/command_lib/container/fleet/packages/utils.py
ApiVersionr   A   s    	r   c                    U c  gU R                   (       a  U R                   R                  (       a  U R                   R                  R                  (       a&  U(       a  [        Ul        O[
        Ul        S/Ul        U R                   R                  R                  (       a'  U(       a  [        Ul        O[        Ul        S/Ul        gggg)zSets format for `rollouts describe` depending on rollout strategy.

Args:
  rollout: Rollout from `rollouts describe`
  args: Command line args
  less: Whether to show truncate rollout messages

Returns:
  None
Nz7info.rolloutStrategyInfo.rollingStrategyInfo.clusters[]z9info.rolloutStrategyInfo.allAtOnceStrategyInfo.clusters[])
inforolloutStrategyInforollingStrategyInfo3ROLLOUTS_DESCRIBE_ROLLING_TRUNCATED_MESSAGES_FORMATformat.ROLLOUTS_DESCRIBE_ROLLING_FULL_MESSAGES_FORMATflattenallAtOnceStrategyInfo5ROLLOUTS_DESCRIBE_ALLATONCE_TRUNCATED_MESSAGES_FORMAT0ROLLOUTS_DESCRIBE_ALLATONCE_FULL_MESSAGES_FORMAT)rolloutargslesss      r   FormatForRolloutsDescriber   E   s     _
\\gll66||'';;	IDOPdl||''==	KF
Edl > 7\r   c                 P   U R                  S5      (       d  U R                  S5      (       d  / $ [        R                  R                  U 5      (       a  / $ / n[        R
                  " U 5      nU H3  nU(       d  M  [        R                  " U5      nUR                  U5        M5     U$ )N.yaml.yml)endswithospathisdirr   load_all_pathdumpappend)r"   	resourcesloaded_resourcesresourcedumped_resources        r   _LoadResourcesFromFiler+   c   s    
--
 
 DMM&$9$9IWW]]4I)''-"hx		(+o' # 
r   c                 N   U R                  S5      (       d  U S-  n [        R                  R                  U 5      S-   n[        R                  " USS9 Vs/ s HC  n[        R                  R                  U5      (       d  M)  UR                  S5      (       d  MA  UPME     sn$ s  snf )zReturns paths of all .yaml and .yml files in a directory and subdirectories.

Args:
  path: The root directory to search in.

Returns:
  A list of strings, where each string is a path to a .yaml or .yml file.
/z/**/*T)pathname	recursive)r   r   )r    r!   r"   dirnameglobisfile)r"   	glob_pathps      r   _AllFilesUnderDirr5   q   s     
s		CKDggood#g-) yy)t<
<!		  zz*;< <
  
s   (B"B"B"c                 B    [         R                  " U 5      R                  $ r   )pathlibPathstemr"   s    r   _VariantNameFromFilePathr;          	d		 	  r   c                 B    [         R                  " U 5      R                  $ r   )r7   r8   namer:   s    r   _VariantNameFromDirr?      r<   r   c                 @    [         R                  R                  U 5      $ r   )r!   r"   
expanduserr:   s    r   ExpandPathForUserrB      s    			D	!!r   c                     [         R                  R                  U 5      nUnSU;   a  [         R                  R                  U5      nU$ )N$)r!   r"   rA   
expandvars)r"   user_expanded_pathvars_expanded_paths      r   _ExpandPathForUserAndVarsrH      sB    ww))$/)++,>?	r   c                     SU  S3$ )NzSource file or dir not found: .r
   r:   s    r   _FileNotFoundMessagerK      s    )$q	11r   c                     SU  S3$ )Nz)Source is not of type directory or file: rJ   r
   r:   s    r   _FileWrongTypeMessagerM      s    4TF!	<<r   c           
         [         R                  R                  U 5      nUnSU;   a  [         R                  R                  U5      n0 n0 nSU ;   =(       d    SU ;   n[        R                  " U5      nU(       d8  U Vs/ s H+  n[         R                  R                  U5      (       d  M)  UPM-     nnUR                  5         U GH  nU(       an  [         R                  R                  U5      (       aH  [        U5      n	U	(       a4  [        U5      n
X;   a  [        SU
< SXJ   < SU< S35      eXU
'   XU
'   Mu  Mw  My  [         R                  R                  U5      (       d  M  [        U5      nS U 5       n[        [        R                  R                  U5      5      nU(       d  M  [!        U5      n
X;   a  [        SU
< SXJ   < SU< S35      eXU
'   XU
'   GM     U$ s  snf )	a  Returns a dictionary of input-format variants discovered from a glob.

Gets all files from user-supplied glob pattern and creates variant(s). If
glob_pattern is for YAML files (e.g. *.yaml), variants are created from each
file matched. If glob_pattern is for directories (e.g. *), variants are
created from each directory matched. These variant(s) can be used for more
advanced deployment setups.

Args:
  glob_pattern: Pattern compatible with python's glob library

Returns:
  A dict of input-formatted variants, for example:
    {'us-a': [resources...], 'us-b': [resources...]}

Raises:
  ValueError: if duplicate variant names are detected.
rD   r   r   zDuplicate variant name z detected: paths z and z# resulted in the same variant name.c              3   r   #    U  H-  n[        [        R                  R                  U5      5      v   M/     g 7fr   )r+   r!   r"   abspath).0files     r   	<genexpr>*VariantsFromGlobPattern.<locals>.<genexpr>   s,      
" #277??4#899"s   57)r!   r"   rA   rE   r1   r#   sortr2   r+   r;   
ValueErrorr5   list	itertoolschainfrom_iterabler?   )glob_patternuser_expanded_globexpanded_globresources_by_variant_nameorigin_by_variant_nameis_file_maskpathsr4   r"   r'   variant_name
files_listresources_per_fileall_resourcess                 r   VariantsFromGlobPatternrf      s   & ww)),7$-MGG&&}5M   L(BFl,B,
))M
"%	21q!1QE2**,d			*40	1$7,3),)9 :*8;556 
 5>L
115
.  
 
t		&t,

"
 Y__::;MNO=,T2,3),)9 :*8;556 
 5BL
115
.? @ 
#"G 3s   =(G )G c                     Sn[        U 5      nU(       d  g[        U5      S:  a  [        US   5      U:  a  US   SU S-   $ US   $ )zShows the first cluster-level message and truncates it if it's too long.

Args:
  resource: A RolloutInfo resource

Returns:
  Message limited to 40 characters
(    r   r   N...$_GetClusterLevelMessagesFromResourcelen)r)   truncated_message_lengthmessagess      r   !TransformTrimClusterLevelMessagesrp      sY      1(;(	]aC,/GGA;001E99	!r   c                     U R                  S0 5      nU(       a2  UR                  SS5      nU(       a  [        U5      S:  a  USS S-   $ U$ g)zTrims rollout-level message if it's too long.

Args:
  resource: A Rollout resource

Returns:
  String message limited to 40 characters
r   messageri   rh   Nrj   )getrm   )r)   rollout_inforollout_messages      r    TransformTrimRolloutLevelMessagerv      sT     fb),"&&y"5O	_		"s#e++	r   c                 2   / nU (       d  / $ U R                  S0 5      R                  S0 5      nUR                  5        Hw  nUR                  S/ 5      nSU;   a!  UR                  UR                  S/ 5      5        UR                  S0 5      nSU;   d  MV  UR                  UR                  S/ 5      5        My     U R                  S0 5      R                  S/ 5      nU(       a5  U H/  nUR                  SS5      nU(       d  M  UR                  U5        M1     U$ )	zGathers cluster-level messages from a Rollout resource.

Args:
  resource: A Rollout resource, from `... rollouts describe ...`

Returns:
  A list of messages from the Rollout resource.
r   r   clustersro   currenterrorserrorMessageri   )rs   valuesextendr&   )	r)   ro   rollout_strategy_infort   rx   ry   info_errorserrorinfo_messages	            r   rl   rl     s     (	I",,vr266R ,224l
B/HXoohll:r23ll9b)GWoogkk*b12 5 VR(,,Xr:+YY~r2l	% 
 
/r   c                 T    [        U 5      nU(       d  g[        U5      S:X  a  US   $ U$ )zReturns all cluster-level messages from a Rollout resource.

Args:
  resource: A Rollout resource, from `... rollouts describe ...`

Returns:
  A single string or string array of cluster-level messages.
ri   r   r   rk   )r)   ro   s     r    TransformAllClusterLevelMessagesr   ,  s/     2(;(	
8}A;	/r   c                     / nU c  gU R                  S0 5      R                  S/ 5      nU H/  nUR                  SS5      nU(       d  M  UR                  U5        M1     U(       d  g[        U5      S:X  a  US   $ U$ )zBGathers errors from 'info.Errors' and returns their errorMessages.ri   r   rz   r{   r   r   )rs   r&   rm   )r)   ro   rz   r   error_messages        r   TransformListFleetPackageErrorsr   =  s{    (<<#''"5&eIInb1M}oom$ 
 

8}A;	/r   c                 4    U R                   (       d  Xl         U $ )a:  Upserts the correct fleet package name into fleet package resource.

Args:
  fleet_package: A user-inputted FleetPackage which may or may not have a name
  fully_qualified_name: The fully qualified name of the FleetPackage resource.

Returns:
  A FleetPackage that definitely has the correct fully qualified name.
)r>   )fleet_packagefully_qualified_names     r   UpsertFleetPackageNamer   P  s     
		-	r   c                 V   [        U 5      (       a  U R                  R                  R                  nUbu  UR	                  S5      (       a_  US:X  a  SU R                  R                  l        U $ U R                  R                  R                  SS U R                  R                  l        U $ )aw  Removes leading slash from fleet package path if it uses Cloud Build.

If we don't remove the leading slash, parsing the path will fail for cloud
build. See b/352756986#comment13

Args:
  fleet_package: A user-inputted FleetPackage which may need its path fixed.

Returns:
  A FleetPackage with a fixed path if it uses Cloud Build, unchanged if it
  doesn't use Cloud Build.
Nr-   z./r   )_FleetPackageUsesCloudBuildresourceBundleSelectorcloudBuildRepositoryr"   
startswith)r   r"   s     r    FixFleetPackagePathForCloudBuildr   _  s     !////DDIIDDOOC00	IM,,AAF
 
 00EEJJ12N 	,,AAF 
r   c                 j    U =(       a+    U R                   =(       a    U R                   R                  S L$ r   )r   r   )r   s    r   r   r   x  s3     P

.
.P

.
.
C
C4
Or   )F)!__doc__r1   rX   r!   r7   googlecloudsdk.corer   r	    _RESOURCE_BUNDLE_PROJECT_SEGMENT!_RESOURCE_BUNDLE_LOCATION_SEGMENTr   r   r   r   r   r   r+   r5   r;   r?   rB   rH   rK   rM   rf   rp   rv   rl   r   r   r   r   r   r
   r   r   <module>r      s    4   	  $  #$  $% !75 395 526 .46 0<&!!"2=C#L$&B"&2r   