| 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 |
| 37 _detailed_help_text = (""" |
| 38 <B>SYNOPSIS</B> |
| 39 gsutil cat [-h] uri... |
| 40 |
| 41 |
| 42 <B>DESCRIPTION</B> |
| 43 The cat command outputs the contents of one or more URIs to stdout. |
| 44 It is equivalent to doing: |
| 45 |
| 46 gsutil cp uri... - |
| 47 |
| 48 (The final '-' causes gsutil to stream the output to stdout.) |
| 49 |
| 50 |
| 51 <B>OPTIONS</B> |
| 52 -h Prints short header for each object. For example: |
| 53 gsutil cat -h gs://bucket/meeting_notes/2012_Feb/*.txt |
| 54 """) |
| 55 |
| 56 |
| 57 class CatCommand(Command): |
| 58 """Implementation of gsutil cat command.""" |
| 59 |
| 60 # Command specification (processed by parent class). |
| 61 command_spec = { |
| 62 # Name of command. |
| 63 COMMAND_NAME : 'cat', |
| 64 # List of command name aliases. |
| 65 COMMAND_NAME_ALIASES : [], |
| 66 # Min number of args required by this command. |
| 67 MIN_ARGS : 0, |
| 68 # Max number of args required by this command, or NO_MAX. |
| 69 MAX_ARGS : NO_MAX, |
| 70 # Getopt-style string specifying acceptable sub args. |
| 71 SUPPORTED_SUB_ARGS : 'h', |
| 72 # True if file URIs acceptable for this command. |
| 73 FILE_URIS_OK : False, |
| 74 # True if provider-only URIs acceptable for this command. |
| 75 PROVIDER_URIS_OK : False, |
| 76 # Index in args of first URI arg. |
| 77 URIS_START_ARG : 0, |
| 78 # True if must configure gsutil before running command. |
| 79 CONFIG_REQUIRED : True, |
| 80 } |
| 81 help_spec = { |
| 82 # Name of command or auxiliary help info for which this help applies. |
| 83 HELP_NAME : 'cat', |
| 84 # List of help name aliases. |
| 85 HELP_NAME_ALIASES : [], |
| 86 # Type of help: |
| 87 HELP_TYPE : HelpType.COMMAND_HELP, |
| 88 # One line summary of this help. |
| 89 HELP_ONE_LINE_SUMMARY : 'Concatenate object content to stdout', |
| 90 # The full help text. |
| 91 HELP_TEXT : _detailed_help_text, |
| 92 } |
| 93 |
| 94 # Command entry point. |
| 95 def RunCommand(self): |
| 96 show_header = False |
| 97 if self.sub_opts: |
| 98 for o, unused_a in self.sub_opts: |
| 99 if o == '-h': |
| 100 show_header = True |
| 101 |
| 102 printed_one = False |
| 103 # We manipulate the stdout so that all other data other than the Object |
| 104 # contents go to stderr. |
| 105 cat_outfd = sys.stdout |
| 106 sys.stdout = sys.stderr |
| 107 did_some_work = False |
| 108 for uri_str in self.args: |
| 109 for uri in self.exp_handler.WildcardIterator(uri_str).IterUris(): |
| 110 if not uri.names_object(): |
| 111 raise CommandException('"%s" command must specify objects.' % |
| 112 self.command_name) |
| 113 did_some_work = True |
| 114 if show_header: |
| 115 if printed_one: |
| 116 print |
| 117 print '==> %s <==' % uri.__str__() |
| 118 printed_one = True |
| 119 key = uri.get_key(False, self.headers) |
| 120 key.get_file(cat_outfd, self.headers) |
| 121 sys.stdout = cat_outfd |
| 122 if not did_some_work: |
| 123 raise CommandException('No URIs matched') |
| OLD | NEW |