OLD | NEW |
(Empty) | |
| 1 # Copyright 2011 Google Inc. All Rights Reserved. |
| 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 |
| 57 |
| 58 class CatCommand(Command): |
| 59 """Implementation of gsutil cat command.""" |
| 60 |
| 61 # Command specification (processed by parent class). |
| 62 command_spec = { |
| 63 # Name of command. |
| 64 COMMAND_NAME : 'cat', |
| 65 # List of command name aliases. |
| 66 COMMAND_NAME_ALIASES : [], |
| 67 # Min number of args required by this command. |
| 68 MIN_ARGS : 0, |
| 69 # Max number of args required by this command, or NO_MAX. |
| 70 MAX_ARGS : NO_MAX, |
| 71 # Getopt-style string specifying acceptable sub args. |
| 72 SUPPORTED_SUB_ARGS : 'hv', |
| 73 # True if file URIs acceptable for this command. |
| 74 FILE_URIS_OK : False, |
| 75 # True if provider-only URIs acceptable for this command. |
| 76 PROVIDER_URIS_OK : False, |
| 77 # Index in args of first URI arg. |
| 78 URIS_START_ARG : 0, |
| 79 # True if must configure gsutil before running command. |
| 80 CONFIG_REQUIRED : True, |
| 81 } |
| 82 help_spec = { |
| 83 # Name of command or auxiliary help info for which this help applies. |
| 84 HELP_NAME : 'cat', |
| 85 # List of help name aliases. |
| 86 HELP_NAME_ALIASES : [], |
| 87 # Type of help: |
| 88 HELP_TYPE : HelpType.COMMAND_HELP, |
| 89 # One line summary of this help. |
| 90 HELP_ONE_LINE_SUMMARY : 'Concatenate object content to stdout', |
| 91 # The full help text. |
| 92 HELP_TEXT : _detailed_help_text, |
| 93 } |
| 94 |
| 95 # Command entry point. |
| 96 def RunCommand(self): |
| 97 show_header = False |
| 98 if self.sub_opts: |
| 99 for o, unused_a in self.sub_opts: |
| 100 if o == '-h': |
| 101 show_header = True |
| 102 elif o == '-v': |
| 103 self.THREADED_LOGGER.info('WARNING: The %s -v option is no longer' |
| 104 ' needed, and will eventually be removed.\n' |
| 105 % self.command_name) |
| 106 |
| 107 printed_one = False |
| 108 # We manipulate the stdout so that all other data other than the Object |
| 109 # contents go to stderr. |
| 110 cat_outfd = sys.stdout |
| 111 sys.stdout = sys.stderr |
| 112 did_some_work = False |
| 113 |
| 114 for uri_str in self.args: |
| 115 for uri in self.WildcardIterator(uri_str).IterUris(): |
| 116 if not uri.names_object(): |
| 117 raise CommandException('"%s" command must specify objects.' % |
| 118 self.command_name) |
| 119 did_some_work = True |
| 120 if show_header: |
| 121 if printed_one: |
| 122 print |
| 123 print '==> %s <==' % uri.__str__() |
| 124 printed_one = True |
| 125 key = uri.get_key(False, self.headers) |
| 126 key.get_file(cat_outfd, self.headers) |
| 127 sys.stdout = cat_outfd |
| 128 if not did_some_work: |
| 129 raise CommandException('No URIs matched') |
| 130 |
| 131 return 0 |
OLD | NEW |