OLD | NEW |
(Empty) | |
| 1 # Copyright 2011 Google Inc. |
| 2 # Copyright 2011, Nexenta Systems Inc. |
| 3 # |
| 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 # you may not use this file except in compliance with the License. |
| 6 # You may obtain a copy of the License at |
| 7 # |
| 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 # |
| 10 # Unless required by applicable law or agreed to in writing, software |
| 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 # See the License for the specific language governing permissions and |
| 14 # limitations under the License. |
| 15 |
| 16 import sys |
| 17 |
| 18 from gslib.command import Command |
| 19 from gslib.command import COMMAND_NAME |
| 20 from gslib.command import COMMAND_NAME_ALIASES |
| 21 from gslib.command import CONFIG_REQUIRED |
| 22 from gslib.command import FILE_URIS_OK |
| 23 from gslib.command import MAX_ARGS |
| 24 from gslib.command import MIN_ARGS |
| 25 from gslib.command import PROVIDER_URIS_OK |
| 26 from gslib.command import SUPPORTED_SUB_ARGS |
| 27 from gslib.command import URIS_START_ARG |
| 28 from gslib.exception import CommandException |
| 29 from gslib.help_provider import HELP_NAME |
| 30 from gslib.help_provider import HELP_NAME_ALIASES |
| 31 from gslib.help_provider import HELP_ONE_LINE_SUMMARY |
| 32 from gslib.help_provider import HELP_TEXT |
| 33 from gslib.help_provider import HelpType |
| 34 from gslib.help_provider import HELP_TYPE |
| 35 from gslib.util import NO_MAX |
| 36 from gslib.wildcard_iterator import ContainsWildcard |
| 37 |
| 38 _detailed_help_text = (""" |
| 39 <B>SYNOPSIS</B> |
| 40 gsutil cat [-h] uri... |
| 41 |
| 42 |
| 43 <B>DESCRIPTION</B> |
| 44 The cat command outputs the contents of one or more URIs to stdout. |
| 45 It is equivalent to doing: |
| 46 |
| 47 gsutil cp uri... - |
| 48 |
| 49 (The final '-' causes gsutil to stream the output to stdout.) |
| 50 |
| 51 |
| 52 <B>OPTIONS</B> |
| 53 -h Prints short header for each object. For example: |
| 54 gsutil cat -h gs://bucket/meeting_notes/2012_Feb/*.txt |
| 55 |
| 56 -v Parses uris for version / generation numbers (only applicable in |
| 57 version-enabled buckets). For example: |
| 58 |
| 59 gsutil cat -v gs://bucket/object#1348772910166003 |
| 60 |
| 61 Note that wildcards are not permitted while using this flag. |
| 62 """) |
| 63 |
| 64 |
| 65 class CatCommand(Command): |
| 66 """Implementation of gsutil cat command.""" |
| 67 |
| 68 # Command specification (processed by parent class). |
| 69 command_spec = { |
| 70 # Name of command. |
| 71 COMMAND_NAME : 'cat', |
| 72 # List of command name aliases. |
| 73 COMMAND_NAME_ALIASES : [], |
| 74 # Min number of args required by this command. |
| 75 MIN_ARGS : 0, |
| 76 # Max number of args required by this command, or NO_MAX. |
| 77 MAX_ARGS : NO_MAX, |
| 78 # Getopt-style string specifying acceptable sub args. |
| 79 SUPPORTED_SUB_ARGS : 'hv', |
| 80 # True if file URIs acceptable for this command. |
| 81 FILE_URIS_OK : False, |
| 82 # True if provider-only URIs acceptable for this command. |
| 83 PROVIDER_URIS_OK : False, |
| 84 # Index in args of first URI arg. |
| 85 URIS_START_ARG : 0, |
| 86 # True if must configure gsutil before running command. |
| 87 CONFIG_REQUIRED : True, |
| 88 } |
| 89 help_spec = { |
| 90 # Name of command or auxiliary help info for which this help applies. |
| 91 HELP_NAME : 'cat', |
| 92 # List of help name aliases. |
| 93 HELP_NAME_ALIASES : [], |
| 94 # Type of help: |
| 95 HELP_TYPE : HelpType.COMMAND_HELP, |
| 96 # One line summary of this help. |
| 97 HELP_ONE_LINE_SUMMARY : 'Concatenate object content to stdout', |
| 98 # The full help text. |
| 99 HELP_TEXT : _detailed_help_text, |
| 100 } |
| 101 |
| 102 def VersionedSrcUriIter(self): |
| 103 for uri_str in self.args: |
| 104 if ContainsWildcard(uri_str): |
| 105 raise CommandException('Wildcarding disallowed with -v flag.') |
| 106 yield self.suri_builder.StorageUri(uri_str, parse_version=True) |
| 107 |
| 108 def UnVersionedSrcUriIter(self): |
| 109 for uri_str in self.args: |
| 110 for uri in self.WildcardIterator(uri_str).IterUris(): |
| 111 yield uri |
| 112 |
| 113 # Command entry point. |
| 114 def RunCommand(self): |
| 115 show_header = False |
| 116 parse_versions = False |
| 117 if self.sub_opts: |
| 118 for o, unused_a in self.sub_opts: |
| 119 if o == '-h': |
| 120 show_header = True |
| 121 elif o == '-v': |
| 122 parse_versions = True |
| 123 |
| 124 printed_one = False |
| 125 # We manipulate the stdout so that all other data other than the Object |
| 126 # contents go to stderr. |
| 127 cat_outfd = sys.stdout |
| 128 sys.stdout = sys.stderr |
| 129 did_some_work = False |
| 130 |
| 131 if parse_versions: |
| 132 uri_iter = self.VersionedSrcUriIter |
| 133 else: |
| 134 uri_iter = self.UnVersionedSrcUriIter |
| 135 |
| 136 for uri in uri_iter(): |
| 137 if not uri.names_object(): |
| 138 raise CommandException('"%s" command must specify objects.' % |
| 139 self.command_name) |
| 140 did_some_work = True |
| 141 if show_header: |
| 142 if printed_one: |
| 143 print |
| 144 print '==> %s <==' % uri.__str__() |
| 145 printed_one = True |
| 146 key = uri.get_key(False, self.headers) |
| 147 if not parse_versions: |
| 148 key.generation = None |
| 149 key.get_file(cat_outfd, self.headers) |
| 150 sys.stdout = cat_outfd |
| 151 if not did_some_work: |
| 152 raise CommandException('No URIs matched') |
| 153 |
| 154 return 0 |
OLD | NEW |