OLD | NEW |
(Empty) | |
| 1 # Copyright 2013 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 import posixpath |
| 16 import os.path |
| 17 import time |
| 18 import urllib |
| 19 import urlparse |
| 20 |
| 21 import unittest |
| 22 if not hasattr(unittest.TestCase, 'assertIsNone'): |
| 23 # external dependency unittest2 required for Python <= 2.6 |
| 24 import unittest2 as unittest |
| 25 |
| 26 # Flags for running different types of tests. |
| 27 RUN_INTEGRATION_TESTS = True |
| 28 RUN_UNIT_TESTS = True |
| 29 |
| 30 # Whether the tests are running verbose or not. |
| 31 VERBOSE_OUTPUT = False |
| 32 |
| 33 |
| 34 def _NormalizeURI(uri): |
| 35 """Normalizes the path component of a URI. |
| 36 |
| 37 Examples: |
| 38 gs://foo//bar -> gs://foo/bar |
| 39 gs://foo/./bar -> gs://foo/bar |
| 40 """ |
| 41 # Note: we have to do this dance of changing gs:// to file:// because on |
| 42 # Windows, the urlparse function won't work with URL schemes that are not |
| 43 # known. urlparse('gs://foo/bar') on Windows turns into: |
| 44 # scheme='gs', netloc='', path='//foo/bar' |
| 45 # while on non-Windows platforms, it turns into: |
| 46 # scheme='gs', netloc='foo', path='/bar' |
| 47 uri = uri.replace('gs://', 'file://') |
| 48 parsed = list(urlparse.urlparse(uri)) |
| 49 parsed[2] = posixpath.normpath(parsed[2]) |
| 50 if parsed[2].startswith('//'): |
| 51 # The normpath function doesn't change '//foo' -> '/foo' by design. |
| 52 parsed[2] = parsed[2][1:] |
| 53 unparsed = urlparse.urlunparse(parsed) |
| 54 unparsed = unparsed.replace('file://', 'gs://') |
| 55 return unparsed |
| 56 |
| 57 |
| 58 def ObjectToURI(obj, *suffixes): |
| 59 """Returns the storage URI string for a given StorageUri or file object. |
| 60 |
| 61 Args: |
| 62 obj: The object to get the URI from. Can be a file object, a subclass of |
| 63 boto.storage_uri.StorageURI, or a string. If a string, it is assumed to |
| 64 be a local on-disk path. |
| 65 suffixes: Suffixes to append. For example, ObjectToUri(bucketuri, 'foo') |
| 66 would return the URI for a key name 'foo' inside the given bucket. |
| 67 """ |
| 68 if isinstance(obj, file): |
| 69 return 'file://%s' % os.path.abspath(os.path.join(obj.name, *suffixes)) |
| 70 if isinstance(obj, basestring): |
| 71 return 'file://%s' % os.path.join(obj, *suffixes) |
| 72 uri = obj.uri |
| 73 if suffixes: |
| 74 uri = _NormalizeURI('/'.join([uri] + list(suffixes))) |
| 75 |
| 76 # Storage URIs shouldn't contain a trailing slash. |
| 77 if uri.endswith('/'): |
| 78 uri = uri[:-1] |
| 79 return uri |
OLD | NEW |