OLD | NEW |
(Empty) | |
| 1 # Copyright 2012 Google Inc. |
| 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 |
| 15 from gslib.command import Command |
| 16 from gslib.command import COMMAND_NAME |
| 17 from gslib.command import COMMAND_NAME_ALIASES |
| 18 from gslib.command import CONFIG_REQUIRED |
| 19 from gslib.command import FILE_URIS_OK |
| 20 from gslib.command import MAX_ARGS |
| 21 from gslib.command import MIN_ARGS |
| 22 from gslib.command import PROVIDER_URIS_OK |
| 23 from gslib.command import SUPPORTED_SUB_ARGS |
| 24 from gslib.command import URIS_START_ARG |
| 25 from gslib.exception import CommandException |
| 26 from gslib.help_provider import HELP_NAME |
| 27 from gslib.help_provider import HELP_NAME_ALIASES |
| 28 from gslib.help_provider import HELP_ONE_LINE_SUMMARY |
| 29 from gslib.help_provider import HELP_TEXT |
| 30 from gslib.help_provider import HelpType |
| 31 from gslib.help_provider import HELP_TYPE |
| 32 import xml |
| 33 |
| 34 _detailed_help_text = (""" |
| 35 <B>SYNOPSIS</B> |
| 36 gsutil getcors uri |
| 37 |
| 38 |
| 39 <B>DESCRIPTION</B> |
| 40 Gets the Cross-Origin Resource Sharing (CORS) configuration for a given |
| 41 bucket. This command is supported for buckets only, not objects and you |
| 42 can get the CORS settings for only one bucket at a time. The output from |
| 43 getcors can be redirected into a file, edited and then updated via the |
| 44 setcors sub-command. The CORS configuration is expressed by an XML document |
| 45 with the following structure: |
| 46 |
| 47 <?xml version="1.0" ?> |
| 48 <CorsConfig> |
| 49 <Cors> |
| 50 <Origins> |
| 51 <Origin>origin1.example.com</Origin> |
| 52 </Origins> |
| 53 <Methods> |
| 54 <Method>GET</Method> |
| 55 </Methods> |
| 56 <ResponseHeaders> |
| 57 <ResponseHeader>Content-Type</ResponseHeader> |
| 58 </ResponseHeaders> |
| 59 </Cors> |
| 60 </CorsConfig> |
| 61 |
| 62 For more info about CORS, see http://www.w3.org/TR/cors/. |
| 63 """) |
| 64 |
| 65 class GetCorsCommand(Command): |
| 66 """Implementation of gsutil getcors command.""" |
| 67 |
| 68 # Command specification (processed by parent class). |
| 69 command_spec = { |
| 70 # Name of command. |
| 71 COMMAND_NAME : 'getcors', |
| 72 # List of command name aliases. |
| 73 COMMAND_NAME_ALIASES : [], |
| 74 # Min number of args required by this command. |
| 75 MIN_ARGS : 1, |
| 76 # Max number of args required by this command, or NO_MAX. |
| 77 MAX_ARGS : 1, |
| 78 # Getopt-style string specifying acceptable sub args. |
| 79 SUPPORTED_SUB_ARGS : '', |
| 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 : 'getcors', |
| 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 : 'Get a bucket\'s CORS XML document', |
| 98 # The full help text. |
| 99 HELP_TEXT : _detailed_help_text, |
| 100 } |
| 101 |
| 102 # Command entry point. |
| 103 def RunCommand(self): |
| 104 # Wildcarding is allowed but must resolve to just one bucket. |
| 105 uris = list(self.WildcardIterator(self.args[0]).IterUris()) |
| 106 if len(uris) == 0: |
| 107 raise CommandException('No URIs matched') |
| 108 if len(uris) != 1: |
| 109 raise CommandException('%s matched more than one URI, which is not\n' |
| 110 'allowed by the %s command' % (self.args[0], self.command_name)) |
| 111 uri = uris[0] |
| 112 if not uri.names_bucket(): |
| 113 raise CommandException('"%s" command must specify a bucket' % |
| 114 self.command_name) |
| 115 cors = uri.get_cors(False, self.headers) |
| 116 # Pretty-print the XML to make it more easily human editable. |
| 117 parsed_xml = xml.dom.minidom.parseString(cors.to_xml().encode('utf-8')) |
| 118 print parsed_xml.toprettyxml(indent=' ') |
| 119 return 0 |
OLD | NEW |