OLD | NEW |
(Empty) | |
| 1 # Copyright 2012 Google Inc. All Rights Reserved. |
| 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 import sys |
| 16 import xml |
| 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 |
| 36 _detailed_help_text = (""" |
| 37 <B>SYNOPSIS</B> |
| 38 gsutil getcors uri |
| 39 |
| 40 |
| 41 <B>DESCRIPTION</B> |
| 42 Gets the Cross-Origin Resource Sharing (CORS) configuration for a given |
| 43 bucket. This command is supported for buckets only, not objects and you |
| 44 can get the CORS settings for only one bucket at a time. The output from |
| 45 getcors can be redirected into a file, edited and then updated via the |
| 46 setcors sub-command. The CORS configuration is expressed by an XML document |
| 47 with the following structure: |
| 48 |
| 49 <?xml version="1.0" ?> |
| 50 <CorsConfig> |
| 51 <Cors> |
| 52 <Origins> |
| 53 <Origin>origin1.example.com</Origin> |
| 54 </Origins> |
| 55 <Methods> |
| 56 <Method>GET</Method> |
| 57 </Methods> |
| 58 <ResponseHeaders> |
| 59 <ResponseHeader>Content-Type</ResponseHeader> |
| 60 </ResponseHeaders> |
| 61 </Cors> |
| 62 </CorsConfig> |
| 63 |
| 64 For more info about CORS, see http://www.w3.org/TR/cors/. |
| 65 """) |
| 66 |
| 67 class GetCorsCommand(Command): |
| 68 """Implementation of gsutil getcors command.""" |
| 69 |
| 70 # Command specification (processed by parent class). |
| 71 command_spec = { |
| 72 # Name of command. |
| 73 COMMAND_NAME : 'getcors', |
| 74 # List of command name aliases. |
| 75 COMMAND_NAME_ALIASES : [], |
| 76 # Min number of args required by this command. |
| 77 MIN_ARGS : 1, |
| 78 # Max number of args required by this command, or NO_MAX. |
| 79 MAX_ARGS : 1, |
| 80 # Getopt-style string specifying acceptable sub args. |
| 81 SUPPORTED_SUB_ARGS : '', |
| 82 # True if file URIs acceptable for this command. |
| 83 FILE_URIS_OK : False, |
| 84 # True if provider-only URIs acceptable for this command. |
| 85 PROVIDER_URIS_OK : False, |
| 86 # Index in args of first URI arg. |
| 87 URIS_START_ARG : 0, |
| 88 # True if must configure gsutil before running command. |
| 89 CONFIG_REQUIRED : True, |
| 90 } |
| 91 help_spec = { |
| 92 # Name of command or auxiliary help info for which this help applies. |
| 93 HELP_NAME : 'getcors', |
| 94 # List of help name aliases. |
| 95 HELP_NAME_ALIASES : [], |
| 96 # Type of help) |
| 97 HELP_TYPE : HelpType.COMMAND_HELP, |
| 98 # One line summary of this help. |
| 99 HELP_ONE_LINE_SUMMARY : 'Get a bucket\'s CORS XML document', |
| 100 # The full help text. |
| 101 HELP_TEXT : _detailed_help_text, |
| 102 } |
| 103 |
| 104 # Command entry point. |
| 105 def RunCommand(self): |
| 106 # Wildcarding is allowed but must resolve to just one bucket. |
| 107 uris = list(self.WildcardIterator(self.args[0]).IterUris()) |
| 108 if len(uris) == 0: |
| 109 raise CommandException('No URIs matched') |
| 110 if len(uris) != 1: |
| 111 raise CommandException('%s matched more than one URI, which is not\n' |
| 112 'allowed by the %s command' % (self.args[0], self.command_name)) |
| 113 uri = uris[0] |
| 114 if not uri.names_bucket(): |
| 115 raise CommandException('"%s" command must specify a bucket' % |
| 116 self.command_name) |
| 117 cors = uri.get_cors(False, self.headers) |
| 118 # Pretty-print the XML to make it more easily human editable. |
| 119 parsed_xml = xml.dom.minidom.parseString(cors.to_xml().encode('utf-8')) |
| 120 sys.stdout.write(parsed_xml.toprettyxml(indent=' ')) |
| 121 return 0 |
OLD | NEW |