|
| 1 | +import os |
| 2 | +import shutil |
| 3 | + |
| 4 | +from boltons import funcutils as bfu |
| 5 | +from google.cloud import storage as gs |
| 6 | +from joblib.disk import mkdirp |
| 7 | +from memoized_property import memoized_property |
| 8 | + |
| 9 | +from . import blobstores as bs |
| 10 | + |
| 11 | + |
| 12 | +# TODO: catch and retry w/new client on |
| 13 | +# BrokenPipeError: [Errno 32] Broken pipe |
| 14 | +# ConnectionResetError: [Errno 54] Connection reset by peer |
| 15 | +# more? |
| 16 | + |
| 17 | +def retry(f, max_attempts=2): |
| 18 | + |
| 19 | + @bfu.wraps(f) |
| 20 | + def with_retry(store, *args, **kargs): |
| 21 | + actual_attempts = 0 |
| 22 | + while True: |
| 23 | + try: |
| 24 | + return f(store, *args, **kargs) |
| 25 | + except (BrokenPipeError, ConnectionError) as e: |
| 26 | + actual_attempts +=1 |
| 27 | + if actual_attempts >= max_attempts: |
| 28 | + raise e |
| 29 | + else: |
| 30 | + store._setup_client() |
| 31 | + return with_retry |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +class GSStore(bs.RemoteStore): |
| 36 | + def __init__(self, cachedir, bucket, basepath='', project=None, |
| 37 | + read=True, write=True, read_through_write=True, |
| 38 | + delete=False, on_duplicate_key='skip', cleanup_cachedir=False, |
| 39 | + always_check_remote=False): |
| 40 | + """ |
| 41 | + Parameters |
| 42 | + ---------- |
| 43 | + always_check_remote : bool |
| 44 | + When True GS (Google Storage) will be checked with every __contains__ call. Otherwise it will |
| 45 | + short-circuit if the blob is found in the cachedir. For performance reasons this |
| 46 | + should always be set to False. The only reason why you would want to use this |
| 47 | + is if you are using a GSStore and a DiskStore in a ChainedStore together for |
| 48 | + some reason. Since the GSStore basically doubles as a DiskStore with it's cachedir |
| 49 | + chaining the two doesn't really make sense though. |
| 50 | + """ |
| 51 | + super(GSStore, self).__init__(always_check_remote=always_check_remote, |
| 52 | + cachedir = cachedir, |
| 53 | + basepath = basepath, |
| 54 | + cleanup_cachedir = cleanup_cachedir, |
| 55 | + read=read, write=write, read_through_write=read_through_write, |
| 56 | + delete=delete, on_duplicate_key=on_duplicate_key) |
| 57 | + |
| 58 | + self.bucket_name = bucket |
| 59 | + self.project = project |
| 60 | + |
| 61 | + def _setup_client(self): |
| 62 | + del self._client |
| 63 | + del self._bucket |
| 64 | + # force re-memoization |
| 65 | + assert self.bucket is not None |
| 66 | + |
| 67 | + @memoized_property |
| 68 | + def client(self): |
| 69 | + return gs.Client(project=self.project) |
| 70 | + |
| 71 | + @memoized_property |
| 72 | + def bucket(self): |
| 73 | + return self.client.get_bucket(self.bucket_name) |
| 74 | + |
| 75 | + @retry |
| 76 | + def _exists(self, path): |
| 77 | + blobs = list(self.bucket.list_blobs(prefix=path)) |
| 78 | + return len(blobs) == 1 |
| 79 | + |
| 80 | + @retry |
| 81 | + def _delete_remote(self, path): |
| 82 | + self.blob(path).delete() |
| 83 | + |
| 84 | + def _blob(self, path): |
| 85 | + return self._bucket.blob(path) |
| 86 | + |
| 87 | + @retry |
| 88 | + def _upload_file(self, filename, path): |
| 89 | + self._blob(path).upload_from_filename(filename) |
| 90 | + |
| 91 | + @retry |
| 92 | + def _download_file(self, remote_path, dest_filename): |
| 93 | + self._blob(remote_path).download_to_filename(dest_filename) |
0 commit comments