| Index: gslib/commands/web.py
|
| ===================================================================
|
| --- gslib/commands/web.py (revision 33376)
|
| +++ gslib/commands/web.py (working copy)
|
| @@ -1,3 +1,4 @@
|
| +# -*- coding: utf-8 -*-
|
| # Copyright 2012 Google Inc. All Rights Reserved.
|
| #
|
| # Licensed under the Apache License, Version 2.0 (the "License");
|
| @@ -11,47 +12,37 @@
|
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| # See the License for the specific language governing permissions and
|
| # limitations under the License.
|
| +"""Implementation of website configuration command for buckets."""
|
|
|
| +from __future__ import absolute_import
|
| +
|
| import getopt
|
| import sys
|
|
|
| from gslib.command import Command
|
| -from gslib.command import COMMAND_NAME
|
| -from gslib.command import COMMAND_NAME_ALIASES
|
| -from gslib.command import FILE_URIS_OK
|
| -from gslib.command import MAX_ARGS
|
| -from gslib.command import MIN_ARGS
|
| -from gslib.command import PROVIDER_URIS_OK
|
| -from gslib.command import SUPPORTED_SUB_ARGS
|
| -from gslib.command import URIS_START_ARG
|
| +from gslib.cs_api_map import ApiSelector
|
| from gslib.exception import CommandException
|
| from gslib.help_provider import CreateHelpText
|
| -from gslib.help_provider import HELP_NAME
|
| -from gslib.help_provider import HELP_NAME_ALIASES
|
| -from gslib.help_provider import HELP_ONE_LINE_SUMMARY
|
| -from gslib.help_provider import HELP_TEXT
|
| -from gslib.help_provider import HelpType
|
| -from gslib.help_provider import HELP_TYPE
|
| -from gslib.help_provider import SUBCOMMAND_HELP_TEXT
|
| +from gslib.third_party.storage_apitools import encoding as encoding
|
| +from gslib.third_party.storage_apitools import storage_v1_messages as apitools_messages
|
| from gslib.util import NO_MAX
|
| -from gslib.util import UnaryDictToXml
|
| -from xml.dom.minidom import parseString as XmlParseString
|
|
|
|
|
| _SET_SYNOPSIS = """
|
| - gsutil web set [-m main_page_suffix] [-e error_page] bucket_uri...
|
| + gsutil web set [-m main_page_suffix] [-e error_page] bucket_url...
|
| """
|
|
|
| _GET_SYNOPSIS = """
|
| - gsutil web get bucket_uri
|
| + gsutil web get bucket_url
|
| """
|
|
|
| _SYNOPSIS = _SET_SYNOPSIS + _GET_SYNOPSIS.lstrip('\n')
|
|
|
| _SET_DESCRIPTION = """
|
| <B>SET</B>
|
| - The "gsutil web set" command will allow you to set Website Configuration
|
| - on your bucket(s). The "set" sub-command has the following options:
|
| + The "gsutil web set" command will allow you to configure or disable
|
| + Website Configuration on your bucket(s). The "set" sub-command has the
|
| + following options (leave both options blank to disable):
|
|
|
| <B>SET OPTIONS</B>
|
| -m <index.html> Specifies the object name to serve when a bucket
|
| @@ -67,19 +58,14 @@
|
| _GET_DESCRIPTION = """
|
| <B>GET</B>
|
| The "gsutil web get" command will gets the web semantics configuration for
|
| - a bucket and displays an XML representation of the configuration.
|
| + a bucket and displays a JSON representation of the configuration.
|
|
|
| In Google Cloud Storage, this would look like:
|
|
|
| - <?xml version="1.0" ?>
|
| - <WebsiteConfiguration>
|
| - <MainPageSuffix>
|
| - index.html
|
| - </MainPageSuffix>
|
| - <NotFoundPage>
|
| - 404.html
|
| - </NotFoundPage>
|
| - </WebsiteConfiguration>
|
| + {
|
| + "notFoundPage": "404.html",
|
| + "mainPageSuffix": "index.html"
|
| + }
|
|
|
| """
|
|
|
| @@ -126,103 +112,69 @@
|
| http://example.com/photos would retrieve
|
| http://example.com/photos/index.html.
|
|
|
| - 2. There is just one 404.html page: For example, a GET request for
|
| + 3. There is just one 404.html page: For example, a GET request for
|
| http://example.com/photos/missing would retrieve
|
| http://example.com/404.html, not http://example.com/photos/404.html.
|
|
|
| - 3. For additional details see
|
| + 4. For additional details see
|
| https://developers.google.com/storage/docs/website-configuration.
|
|
|
| The web command has two sub-commands:
|
| """ + _SET_DESCRIPTION + _GET_DESCRIPTION
|
|
|
| -_detailed_help_text = CreateHelpText(_SYNOPSIS, _DESCRIPTION)
|
| +_DETAILED_HELP_TEXT = CreateHelpText(_SYNOPSIS, _DESCRIPTION)
|
|
|
| _get_help_text = CreateHelpText(_GET_SYNOPSIS, _GET_DESCRIPTION)
|
| _set_help_text = CreateHelpText(_SET_SYNOPSIS, _SET_DESCRIPTION)
|
|
|
|
|
| -def BuildGSWebConfig(main_page_suffix=None, not_found_page=None):
|
| - config_body_l = ['<WebsiteConfiguration>']
|
| - if main_page_suffix:
|
| - config_body_l.append('<MainPageSuffix>%s</MainPageSuffix>' %
|
| - main_page_suffix)
|
| - if not_found_page:
|
| - config_body_l.append('<NotFoundPage>%s</NotFoundPage>' %
|
| - not_found_page)
|
| - config_body_l.append('</WebsiteConfiguration>')
|
| - return "".join(config_body_l)
|
| -
|
| -def BuildS3WebConfig(main_page_suffix=None, error_page=None):
|
| - config_body_l = ['<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">']
|
| - if not main_page_suffix:
|
| - raise CommandException('S3 requires main page / index document')
|
| - config_body_l.append('<IndexDocument><Suffix>%s</Suffix></IndexDocument>' %
|
| - main_page_suffix)
|
| - if error_page:
|
| - config_body_l.append('<ErrorDocument><Key>%s</Key></ErrorDocument>' %
|
| - error_page)
|
| - config_body_l.append('</WebsiteConfiguration>')
|
| - return "".join(config_body_l)
|
| -
|
| class WebCommand(Command):
|
| """Implementation of gsutil web command."""
|
|
|
| - # Command specification (processed by parent class).
|
| - command_spec = {
|
| - # Name of command.
|
| - COMMAND_NAME : 'web',
|
| - # List of command name aliases.
|
| - COMMAND_NAME_ALIASES : ['setwebcfg', 'getwebcfg'],
|
| - # Min number of args required by this command.
|
| - MIN_ARGS : 2,
|
| - # Max number of args required by this command, or NO_MAX.
|
| - MAX_ARGS : NO_MAX,
|
| - # Getopt-style string specifying acceptable sub args.
|
| - SUPPORTED_SUB_ARGS : 'm:e:',
|
| - # True if file URIs acceptable for this command.
|
| - FILE_URIS_OK : False,
|
| - # True if provider-only URIs acceptable for this command.
|
| - PROVIDER_URIS_OK : False,
|
| - # Index in args of first URI arg.
|
| - URIS_START_ARG : 1,
|
| - }
|
| - help_spec = {
|
| - # Name of command or auxiliary help info for which this help applies.
|
| - HELP_NAME : 'web',
|
| - # List of help name aliases.
|
| - HELP_NAME_ALIASES : ['getwebcfg', 'setwebcfg'],
|
| - # Type of help)
|
| - HELP_TYPE : HelpType.COMMAND_HELP,
|
| - # One line summary of this help.
|
| - HELP_ONE_LINE_SUMMARY : 'Set a main page and/or error page for one or more buckets',
|
| - # The full help text.
|
| - HELP_TEXT : _detailed_help_text,
|
| - # Help text for sub-commands.
|
| - SUBCOMMAND_HELP_TEXT : {'get' : _get_help_text,
|
| - 'set' : _set_help_text},
|
| - }
|
| -
|
| + # Command specification. See base class for documentation.
|
| + command_spec = Command.CreateCommandSpec(
|
| + 'web',
|
| + command_name_aliases=['setwebcfg', 'getwebcfg'],
|
| + min_args=2,
|
| + max_args=NO_MAX,
|
| + supported_sub_args='m:e:',
|
| + file_url_ok=False,
|
| + provider_url_ok=False,
|
| + urls_start_arg=1,
|
| + gs_api_support=[ApiSelector.XML, ApiSelector.JSON],
|
| + gs_default_api=ApiSelector.JSON,
|
| + )
|
| + # Help specification. See help_provider.py for documentation.
|
| + help_spec = Command.HelpSpec(
|
| + help_name='web',
|
| + help_name_aliases=['getwebcfg', 'setwebcfg'],
|
| + help_type='command_help',
|
| + help_one_line_summary=(
|
| + 'Set a main page and/or error page for one or more buckets'),
|
| + help_text=_DETAILED_HELP_TEXT,
|
| + subcommand_help_text={'get': _get_help_text, 'set': _set_help_text},
|
| + )
|
| +
|
| def _GetWeb(self):
|
| - uri_args = self.args
|
| + """Gets website configuration for a bucket."""
|
| + bucket_url, bucket_metadata = self.GetSingleBucketUrlFromArg(
|
| + self.args[0], bucket_fields=['website'])
|
|
|
| - # Iterate over URIs, expanding wildcards, and getting the website
|
| - # configuration on each.
|
| - some_matched = False
|
| - for uri_str in uri_args:
|
| - for blr in self.WildcardIterator(uri_str):
|
| - uri = blr.GetUri()
|
| - if not uri.names_bucket():
|
| - raise CommandException('URI %s must name a bucket for the %s command'
|
| - % (uri, self.command_name))
|
| - some_matched = True
|
| - sys.stderr.write('Getting website config on %s...\n' % uri)
|
| - web_config_xml = UnaryDictToXml(uri.get_website_config())
|
| - sys.stdout.write(XmlParseString(web_config_xml).toprettyxml())
|
| - if not some_matched:
|
| - raise CommandException('No URIs matched')
|
| + if bucket_url.scheme == 's3':
|
| + sys.stdout.write(self.gsutil_api.XmlPassThroughGetWebsite(
|
| + bucket_url, provider=bucket_url.scheme))
|
| + else:
|
| + if bucket_metadata.website and (bucket_metadata.website.mainPageSuffix or
|
| + bucket_metadata.website.notFoundPage):
|
| + sys.stdout.write(str(encoding.MessageToJson(
|
| + bucket_metadata.website)) + '\n')
|
| + else:
|
| + sys.stdout.write('%s has no website configuration.\n' % bucket_url)
|
|
|
| + return 0
|
| +
|
| def _SetWeb(self):
|
| + """Sets website configuration for a bucket."""
|
| main_page_suffix = None
|
| error_page = None
|
| if self.sub_opts:
|
| @@ -232,28 +184,32 @@
|
| elif o == '-e':
|
| error_page = a
|
|
|
| - uri_args = self.args
|
| + url_args = self.args
|
|
|
| - # Iterate over URIs, expanding wildcards, and setting the website
|
| + website = apitools_messages.Bucket.WebsiteValue(
|
| + mainPageSuffix=main_page_suffix, notFoundPage=error_page)
|
| +
|
| + # Iterate over URLs, expanding wildcards and setting the website
|
| # configuration on each.
|
| some_matched = False
|
| - for uri_str in uri_args:
|
| - for blr in self.WildcardIterator(uri_str):
|
| - uri = blr.GetUri()
|
| - if not uri.names_bucket():
|
| - raise CommandException('URI %s must name a bucket for the %s command'
|
| - % (str(uri), self.command_name))
|
| + for url_str in url_args:
|
| + bucket_iter = self.GetBucketUrlIterFromArg(url_str, bucket_fields=['id'])
|
| + for blr in bucket_iter:
|
| + url = blr.storage_url
|
| some_matched = True
|
| - self.logger.info('Setting website config on %s...', uri)
|
| - uri.set_website_config(main_page_suffix, error_page)
|
| + self.logger.info('Setting website configuration on %s...', blr)
|
| + bucket_metadata = apitools_messages.Bucket(website=website)
|
| + self.gsutil_api.PatchBucket(url.bucket_name, bucket_metadata,
|
| + provider=url.scheme, fields=['id'])
|
| if not some_matched:
|
| - raise CommandException('No URIs matched')
|
| + raise CommandException('No URLs matched')
|
| + return 0
|
|
|
| - # Command entry point.
|
| def RunCommand(self):
|
| + """Command entry point for the web command."""
|
| action_subcommand = self.args.pop(0)
|
| - (self.sub_opts, self.args) = getopt.getopt(self.args,
|
| - self.command_spec[SUPPORTED_SUB_ARGS])
|
| + self.sub_opts, self.args = getopt.getopt(
|
| + self.args, self.command_spec.supported_sub_args)
|
| self.CheckArguments()
|
| if action_subcommand == 'get':
|
| func = self._GetWeb
|
| @@ -261,7 +217,6 @@
|
| func = self._SetWeb
|
| else:
|
| raise CommandException(('Invalid subcommand "%s" for the %s command.\n'
|
| - 'See "gsutil help web".') %
|
| + 'See "gsutil help web".') %
|
| (action_subcommand, self.command_name))
|
| - func()
|
| - return 0
|
| + return func()
|
|
|