OLD | NEW |
(Empty) | |
| 1 # Copyright 2010 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 |
| 15 """Static data and helper functions.""" |
| 16 |
| 17 import math |
| 18 import re |
| 19 import sys |
| 20 import time |
| 21 |
| 22 import boto |
| 23 from third_party.retry_decorator.decorators import retry |
| 24 |
| 25 # 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 # state instead of Command instance state because the top-level gsutil code |
| 28 # needs to check it. |
| 29 HAVE_OAUTH2 = False |
| 30 try: |
| 31 from oauth2_plugin import oauth2_helper |
| 32 HAVE_OAUTH2 = True |
| 33 except ImportError: |
| 34 pass |
| 35 |
| 36 TWO_MB = 2 * 1024 * 1024 |
| 37 |
| 38 NO_MAX = sys.maxint |
| 39 |
| 40 # Binary exponentiation strings. |
| 41 _EXP_STRINGS = [ |
| 42 (0, 'B', 'bit'), |
| 43 (10, 'KB', 'kbit'), |
| 44 (20, 'MB', 'Mbit'), |
| 45 (30, 'GB', 'Gbit'), |
| 46 (40, 'TB', 'Tbit'), |
| 47 (50, 'PB', 'Pbit'), |
| 48 ] |
| 49 |
| 50 # Detect platform types. |
| 51 IS_WINDOWS = 'win32' in str(sys.platform).lower() |
| 52 IS_LINUX = 'linux' in str(sys.platform).lower() |
| 53 IS_OSX = 'darwin' in str(sys.platform).lower() |
| 54 |
| 55 Retry = retry |
| 56 |
| 57 # Enum class for specifying listing style. |
| 58 class ListingStyle(object): |
| 59 SHORT = 'SHORT' |
| 60 LONG = 'LONG' |
| 61 LONG_LONG = 'LONG_LONG' |
| 62 |
| 63 |
| 64 def HasConfiguredCredentials(): |
| 65 """Determines if boto credential/config file exists.""" |
| 66 config = boto.config |
| 67 has_goog_creds = (config.has_option('Credentials', 'gs_access_key_id') and |
| 68 config.has_option('Credentials', 'gs_secret_access_key')) |
| 69 has_amzn_creds = (config.has_option('Credentials', 'aws_access_key_id') and |
| 70 config.has_option('Credentials', 'aws_secret_access_key')) |
| 71 has_oauth_creds = (HAVE_OAUTH2 and |
| 72 config.has_option('Credentials', 'gs_oauth2_refresh_token')) |
| 73 has_auth_plugins = config.has_option('Plugin', 'plugin_directory') |
| 74 return (has_goog_creds or has_amzn_creds or has_oauth_creds |
| 75 or has_auth_plugins) |
| 76 |
| 77 |
| 78 def _RoundToNearestExponent(num): |
| 79 i = 0 |
| 80 while i+1 < len(_EXP_STRINGS) and num >= (2 ** _EXP_STRINGS[i+1][0]): |
| 81 i += 1 |
| 82 return i, round(float(num) / 2 ** _EXP_STRINGS[i][0], 2) |
| 83 |
| 84 def MakeHumanReadable(num): |
| 85 """Generates human readable string for a number of bytes. |
| 86 |
| 87 Args: |
| 88 num: The number, in bytes. |
| 89 |
| 90 Returns: |
| 91 A string form of the number using size abbreviations (KB, MB, etc.). |
| 92 """ |
| 93 i, rounded_val = _RoundToNearestExponent(num) |
| 94 return '%s %s' % (rounded_val, _EXP_STRINGS[i][1]) |
| 95 |
| 96 def MakeBitsHumanReadable(num): |
| 97 """Generates human readable string for a number of bits. |
| 98 |
| 99 Args: |
| 100 num: The number, in bits. |
| 101 |
| 102 Returns: |
| 103 A string form of the number using bit size abbreviations (kbit, Mbit, etc.) |
| 104 """ |
| 105 i, rounded_val = _RoundToNearestExponent(num) |
| 106 return '%s %s' % (rounded_val, _EXP_STRINGS[i][2]) |
| 107 |
| 108 def Percentile(values, percent, key=lambda x:x): |
| 109 """Find the percentile of a list of values. |
| 110 |
| 111 Taken from: http://code.activestate.com/recipes/511478/ |
| 112 |
| 113 Args: |
| 114 values: a list of numeric values. Note that the values MUST BE already |
| 115 sorted. |
| 116 percent: a float value from 0.0 to 1.0. |
| 117 key: optional key function to compute value from each element of the list |
| 118 of values. |
| 119 |
| 120 Returns: |
| 121 The percentile of the values. |
| 122 """ |
| 123 if not values: |
| 124 return None |
| 125 k = (len(values) - 1) * percent |
| 126 f = math.floor(k) |
| 127 c = math.ceil(k) |
| 128 if f == c: |
| 129 return key(values[int(k)]) |
| 130 d0 = key(values[int(f)]) * (c-k) |
| 131 d1 = key(values[int(c)]) * (k-f) |
| 132 return d0 + d1 |
| 133 |
| 134 def ExtractErrorDetail(e): |
| 135 """Extract <Details> text from XML content. |
| 136 |
| 137 Args: |
| 138 e: The GSResponseError that includes XML to be parsed. |
| 139 |
| 140 Returns: |
| 141 (exception_name, d), where d is <Details> text or None if not found. |
| 142 """ |
| 143 exc_name_parts = re.split("[\.']", str(type(e))) |
| 144 if len(exc_name_parts) < 2: |
| 145 # Shouldn't happen, but have fallback in case. |
| 146 exc_name = str(type(e)) |
| 147 else: |
| 148 exc_name = exc_name_parts[-2] |
| 149 if not hasattr(e, 'body'): |
| 150 return (exc_name, None) |
| 151 detail_start = e.body.find('<Details>') |
| 152 detail_end = e.body.find('</Details>') |
| 153 if detail_start != -1 and detail_end != -1: |
| 154 return (exc_name, e.body[detail_start+9:detail_end]) |
| 155 return (exc_name, None) |
OLD | NEW |