| OLD | NEW |
| 1 # Copyright 2010 Google Inc. All Rights Reserved. | 1 # Copyright 2010 Google Inc. All Rights Reserved. |
| 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 """Static data and helper functions.""" | 15 """Static data and helper functions.""" |
| 16 | 16 |
| 17 import math | 17 import math |
| 18 import re | 18 import re |
| 19 import os |
| 19 import sys | 20 import sys |
| 20 import time | 21 import time |
| 21 | 22 |
| 22 import boto | 23 import boto |
| 23 from third_party.retry_decorator.decorators import retry | 24 from third_party.retry_decorator.decorators import retry |
| 24 | 25 |
| 25 # We don't use the oauth2 authentication plugin directly; importing it here | 26 # We don't use the oauth2 authentication plugin directly; importing it here |
| 26 # ensures that it's loaded and available by default. Note: we made this static | 27 # ensures that it's loaded and available by default. Note: we made this static |
| 27 # state instead of Command instance state because the top-level gsutil code | 28 # state instead of Command instance state because the top-level gsutil code |
| 28 # needs to check it. | 29 # needs to check it. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 54 | 55 |
| 55 Retry = retry | 56 Retry = retry |
| 56 | 57 |
| 57 # Enum class for specifying listing style. | 58 # Enum class for specifying listing style. |
| 58 class ListingStyle(object): | 59 class ListingStyle(object): |
| 59 SHORT = 'SHORT' | 60 SHORT = 'SHORT' |
| 60 LONG = 'LONG' | 61 LONG = 'LONG' |
| 61 LONG_LONG = 'LONG_LONG' | 62 LONG_LONG = 'LONG_LONG' |
| 62 | 63 |
| 63 | 64 |
| 64 def HasConfiguredCredentials(): | 65 def HasConfiguredCredentials(bypass_prodaccess): |
| 65 """Determines if boto credential/config file exists.""" | 66 """Determines if boto credential/config file exists.""" |
| 66 config = boto.config | 67 config = boto.config |
| 67 has_goog_creds = (config.has_option('Credentials', 'gs_access_key_id') and | 68 has_goog_creds = (config.has_option('Credentials', 'gs_access_key_id') and |
| 68 config.has_option('Credentials', 'gs_secret_access_key')) | 69 config.has_option('Credentials', 'gs_secret_access_key')) |
| 69 has_amzn_creds = (config.has_option('Credentials', 'aws_access_key_id') and | 70 has_amzn_creds = (config.has_option('Credentials', 'aws_access_key_id') and |
| 70 config.has_option('Credentials', 'aws_secret_access_key')) | 71 config.has_option('Credentials', 'aws_secret_access_key')) |
| 71 has_oauth_creds = (HAVE_OAUTH2 and | 72 has_oauth_creds = (HAVE_OAUTH2 and |
| 72 config.has_option('Credentials', 'gs_oauth2_refresh_token')) | 73 config.has_option('Credentials', 'gs_oauth2_refresh_token')) |
| 73 has_auth_plugins = config.has_option('Plugin', 'plugin_directory') | 74 has_auth_plugins = config.has_option('Plugin', 'plugin_directory') |
| 75 # Pretend prodaccess doesn't exist if --bypass_prodaccess is passed in. |
| 76 has_prodaccess = HasExecutable('prodaccess') and not bypass_prodaccess |
| 74 return (has_goog_creds or has_amzn_creds or has_oauth_creds | 77 return (has_goog_creds or has_amzn_creds or has_oauth_creds |
| 75 or has_auth_plugins) | 78 or has_auth_plugins or has_prodaccess) |
| 76 | 79 |
| 77 | 80 |
| 78 def _RoundToNearestExponent(num): | 81 def _RoundToNearestExponent(num): |
| 79 i = 0 | 82 i = 0 |
| 80 while i+1 < len(_EXP_STRINGS) and num >= (2 ** _EXP_STRINGS[i+1][0]): | 83 while i+1 < len(_EXP_STRINGS) and num >= (2 ** _EXP_STRINGS[i+1][0]): |
| 81 i += 1 | 84 i += 1 |
| 82 return i, round(float(num) / 2 ** _EXP_STRINGS[i][0], 2) | 85 return i, round(float(num) / 2 ** _EXP_STRINGS[i][0], 2) |
| 83 | 86 |
| 84 def MakeHumanReadable(num): | 87 def MakeHumanReadable(num): |
| 85 """Generates human readable string for a number of bytes. | 88 """Generates human readable string for a number of bytes. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 exc_name = str(type(e)) | 149 exc_name = str(type(e)) |
| 147 else: | 150 else: |
| 148 exc_name = exc_name_parts[-2] | 151 exc_name = exc_name_parts[-2] |
| 149 if not hasattr(e, 'body'): | 152 if not hasattr(e, 'body'): |
| 150 return (exc_name, None) | 153 return (exc_name, None) |
| 151 detail_start = e.body.find('<Details>') | 154 detail_start = e.body.find('<Details>') |
| 152 detail_end = e.body.find('</Details>') | 155 detail_end = e.body.find('</Details>') |
| 153 if detail_start != -1 and detail_end != -1: | 156 if detail_start != -1 and detail_end != -1: |
| 154 return (exc_name, e.body[detail_start+9:detail_end]) | 157 return (exc_name, e.body[detail_start+9:detail_end]) |
| 155 return (exc_name, None) | 158 return (exc_name, None) |
| 159 |
| 160 |
| 161 def HasExecutable(filename): |
| 162 """Determines if an executable is available on the system.""" |
| 163 for path in os.environ['PATH'].split(os.pathsep): |
| 164 exe_file = os.path.join(path, filename) |
| 165 if os.path.exists(exe_file) and os.access(exe_file, os.X_OK): |
| 166 return True |
| 167 return False |
| OLD | NEW |