getwebcfg.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. import sys
  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. from xml.dom.minidom import parseString as XmlParseString
  33. _detailed_help_text = ("""
  34. <B>SYNOPSIS</B>
  35. gsutil getwebcfg bucket_uri
  36. <B>DESCRIPTION</B>
  37. The Website Configuration feature enables you to configure a Google Cloud
  38. Storage bucket to simulate the behavior of a static website. You can define
  39. main pages or directory indices (for example, index.html) for buckets and
  40. "directories". Also, you can define a custom error page in case a requested
  41. resource does not exist.
  42. The gsutil getwebcfg command gets the web semantics configuration for a
  43. bucket, and displays an XML representation of the configuration.
  44. In Google Cloud Storage, this would look like:
  45. <?xml version="1.0" ?>
  46. <WebsiteConfiguration>
  47. <MainPageSuffix>
  48. index.html
  49. </MainPageSuffix>
  50. <NotFoundPage>
  51. 404.html
  52. </NotFoundPage>
  53. </WebsiteConfiguration>
  54. """)
  55. class GetWebcfgCommand(Command):
  56. """Implementation of gsutil getwebcfg command."""
  57. # Command specification (processed by parent class).
  58. command_spec = {
  59. # Name of command.
  60. COMMAND_NAME : 'getwebcfg',
  61. # List of command name aliases.
  62. COMMAND_NAME_ALIASES : [],
  63. # Min number of args required by this command.
  64. MIN_ARGS : 1,
  65. # Max number of args required by this command, or NO_MAX.
  66. MAX_ARGS : 1, # Getopt-style string specifying acceptable sub args.
  67. SUPPORTED_SUB_ARGS : '',
  68. # True if file URIs acceptable for this command.
  69. FILE_URIS_OK : False,
  70. # True if provider-only URIs acceptable for this command.
  71. PROVIDER_URIS_OK : False,
  72. # Index in args of first URI arg.
  73. URIS_START_ARG : 1,
  74. # True if must configure gsutil before running command.
  75. CONFIG_REQUIRED : True,
  76. }
  77. help_spec = {
  78. # Name of command or auxiliary help info for which this help applies.
  79. HELP_NAME : 'getwebcfg',
  80. # List of help name aliases.
  81. HELP_NAME_ALIASES : [],
  82. # Type of help)
  83. HELP_TYPE : HelpType.COMMAND_HELP,
  84. # One line summary of this help.
  85. HELP_ONE_LINE_SUMMARY : ('Get the website configuration '
  86. 'for one or more buckets'),
  87. # The full help text.
  88. HELP_TEXT : _detailed_help_text,
  89. }
  90. # Command entry point.
  91. def RunCommand(self):
  92. uri_args = self.args
  93. # Iterate over URIs, expanding wildcards, and getting the website
  94. # configuration on each.
  95. some_matched = False
  96. for uri_str in uri_args:
  97. for blr in self.WildcardIterator(uri_str):
  98. uri = blr.GetUri()
  99. if not uri.names_bucket():
  100. raise CommandException('URI %s must name a bucket for the %s command'
  101. % (str(uri), self.command_name))
  102. some_matched = True
  103. sys.stderr.write('Getting website config on %s...\n' % uri)
  104. _, xml_body = uri.get_website_config()
  105. sys.stdout.write(XmlParseString(xml_body).toprettyxml())
  106. if not some_matched:
  107. raise CommandException('No URIs matched')
  108. return 0