OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # coding=utf8 |
| 3 # Copyright 2011 Google Inc. All Rights Reserved. |
| 4 # |
| 5 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 # you may not use this file except in compliance with the License. |
| 7 # You may obtain a copy of the License at |
| 8 # |
| 9 # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 # |
| 11 # Unless required by applicable law or agreed to in writing, software |
| 12 # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 # See the License for the specific language governing permissions and |
| 15 # limitations under the License. |
| 16 |
| 17 """Class that runs a named gsutil command.""" |
| 18 |
| 19 import boto |
| 20 import os |
| 21 |
| 22 from boto.storage_uri import BucketStorageUri |
| 23 from gslib.command import Command |
| 24 from gslib.command import COMMAND_NAME |
| 25 from gslib.command import COMMAND_NAME_ALIASES |
| 26 from gslib.exception import CommandException |
| 27 |
| 28 |
| 29 class CommandRunner(object): |
| 30 |
| 31 def __init__(self, gsutil_bin_dir, boto_lib_dir, config_file_list, |
| 32 gsutil_ver, bucket_storage_uri_class=BucketStorageUri): |
| 33 """ |
| 34 Args: |
| 35 gsutil_bin_dir: Bin dir from which gsutil is running. |
| 36 boto_lib_dir: Lib dir where boto runs. |
| 37 config_file_list: Config file list returned by _GetBotoConfigFileList(). |
| 38 gsutil_ver: Version string of currently running gsutil command. |
| 39 bucket_storage_uri_class: Class to instantiate for cloud StorageUris. |
| 40 Settable for testing/mocking. |
| 41 """ |
| 42 self.gsutil_bin_dir = gsutil_bin_dir |
| 43 self.boto_lib_dir = boto_lib_dir |
| 44 self.config_file_list = config_file_list |
| 45 self.gsutil_ver = gsutil_ver |
| 46 self.bucket_storage_uri_class = bucket_storage_uri_class |
| 47 self.command_map = self._LoadCommandMap() |
| 48 |
| 49 def _LoadCommandMap(self): |
| 50 """Returns dict mapping each command_name to implementing class.""" |
| 51 # Walk gslib/commands and find all commands. |
| 52 commands_dir = os.path.join(self.gsutil_bin_dir, 'gslib', 'commands') |
| 53 for f in os.listdir(commands_dir): |
| 54 # Handles no-extension files, etc. |
| 55 (module_name, ext) = os.path.splitext(f) |
| 56 if ext == '.py': |
| 57 __import__('gslib.commands.%s' % module_name) |
| 58 command_map = {} |
| 59 # Only include Command subclasses in the dict. |
| 60 for command in Command.__subclasses__(): |
| 61 command_map[command.command_spec[COMMAND_NAME]] = command |
| 62 for command_name_aliases in command.command_spec[COMMAND_NAME_ALIASES]: |
| 63 command_map[command_name_aliases] = command |
| 64 return command_map |
| 65 |
| 66 def RunNamedCommand(self, command_name, args=None, headers=None, debug=0, |
| 67 parallel_operations=False, test_method=None): |
| 68 """Runs the named command. Used by gsutil main, commands built atop |
| 69 other commands, and tests . |
| 70 |
| 71 Args: |
| 72 command_name: The name of the command being run. |
| 73 args: Command-line args (arg0 = actual arg, not command name ala bash). |
| 74 headers: Dictionary containing optional HTTP headers to pass to boto. |
| 75 debug: Debug level to pass in to boto connection (range 0..3). |
| 76 parallel_operations: Should command operations be executed in parallel? |
| 77 test_method: Optional general purpose method for testing purposes. |
| 78 Application and semantics of this method will vary by |
| 79 command and test type. |
| 80 |
| 81 Raises: |
| 82 CommandException: if errors encountered. |
| 83 """ |
| 84 if not args: |
| 85 args = [] |
| 86 |
| 87 # Include api_version header in all commands. |
| 88 api_version = boto.config.get_value('GSUtil', 'default_api_version', '1') |
| 89 if not headers: |
| 90 headers = {} |
| 91 headers['x-goog-api-version'] = api_version |
| 92 |
| 93 if command_name not in self.command_map: |
| 94 raise CommandException('Invalid command "%s".' % command_name) |
| 95 command_class = self.command_map[command_name] |
| 96 command_inst = command_class(self, args, headers, debug, |
| 97 parallel_operations, self.gsutil_bin_dir, |
| 98 self.boto_lib_dir, self.config_file_list, |
| 99 self.gsutil_ver, self.bucket_storage_uri_class, |
| 100 test_method) |
| 101 return command_inst.RunCommand() |
OLD | NEW |