| OLD | NEW |
| 1 # Copyright 2011 Google Inc. | 1 # Copyright 2011 Google Inc. |
| 2 # | 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with 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 | 5 # You may obtain a copy of the License at |
| 6 # | 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and | 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. | 13 # limitations under the License. |
| 14 | 14 |
| 15 import boto | 15 import boto |
| 16 |
| 16 from gslib.exception import ProjectIdException | 17 from gslib.exception import ProjectIdException |
| 17 from gslib.wildcard_iterator import WILDCARD_BUCKET_ITERATOR | 18 from gslib.wildcard_iterator import WILDCARD_BUCKET_ITERATOR |
| 18 | 19 |
| 19 GOOG_PROJ_ID_HDR = 'x-goog-project-id' | 20 GOOG_PROJ_ID_HDR = 'x-goog-project-id' |
| 20 | 21 |
| 21 | 22 |
| 22 class ProjectIdHandler(object): | 23 class ProjectIdHandler(object): |
| 23 """Google Project ID header handling.""" | 24 """Google Project ID header handling.""" |
| 24 | 25 |
| 25 def __init__(self): | 26 def __init__(self): |
| 26 """Instantiates Project ID handler. Call after boto config file loaded.""" | 27 """Instantiates Project ID handler. Call after boto config file loaded.""" |
| 27 config = boto.config | 28 config = boto.config |
| 28 self.project_id = config.get_value('GSUtil', 'default_project_id', None) | 29 self.project_id = config.get_value('GSUtil', 'default_project_id') |
| 29 | 30 |
| 30 def SetProjectId(self, project_id): | 31 def SetProjectId(self, project_id): |
| 31 """Overrides project ID value from config file default. | 32 """Overrides project ID value from config file default. |
| 32 | 33 |
| 33 Args: | 34 Args: |
| 34 project_id: project_id to use | 35 project_id: Project ID to use. |
| 35 """ | 36 """ |
| 36 self.project_id = project_id | 37 self.project_id = project_id |
| 37 | 38 |
| 38 def FillInProjectHeaderIfNeeded(self, command, uri, headers): | 39 def FillInProjectHeaderIfNeeded(self, command, uri, headers): |
| 39 """Fills project ID header into headers if defined and applicable. | 40 """Fills project ID header into headers if defined and applicable. |
| 40 | 41 |
| 41 Args: | 42 Args: |
| 42 command: The command being run. | 43 command: The command being run. |
| 43 uri: The URI against which this command is being run. | 44 uri: The URI against which this command is being run. |
| 44 headers: dictionary containing optional HTTP headers to pass to boto. | 45 headers: Dictionary containing optional HTTP headers to pass to boto. |
| 45 Must not be None. | 46 Must not be None. |
| 46 """ | 47 """ |
| 47 | 48 |
| 48 # We only include the project ID header if it's a GS URI and a project_id | 49 # We only include the project ID header if it's a GS URI and a project_id |
| 49 # was specified and | 50 # was specified and |
| 50 # (it's an 'mb' command or | 51 # (it's an 'mb', 'disablelogging, or 'enablelogging' command |
| 51 # (an 'ls' command that doesn't specify a bucket or a wildcard bucket itera
tor)). | 52 # (an 'ls' command that doesn't specify a bucket or wildcarded bucket)). |
| 52 if (uri.scheme.lower() == 'gs' and self.project_id and | 53 if (uri.scheme.lower() == 'gs' and self.project_id |
| 53 (command == 'mb' or | 54 and (command == 'mb' or command == 'disablelogging' |
| 54 (command == 'ls' and not uri.bucket_name) or | 55 or command == 'enablelogging' |
| 55 (command == WILDCARD_BUCKET_ITERATOR))): | 56 or (command == 'ls' and not uri.names_bucket()) |
| 57 or (command == WILDCARD_BUCKET_ITERATOR))): |
| 58 # Note: check for None (as opposed to "not headers") here beause |
| 59 # it's ok to pass empty headers. |
| 56 if headers is None: | 60 if headers is None: |
| 57 raise ProjectIdException( | 61 raise ProjectIdException( |
| 58 'FillInProjectHeaderIfNeeded called with headers=None') | 62 'FillInProjectHeaderIfNeeded called with headers=None') |
| 59 headers[GOOG_PROJ_ID_HDR] = self.project_id | 63 headers[GOOG_PROJ_ID_HDR] = self.project_id |
| 60 elif headers.has_key(GOOG_PROJ_ID_HDR): | 64 elif headers.has_key(GOOG_PROJ_ID_HDR): |
| 61 del headers[GOOG_PROJ_ID_HDR] | 65 del headers[GOOG_PROJ_ID_HDR] |
| OLD | NEW |