
    (                        d 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	mZ ddl	mZ ddlmZ dZdZdZej,                   ej.                  ej0                  j2                         G d dej4                                      Zej,                   ej.                  ej0                  j8                         G d de                    Zy)z(Command to list Cloud Storage resources.    )absolute_import)division)unicode_literals)	cloud_api)base)encryption_util)errors)flags)	list_util)ls_command_util)stdin_iterator)storage_url)logz
List your Cloud Storage buckets in a project and objects in a bucket.
This command treats forward slashes in object names as directories. See
below for examples of how to use wildcards to get the listing behavior
you want.
a  
The following command lists the buckets in the default project:

  $ {command}

The following command lists the buckets in the specified project:

  $ {command} --project=my-project

The following command lists the contents of a bucket:

  $ {command} gs://my-bucket

You can use [wildcards](https://cloud.google.com/storage/docs/wildcards)
to match multiple paths (including multiple buckets). Bucket wildcards are
expanded to match only buckets contained in your current project. The
following command matches ``.txt'' objects that begin with ``log'' and
that are stored in buckets in your project that begin with ``my-b'':

  $ {command} gs://my-b*/log*.txt

You can use double-star wildcards to match zero or more directory levels
in a path. The following command matches all ``.txt'' objects in a bucket.

  $ {command} gs://my-bucket/**/*.txt

The wildcard `**` retrieves a flat list of objects in a single API call
and does not match prefixes. The following command would not match
`gs://my-bucket/dir/log.txt`:

  $ {command} gs://my-bucket/**/dir

Double-star expansion also can not be combined with other expressions in a
given path segment and operates as a single star in that context. For
example, the command `gs://my-bucket/dir**/log.txt` is treated as
`gs://my-bucket/dir*/log.txt`. To get the recursive behavior, the command
should instead be written the following way:

  gs://my-bucket/dir*/**/log.txt

The following command lists all items recursively with formatting by
using `--recursive`:

  $ {command} --recursive gs://bucket

Recursive listings are similar to `**` except recursive listings include
line breaks and header formatting for each subdirectory.

c                   @    e Zd ZdZeedZed        Zed        Z	d Z
y)Ls'List Cloud Storage buckets and objects.DESCRIPTIONEXAMPLESc                 "   |j                  ddd       |j                  dddd	       |j                  d
ddd	       |j                  dddd	       |j                  dd       |j                  ddd	       |j                  ddddd	       |j                  d      }|j                  dddd	       |j                  dddd 	       |j                  d!d"dd#	       t        j                  |       t        j                  |d$%       t        j
                  |d$&       t        j                  |       t        j                  |       | j                         t        j                  j                  k(  rt        j                  |       y'y')(z-Edit argparse.ArgumentParser for the command.path*zzThe path of objects and directories to list. The path must begin with gs:// and is allowed to contain wildcard characters.)nargshelpz-az--all-versions
store_truea  Include noncurrent object versions in the listing. This flag is typically only useful for buckets with [object versioning](https://cloud.google.com/storage/docs/object-versioning) enabled. If combined with the `--long` option, the metageneration for each listed object is also included.)actionr   z-bz	--bucketszWhen given a bucket URL, only return buckets. Useful for avoiding the rule that prints the top-level objects of buckets matching a query. Typically used in combination with `--full` to get the full metadata of buckets.z-ez--etagz=Include ETag metadata in listings that use the `--long` flag.z--formatzUse "gsutil" to get the style of the older gsutil CLI. (e.g. "--format=gsutil"). Other format values (e.g. "json") do not work. See different ls flags and commands for alternative formatting.)r   z--readable-sizeszgWhen used with `--long`, print object sizes in human readable format, such as 1 KiB, 234 MiB, or 2 GiB.z-Rz-rz--recursivezPRecursively list the contents of any directories that match the path expression.True)mutexz-lz--longz=For objects only. List size in bytes, creation time, and URL.z-Lz--fullz0List all available metadata about items in rows.z-jz--jsonz7List all available metadata about items as a JSON dump.T)command_only_reads_data)is_listN)add_argument	add_groupr
   add_additional_headers_flagadd_encryption_flags&add_fetch_encrypted_object_hashes_flagadd_read_paths_from_stdin_flagadd_soft_delete_flagsReleaseTrackr   ALPHAadd_metadata_filter_flag)clsparseroutput_styless      lib/surface/storage/ls.pyArgszLs.Argsh   s    I	   8   )  
 L	      B	      	 $$6$2ML	   ?	   F	   
%%f-	vtD	00F	((0	'
T..444$$V, 5    c                 Z    t        |dd      }t        |dd      }t        |dd      }|||fS )zGet the args for the command.soft_deletedFall_versionsbuckets)getattr)r,   argsr3   r4   r5   s        r/   	_get_argszLs._get_args   s;     47L47LdIu-Gw..r1   c                    t        j                  |       | j                  |      \  }}}|r|rt        j                  d       |r|rt        j                  d       t        j                  |      }d}|j                  s|j                  r-t        j                  |j                  |j                  d      }nt        j                  j                  dz   g}t        |dd      }|D 	cg c]  }	t        j                   |	       }
}	|
D ]  }t#        |t        j$                        s.t'        j(                  d	j+                  |j,                              |2|j.                  t        j                  k7  rt'        j0                  d
      |j.                  t        j                  usd} |j2                  rt4        j6                  j8                  }nh|j:                  rt4        j6                  j<                  }nA|j>                  rt4        j6                  j@                  }nt4        j6                  jB                  }tE        jF                  |
|||jH                  t        |dd       |jJ                  dt        |dd      t        jL                  |      |jN                  |jP                  ||xr ||      jS                          |r#|j2                  rt        j                  d       yyyc c}	w )zCommand execution logic.zThe --all-versions flag has no effect when used with the --soft-deleted flag. When --soft-deleted is used, all soft-deleted versions of the resource are returned.z;The --all-versions flag has no effect when listing buckets.FT)allow_emptyz://metadata_filterNz+Ls only works for cloud URLs. Error for: {}z/Metadata filter is only supported for GCS URLs.
exhaustivenext_page_token)buckets_flagdisplay_detailfetch_encrypted_object_hasheshalt_on_empty_responseinclude_etaginclude_managed_foldersr=   object_statereadable_sizesrecursion_flaguse_gsutil_stylesoft_deleted_bucketslist_filterz:For additional metadata information, please run ls --json.)*r   initialize_key_storer8   r   warningr
   check_if_use_gsutil_styler   read_paths_from_stdinr   get_urls_iterabler   DEFAULT_PROVIDERvaluer6   r   storage_url_from_string
isinstanceCloudUrlr	   InvalidUrlErrorformat
url_stringschemeErrorfullr   DisplayDetailFULLjsonJSONlongLONGSHORTr   
LsExecutorr@   etagget_object_state_from_flagsrE   	recursive	list_urls)selfr7   r3   r4   r5   rG   found_non_default_providerpathsr;   r   storage_urlsurlr?   s                r/   RunzLs.Run   sd    ((.
 +/..*>'L, 	kkA <	kkOP66t<!&yyD....
))T//Te ))//%78ed$5t<OJOP%$K77=%LP[112$$9@@P
 	
 
%jjI666llLMM	955	5%)"  yy ..33n	 ..33n	 ..33n ..44n%&*&H&H#*4u#EEYY $&7>66t<**~~)$5# ik!dii 
kkNO '0!Q Qs   (K$N)__name__
__module____qualname____doc___COMMAND_DESCRIPTION_GA_EXAMPLESdetailed_helpclassmethodr0   r8   rk    r1   r/   r   r   \   sH     0 *- ^- ^-@ / /KPr1   r   c                        e Zd ZdZeeez   dZy)LsAlphar   r   N)rl   rm   rn   ro   rp   rq   _ALPHA_EXAMPLESrr   rt   r1   r/   rv   rv     s     0 *0-r1   rv   N)ro   
__future__r   r   r   googlecloudsdk.api_lib.storager   googlecloudsdk.callioper   "googlecloudsdk.command_lib.storager   r	   r
   r   r   r   r   googlecloudsdk.corer   rp   rq   rw   UniverseCompatibleReleaseTracksr)   GACommandr   r*   rv   rt   r1   r/   <module>r      s     / &  ' 4 ( > 5 4 8 > = : # /` D%%(()~P ~P * ~PB D%%++,b  - r1   