OLD | NEW |
(Empty) | |
| 1 # Copyright 2012 Google Inc. |
| 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 """ |
| 16 Class that holds state (bucket_storage_uri_class and debug) needed for |
| 17 instantiating StorageUri objects. The StorageUri func defined in this class |
| 18 uses that state plus gsutil default flag values to instantiate this frequently |
| 19 constructed object with just one param for most cases. |
| 20 """ |
| 21 |
| 22 import boto |
| 23 import re |
| 24 from gslib.exception import CommandException |
| 25 |
| 26 GENERATION_RE = ('(?P<uri_str>.+)#' |
| 27 '(?P<generation>[0-9]+)(\.(?P<meta_generation>[0-9]+))?') |
| 28 VERSION_RE = '(?P<uri_str>.+)#(?P<version_id>.+)' |
| 29 |
| 30 |
| 31 class StorageUriBuilder(object): |
| 32 |
| 33 def __init__(self, debug, bucket_storage_uri_class): |
| 34 """ |
| 35 Args: |
| 36 debug: Debug level to pass in to boto connection (range 0..3). |
| 37 bucket_storage_uri_class: Class to instantiate for cloud StorageUris. |
| 38 Settable for testing/mocking. |
| 39 """ |
| 40 self.bucket_storage_uri_class = bucket_storage_uri_class |
| 41 self.debug = debug |
| 42 |
| 43 def StorageUri(self, uri_str, parse_version=False): |
| 44 """ |
| 45 Instantiates StorageUri using class state and gsutil default flag values. |
| 46 |
| 47 Args: |
| 48 uri_str: StorageUri naming bucket + optional object. |
| 49 parse_version: boolean indicating whether to parse out version/generation |
| 50 information from uri_str. |
| 51 |
| 52 Returns: |
| 53 boto.StorageUri for given uri_str. |
| 54 |
| 55 Raises: |
| 56 InvalidUriError: if uri_str not valid. |
| 57 """ |
| 58 version_id = None |
| 59 generation = None |
| 60 meta_generation = None |
| 61 |
| 62 uri_str_only = uri_str |
| 63 if parse_version: |
| 64 if uri_str.startswith('gs'): |
| 65 match = re.search(GENERATION_RE, uri_str) |
| 66 if not match: |
| 67 raise CommandException( |
| 68 'Generation number expected in uri %s' % uri_str) |
| 69 md = match.groupdict() |
| 70 uri_str_only = md['uri_str'] |
| 71 generation = int(md['generation']) |
| 72 if md['meta_generation']: |
| 73 meta_generation = int(md['meta_generation']) |
| 74 |
| 75 elif uri_str.startswith('s3'): |
| 76 match = re.search(VERSION_RE, uri_str) |
| 77 if not match: |
| 78 raise CommandException('Version ID expected in uri %s' % uri_str) |
| 79 md = match.groupdict() |
| 80 uri_str_only = md['uri_str'] |
| 81 version_id = md['version_id'] |
| 82 |
| 83 else: |
| 84 raise CommandException('Unrecognized provider scheme in uri %s' % |
| 85 uri_str) |
| 86 |
| 87 suri = boto.storage_uri( |
| 88 uri_str_only, 'file', debug=self.debug, validate=False, |
| 89 bucket_storage_uri_class=self.bucket_storage_uri_class, |
| 90 suppress_consec_slashes=False) |
| 91 |
| 92 suri.version_id = version_id |
| 93 suri.generation = generation |
| 94 suri.meta_generation = meta_generation |
| 95 |
| 96 return suri |
OLD | NEW |