
    G                     6   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSK	r	SSK
Jr  SSK
Jr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ r   SSK!r!Sr"S r#S#S jr$\RJ                  RL                  4S jr'\RJ                  RL                  4S jr(S r)S r*\RJ                  RL                  4S jr+S r,S r-S r.\RJ                  RL                  4S jr/S r0S r1S$S  jr2S! r3\RJ                  RL                  4S" jr4g)%zCCommon utility functions for Composer environment storage commands.    )absolute_import)division)unicode_literalsN)
exceptions)transfer)environments_util)storage_api)storage_util)base)util)gcloudignore)execution_utils)log)
properties)filesz8Could not retrieve Cloud Storage bucket for environment.c                     U (       aO  SU ;   d"  SU ;   d  [         R                  " SU 5      (       a&  [        R                  " SR	                  U5      5        ggg)z9Logs deprecation warning if gsutil wildcards are in args.*?z\[.*\]zUse of gsutil wildcards is no longer supported in {0}. Set the storage/use_gsutil property to get the old behavior back temporarily. However, this property will eventually be removed.N)researchr   warningformat)path	flag_names     7lib/googlecloudsdk/command_lib/composer/storage_util.pyWarnIfWildcardIsPresentr   -   sF    	sd{cTkRYYy$-G-GKK  "6),. .HT    c                 z    U(       a  [         R                  " X5      $ [        R                  R                  X5      $ )z@Joins paths using the appropriate separator for local or gsutil.)	posixpathjoinosr   )path1path2gsutil_paths      r   
_JoinPathsr%   8   s'    >>%''77<<%%r   c                 b    [        XS9n[        R                  " 5       nUR                  X1S-   S9$ )a   Lists all resources in one folder of bucket.

Args:
  env_ref: googlecloudsdk.core.resources.Resource, Resource representing
      the Environment whose corresponding bucket to list.
  gcs_subdir: str, subdir of the Cloud Storage bucket which to list
  release_track: base.ReleaseTrack, the release track of command. Will dictate
      which Composer client library will be used.

Returns:
  list of Objects inside subdirectory of Cloud Storage bucket for environment

Raises:
  command_util.Error: if the storage bucket could not be retrieved
release_track/)prefix)_GetStorageBucketr	   StorageClient
ListBucket)env_ref
gcs_subdirr(   
bucket_refstorage_clients        r   Listr2   @   s5      !F*,,..		"	":36F	"	GGr   c                     [        XS9n[        R                  R                  R                  R                  5       nU(       a  [        XAU5        g[        XAU5        g)a|  Imports files and directories into a bucket.

Args:
  env_ref: googlecloudsdk.core.resources.Resource, Resource representing
      the Environment whose bucket into which to import.
  source: str, a path from which to import files into the
      environment's bucket. Directory sources are imported recursively; the
      directory itself will be present in the destination bucket.
      Must contain at least one non-empty value.
  destination: str, subdir of the Cloud Storage bucket into which to import
      `sources`. Must have a single trailing slash but no leading slash. For
      example, 'data/foo/bar/'.
  release_track: base.ReleaseTrack, the release track of command. Will dictate
      which Composer client library will be used.

Returns:
  None

Raises:
  command_util.Error: if the storage bucket could not be retrieved
  command_util.GsutilError: the gsutil command failed
r'   N)r+   r   VALUESstorage
use_gsutilGetBool_ImportGsutil_ImportStorageApir.   sourcedestinationr(   
gcs_bucketr6   s         r   Importr>   U   sE    . !F*  ((33;;=**k2j+6r   c           
         [         R                  " 5       nUnUR                  S5      nXA:g  nU(       d  UR                  [        R                  5      nUR                  S5      (       + nU(       a:  [        R                  R                  U5      (       d  [        R                  " S5      e[        [        R                  R                  U5      SU(       + S9nU(       Ga  [        R                  R                  U5      (       a  [        R                  " U5      nUR!                  U5       Hr  n	[        X5      n
[        R                  R                  U
5      (       a  M4  [#        XzUS5      n[$        R&                  R)                  U U5      nUR+                  X5        Mt     g[#        XqUS5      n[$        R&                  R)                  U U5      nUR+                  X5        g[$        R&                  R-                  U5      n[/        XUS9nU HN  n[$        R&                  R)                  U [#        XR1                  5       US5      5      nUR3                  UU5        MP     g)	,Imports files and directories into a bucket.r   gs://z!Source for import does not exist. r$   Fobject_is_subdirclientN)r	   r,   rstripr   sep
startswithr!   r   existscommand_utilErrorr%   dirnameisdirr   GetFileChooserForDirGetIncludedFiles_GetDestPathr
   ObjectReferenceFromBucketRefCopyFileToGCSFromUrl_GetObjectOrSubdirObjectsToUrlCopy)r=   r;   r<   rF   
old_sourcerE   source_is_localsource_dirnamefile_chooserrel_path	file_path	dest_pathobj_ref
source_ref	to_importobjdest_objects                    r   r9   r9   u   s   $$&&*==&  )	 ]]9==)F))'22/RWW^^F33


@
AA ggoofr?/BD.	ww}}V!66v>l"33F;(v0	77==##
 KO	..<<Z=FHY0 < ~{EJi,,:::;DFg6+--55f=J)fFI 00>>

~yy{K
GIk kk#{# r   c                    [         R                  R                  X5      n [         R                  " SSXR	                  5       /S[
        R                  R                  [
        R                  R                  S9nU(       a  [        R                  " S5      eg! [        R                  [        R                  4 a/  n[        R                  " [        R                  " U5      5      eSnAff = f)r@   cp-rTcommand_argsrun_concurrentout_funcerr_funcN%gsutil returned non-zero status code.)r
   rR   rS   RunGsutilCommandrW   r   outwriteerrr   PermissionErrorInvalidCommandErrorrK   GsutilErrorsix	text_type)r=   r;   r<   destination_refretvales         r   r8   r8      s     00>>/	5**V%:%:%<= F 

"
"#J
KK  
)
)

-
-
/ 5

"
"3==#3
445s   AB $C*;*C%%C*c                     [        XS9n[        R                  R                  R                  R                  5       nU(       a  [        XAU5        g[        XAU5        g)a  Exports files and directories from an environment's Cloud Storage bucket.

Args:
  env_ref: googlecloudsdk.core.resources.Resource, Resource representing
      the Environment whose bucket from which to export.
  source: str, a  bucket-relative path from which to export files.
      Directory sources are imported recursively; the directory itself will
      be present in the destination bucket. Can also include wildcards.
  destination: str, existing local directory or path to a Cloud Storage
      bucket or directory object to which to export.
      Must have a single trailing slash but no leading slash. For
      example, 'dir/foo/bar/'.
  release_track: base.ReleaseTrack, the release track of command. Will dictate
      which Composer client library will be used.

Returns:
  None

Raises:
  command_util.Error: if the storage bucket could not be retrieved or a
    non-Cloud Storage destination that is not a local directory was provided.
  command_util.GsutilError: the gsutil command failed
r'   N)r+   r   r4   r5   r6   r7   _ExportGsutil_ExportStorageApir:   s         r   Exportr}      sE    0 !F*  ((33;;=**k2j+6r   c           	      j   UnUR                  S5      nX1:g  n[        R                  " 5       n[        R                  R                  X5      nSnUR                  S5      (       a+  [        UR                  [        R                  5      SSS9nSnO:[        R                  R                  U5      (       d  [        R                  " S5      e[        [        R                  R!                  U5      SSS9n[#        XdUS9n	U(       ad  U	 H]  n
[%        XR&                  US5      n[(        R*                  " [        R                  R!                  U5      5        UR-                  XSS	9  M_     g
U	 HH  n
[        R                  R/                  [%        XR&                  US5      5      nUR1                  X5        MJ     g
)?Exports files and directories from an environment's GCS bucket.r   TrA   rB   rC   F+Destination for export must be a directory.rD   )	overwriteN)rG   r	   r,   r
   rR   rS   rI   r%   stripr   rH   r!   r   rN   rK   rL   rM   rV   rQ   namer   MakeDirCopyFileFromGCSrU   rX   )r=   r;   r<   rY   rE   rF   ra   dest_is_localr[   	to_exportrc   r_   rd   s                r   r|   r|      s\   *==&  )$$&&++99*M*-G$$)--("$@KM77==%%


J
KKbggoof5rtL.'FD)~xxdKimmBGGOOI./ St<   0088
~xxe
DFkkk## r   c                    [         R                  R                  X5      nUR                  S5      (       a)  [	        UR                  [        R                  5      SSS9nO:[        R                  R                  U5      (       d  [        R                  " S5      e [         R                  " SSUR                  5       U/S[        R                   R"                  [        R$                  R"                  S9nU(       a  [        R,                  " S
5      eg	! [&        R(                  [&        R*                  4 a/  n[        R,                  " [.        R0                  " U5      5      eS	nAff = f)r   rA   rB   TrC   r   rf   rg   rh   Nrm   )r
   rR   rS   rI   r%   r   r   rH   r!   r   rN   rK   rL   rn   rW   r   ro   rp   rq   r   rr   rs   rt   ru   rv   )r=   r;   r<   ra   rx   ry   s         r   r{   r{     s   ++99*M*G$$)--("$@K77==%%


J
KK	5**J,,.< F 

"
"#J
KK  
)
)

-
-
/ 5

"
"3==#3
445s   AD $E$5*EE$c                     UnUR                  U 5      (       a  U[        U 5      S nU(       d8  UR                  [        R                  R
                  [        R
                  5      n[        X$U(       + S9$ )z?Get dest path without the dirname of the source dir if present.NrC   )rI   lenreplacer!   r   rH   r   r%   )r[   source_pathr<   r   dest_path_suffixs        r   rQ   rQ     se     N++"3~#6#78	'//Y]]K	]1B
D Dr   c                    [        XS9n[        R                  R                  R                  R                  5       nU(       a  [        XAU5        O[        XAU5        [        R                  R                  S5        [        XB5        g)a}  Deletes objects in a folder of an environment's bucket.

gsutil deletes directory marker objects even when told to delete just the
directory's contents, so we need to check that it exists and create it if it
doesn't.

A better alternative will be to use the storage API to list
objects by prefix and implement deletion ourselves

Args:
  env_ref: googlecloudsdk.core.resources.Resource, Resource representing
      the Environment in whose corresponding bucket to delete objects.
  target: str, the path within the gcs_subdir directory in the bucket
      to delete. If this is equal to '*', then delete everything in
      gcs_subdir.
  gcs_subdir: str, subdir of the Cloud Storage bucket in which to delete.
      Should not contain slashes, for example "dags".
  release_track: base.ReleaseTrack, the release track of command. Will dictate
      which Composer client library will be used.
r'   zDeletion operation succeeded.N)r+   r   r4   r5   r6   r7   _DeleteGsutil_DeleteStorageApir   statusPrint_EnsureSubdirExists)r.   targetr/   r(   r=   r6   s         r   Deleter   '  s`    * !F*  ((33;;=**j1j*5**23j-r   c           	          [         R                  " 5       nUS:H  nU(       a  SOUn[        R                  R	                  U [        X!SS95      n[        XTUS9nU H  nUR                  U5        M     g)zHDeletes objects in a folder of an environment's bucket with storage API.r   rB   TrC   rD   N)r	   r,   r
   rR   rS   r%   rV   DeleteObject)r=   r   r/   rF   
delete_all
target_ref	to_deleter`   s           r   r   r   H  sp    $$&& }*2&++99*ZTBD* (f>)g
  r   c           	         [         R                  R                  U [        X!SS95      n [         R                  " SSUR                  5       /S[        R                  R                  [        R                  R                  S9nU(       a  [        R                  " S5      eg! [        R                  [        R                  4 a/  n[        R                  " [        R                   " U5      5      eSnAff = f)zCDeletes objects in a folder of an environment's bucket with gsutil.TrC   rmrg   rh   Nrm   )r
   rR   rS   r%   rn   rW   r   ro   rp   rq   r   rr   rs   rK   rt   ru   rv   )r=   r   r/   r   rx   ry   s         r   r   r   \  s    ++99*ZTBD*	5**Z--/0 F 

"
"#J
KK  
)
)

-
-
/ 5

"
"3==#3
445s   AB   $C3*C..C3c                 J   U=(       d    [         R                  " 5       n/ nSnU(       d#   UR                  U 5        UR                  U 5        U(       d  U(       a  [        R                  " U R                  S5      nUR                  U R                  U5      nU Hi  nU(       a  UR                  U R                  :X  a  M&  UR                  [        R                  R                  U R                  UR                  5      5        Mk     U$ ! [        R
                   a    Sn Nf = f)zAGets object_ref or the objects under object_ref is it's a subdir.FTrB   )r	   r,   	GetObjectappendapitools_exceptionsHttpNotFoundErrorr   r    r   r-   r0   r
   rR   FromNamebucket)
object_refrE   rF   objectstarget_is_subdirtarget_pathsubdir_objectsrc   s           r   rV   rV   n  s    0[..0&' 	z"nnZ  ).."5K&&z'<'<kJN	chh*//9 	nn

&
&
/
/
0A0A388
LN  
. 00 s   "D	 	D"!D"c                    SR                  U5      n[        R                  R                  U U5      n[        R
                  " 5       n UR                  U5        g! [        R                   a    UR                  R                  U R                  US9n[        R                  R                  [        R                   " 5       S5      n UR"                  R$                  R'                  XVS9   g! [        R(                   a&    [*        R,                  " SR                  U5      5      ef = f[        R.                   a&    [*        R,                  " SR                  U5      5      ef = f)a  Checks that a directory marker object exists in the bucket or creates one.

The directory marker object is needed for subdir listing to not crash
if the directory is empty.

Args:
  bucket_ref: googlecloudsk.api_lib.storage.storage_util.BucketReference,
      a reference to the environment's bucket
  subdir: str, the subdirectory to check or recreate. Should not contain
      slashes.
z{}/)r   r   zapplication/octet-stream)uploadzMError re-creating empty {}/ directory most likely due to lack of permissions.zMError checking directory {}/ marker object exists due to lack of permissions.N)r   r
   rR   rS   r	   r,   r   r   r   messagesStorageObjectsInsertRequestr   r   Upload
FromStreamioBytesIOrF   r   Insert	HttpErrorrK   rL   HttpForbiddenError)r0   subdirsubdir_name
subdir_refr1   
insert_reqr   s          r   r   r     s9    V$+++99*:EG*,,..'Z(		.	. )  ((DD   E J __''


02F)##**:*E(( )) )) 
	/	/ '


	vf~' ''s   A A+E!#C++:D%%<E!c                    [         R                  " XS9nUR                  R                  (       d  [        R
                  " [        5      e [        R                  R                  UR                  R                  5      nUR                  $ ! [        R                  [        4 a    [        R
                  " [        5      ef = f)Nr'   )environments_api_utilGetconfigdagGcsPrefixrK   rL   BUCKET_MISSING_MSGr
   rR   rU   InvalidObjectNameError
ValueErrorr0   )r.   r(   envgcs_dag_dirs       r   r+   r+     s    !!'G#		 	 


/
001..66szz7N7NOK 
		 
-
-z	: 1


/
001s   3B
 
5B?)F)FN)5__doc__
__future__r   r   r   r   os.pathr!   r   r   apitools.base.pyr   r   r   googlecloudsdk.api_lib.composerr   r   googlecloudsdk.api_lib.storager	   r
   googlecloudsdk.callioper   #googlecloudsdk.command_lib.composerr   rK   googlecloudsdk.command_lib.utilr   googlecloudsdk.corer   r   r   googlecloudsdk.core.utilr   ru   r   r   r%   ReleaseTrackGAr2   r>   r9   r8   r}   r|   r{   rQ   r   r   r   rV   r   r+    r   r   <module>r      s    J &  ' 	   	 > % V 6 7 ( D 8 / # * * 
 P .& -1,=,=,@,@ H* 8<7H7H7K7K 7@2$jL$ 8<7H7H7K7K 7B#$LL.
D 7;6G6G6J6J .B!(L$6#'L .2->->-A-A  r   