Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: gslib/third_party/storage_apitools/util.py

Issue 698893003: Update checked in version of gsutil to version 4.6 (Closed) Base URL: http://dart.googlecode.com/svn/third_party/gsutil/
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gslib/third_party/storage_apitools/transfer.py ('k') | gslib/thread_pool.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 # Copyright 2014 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 """Assorted utilities shared between parts of apitools."""
15
16 import collections
17 import httplib
18 import os
19 import random
20 import types
21 import urllib2
22
23 from gslib.third_party.storage_apitools import exceptions
24
25 __all__ = [
26 'DetectGae',
27 'DetectGce',
28 ]
29
30
31 def DetectGae():
32 """Determine whether or not we're running on GAE.
33
34 This is based on:
35 https://developers.google.com/appengine/docs/python/#The_Environment
36
37 Returns:
38 True iff we're running on GAE.
39 """
40 server_software = os.environ.get('SERVER_SOFTWARE', '')
41 return (server_software.startswith('Development/') or
42 server_software.startswith('Google App Engine/'))
43
44
45 def DetectGce():
46 """Determine whether or not we're running on GCE.
47
48 This is based on:
49 https://developers.google.com/compute/docs/instances#dmi
50
51 Returns:
52 True iff we're running on a GCE instance.
53 """
54 try:
55 o = urllib2.urlopen('http://metadata.google.internal')
56 except urllib2.URLError:
57 return False
58 return o.getcode() == httplib.OK
59
60
61 def NormalizeScopes(scope_spec):
62 """Normalize scope_spec to a set of strings."""
63 if isinstance(scope_spec, types.StringTypes):
64 return set(scope_spec.split(' '))
65 elif isinstance(scope_spec, collections.Iterable):
66 return set(scope_spec)
67 raise exceptions.TypecheckError(
68 'NormalizeScopes expected string or iterable, found %s' % (
69 type(scope_spec),))
70
71
72 def Typecheck(arg, arg_type, msg=None):
73 if not isinstance(arg, arg_type):
74 if msg is None:
75 if isinstance(arg_type, tuple):
76 msg = 'Type of arg is "%s", not one of %r' % (type(arg), arg_type)
77 else:
78 msg = 'Type of arg is "%s", not "%s"' % (type(arg), arg_type)
79 raise exceptions.TypecheckError(msg)
80 return arg
81
82
83 def CalculateWaitForRetry(retry_attempt, max_wait=60):
84 """Calculates amount of time to wait before a retry attempt.
85
86 Wait time grows exponentially with the number of attempts.
87 A random amount of jitter is added to spread out retry attempts from different
88 clients.
89
90 Args:
91 retry_attempt: Retry attempt counter.
92 max_wait: Upper bound for wait time.
93
94 Returns:
95 Amount of time to wait before retrying request.
96 """
97
98 wait_time = 2 ** retry_attempt
99 max_jitter = (2 ** retry_attempt) / 2
100 return min(wait_time + random.randrange(-max_jitter, max_jitter), max_wait)
OLDNEW
« no previous file with comments | « gslib/third_party/storage_apitools/transfer.py ('k') | gslib/thread_pool.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698