
    8                         S 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!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!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   @   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   D   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$ )Nz.yamlz.yml)endswithospathisdirr   load_all_pathdumpappend)r    	resourcesloaded_resourcesresourcedumped_resources        r   _LoadResourcesFromFiler)   b   s    
--
 
 DMM&$9$9IWW]]4I)''-"hx		(+o' # 
r   c                     U R                  S5      (       d  U S-  n [        R                  R                  U 5      S-   n[        R                  " USS9$ )N/z/**/*T)pathname	recursive)r   r   r    dirnameglob)r    	glob_paths     r   _AllFilesUnderDirr1   p   sG    	s		CKDggood#g-)	D
 r   c                 B    [         R                  " U 5      R                  $ r   )pathlibPathstemr    s    r   _VariantNameFromPathr7   y   s    	d		 	  r   c                 d    U R                  S5      (       d  U S-  n U R                  S5      S   nU$ )Nr+   )r   split)r    variant_names     r   _VariantNameFromDirr<   }   s2    	s		CKDC$,	r   c                 @    [         R                  R                  U 5      $ r   )r   r    
expanduserr6   s    r   ExpandPathForUserr?      s    			D	!!r   c                     [         R                  R                  U 5      nUnSU;   a  [         R                  R                  U5      nU$ )N$)r   r    r>   
expandvars)r    user_expanded_pathvars_expanded_paths      r   _ExpandPathForUserAndVarsrE      sB    ww))$/)++,>?	r   c                     U(       d  U $ [        U 5      n[        U5      n[        R                  R                  U5      (       a  [        R                  R	                  X#5      $ U$ )a  Creates glob pattern by combining source and variants_pattern.

Args:
  source: Directory or source configuration file.
  variants_pattern: Optional variants_pattern for use with source.

Returns:
  A glob_pattern for use with 'VariantsFromGlobPattern'. If source
  is a directory, the pattern is applied within the directory. If source is
  not a directory i.e., a file, the pattern is not applied.

  Ex: source=/cfg/, variants_pattern='*.yaml'; returns '/cfg/*.yaml'.
      source=manifest.yaml, variants_pattern=*; returns manifest.yaml.
)rE   r   r    r!   join)sourcevariants_patternexpanded_sourceexpanded_variants_patterns       r   'GlobPatternFromSourceAndVariantsPatternrL      sN     
M-f5/78HIWW]]?##77<<CCr   c                     SU  S3$ )NzSource file or dir not found: .r
   r6   s    r   _FileNotFoundMessagerO      s    )$q	11r   c                     SU  S3$ )Nz)Source is not of type directory or file: rN   r
   r6   s    r   _FileWrongTypeMessagerQ      s    4TF!	<<r   c                    [        U 5      n[        R                  R                  U5      (       d^  [        R                  R	                  U5      (       d9  [        R                  R                  U5      (       d  [        [        U5      5      eg g g r   )rE   r   r    r!   isfileexistsFileNotFoundErrorrO   )rH   rJ   s     r   ValidateSourcerV      sc    -f5/		'	'0O0O77>>/**2?CDD + 1P	'r   c                    [         R                  R                  U 5      nUnSU;   a  [         R                  R                  U5      n[        R                  " U5      nUR                  5         0 n[        U5      S:X  a  [         R                  R                  US   5      (       a  [        US   5      nU(       a  XTS'   U$ [         R                  R                  US   5      (       ah  [        US   5      n/ nU HG  n[         R                  R                  U5      n	[        U	5      nU(       d  M6  UR                  U5        MI     U(       a  XtS'   U$ [        U5      S:  a  U H  n
[         R                  R                  U
5      (       a%  [        U
5      nU(       a  [        U
5      nXTU'   MJ  ML  [         R                  R                  U
5      (       d  Mr  [        U
5      n/ nU HG  n[         R                  R                  U5      n	[        U	5      nU(       d  M6  UR                  U5        MI     U(       d  M  [        U
5      nXtU'   M     U$ )a  Returns a dictionary of input-format variants discovered from a glob.

Gets all files from user-supplied glob pattern and creates variant(s). There
will be a single variant 'default' if the inputted glob pattern has no
wildcards, and multiple variants if there are wildcards. 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...]}
rA   r   r   default)r   r    r>   rB   r/   sortlenrS   r)   r!   r1   abspathextendr7   r<   )glob_patternuser_expanded_globexpanded_globpathsvariantsr%   
files_listall_resourcesfilefull_file_pathr    r;   s               r   VariantsFromGlobPatternrf      s    ww)),7$-MGG&&}5M
))M
"%**,(Z1_	ww~~eAh(q2i	': 
/9 
uQx	 	 $U1X.jm$.*>:	9


y
)	 
 
+& 
/% 5zA~			*40	-d3,#,<
   77==&t,
D77??40.,^<)Y  +	 
 =,T2,#0<
 ! " 
/r   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...$_GetClusterLevelMessagesFromResourcerZ   )r'   truncated_message_lengthmessagess      r   !TransformTrimClusterLevelMessagesro      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   )getrZ   )r'   rollout_inforollout_messages      r    TransformTrimRolloutLevelMessageru     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   clustersrn   currenterrorserrorMessageri   )rr   valuesr\   r$   )	r'   rn   rollout_strategy_infors   rw   rx   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'   rn   s     r    TransformAllClusterLevelMessagesr   8  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   ry   rz   r   r   )rr   r$   rZ   )r'   rn   ry   r~   error_messages        r   TransformListFleetPackageErrorsr   I  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.
)name)fleet_packagefully_qualified_names     r   UpsertFleetPackageNamer   \  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   k  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     s3     P

.
.P

.
.
C
C4
Or   )Fr   )"__doc__r/   r   r3   googlecloudsdk.corer   r	    _RESOURCE_BUNDLE_PROJECT_SEGMENT!_RESOURCE_BUNDLE_LOCATION_SEGMENTr   r   r   r   r   r   r)   r1   r7   r<   r?   rE   rL   rO   rQ   rV   rf   ro   ru   rl   r   r   r   r   r   r
   r   r   <module>r      s    4  	  $  #$  $% !75 395 526 .46 0<!"22=E7t$&B"&2r   