Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
Please comment that this is mostly documented in g
Renato Mangini (chromium)
2014/01/31 02:29:04
Done.
| |
| 5 from third_party.cloudstorage import cloudstorage_api | |
| 6 from third_party.cloudstorage import common | |
| 7 from third_party.cloudstorage import errors | |
| 8 | |
| 9 from docs_server_utils import StringIdentity | |
| 10 from file_system import FileSystem, FileNotFoundError, StatInfo | |
| 11 from future import Gettable, Future | |
| 12 | |
| 13 import logging | |
| 14 | |
| 15 def _ReadFile(filename): | |
| 16 try: | |
| 17 with cloudstorage_api.open(filename, 'r') as f: | |
| 18 return f.read() | |
| 19 except errors.Error as e: | |
| 20 raise FileNotFoundError('Read failed for %s: %s' % (filename, e)) | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
Usually traceback.format_exc() is a better context
Renato Mangini (chromium)
2014/01/31 02:29:04
Done.
Jeffrey Yasskin
2014/02/03 22:12:56
Please do this everywhere you're currently using %
| |
| 21 | |
| 22 def _ListDir(dir_name): | |
| 23 all_files = [] | |
| 24 try: | |
| 25 files = cloudstorage_api.listbucket(dir_name) | |
| 26 except errors.Error as e: | |
| 27 raise FileNotFoundError('cloudstorage.listbucket failed for %s: %s' % | |
| 28 (dir_name, e)) | |
| 29 for os_path in files: | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
Maybe just "return [os_path.filename for os_path i
Renato Mangini (chromium)
2014/01/31 02:29:04
Done.
| |
| 30 all_files.append(os_path.filename) | |
| 31 return all_files | |
| 32 | |
| 33 def _CreateStatInfo(bucket, path): | |
| 34 bucket = "/%s" % bucket | |
| 35 full_path = '/'.join( (bucket, path.lstrip('/')) ) | |
| 36 try: | |
| 37 if full_path.endswith('/'): | |
| 38 full_path = full_path.rstrip('/'); | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
"In this mode, the "path_prefix" argument should e
Renato Mangini (chromium)
2014/01/31 02:29:04
Done.
| |
| 39 child_versions = dict() | |
| 40 version = 0 | |
| 41 for _file in cloudstorage_api.listbucket(full_path, delimiter="/"): | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
Be consistent about ' vs ". In this file, it looks
Renato Mangini (chromium)
2014/01/31 02:29:04
Done.
| |
| 42 if not _file.is_dir: | |
| 43 # GCS doesn't have metadata for dirs | |
| 44 child_stat = cloudstorage_api.stat('%s' % _file.filename).st_ctime | |
| 45 filename = _file.filename[len(bucket)+1:] | |
| 46 child_versions[filename] = child_stat | |
| 47 version = max(version, child_stat) | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
Please comment whether you want the maximum st_cti
Renato Mangini (chromium)
2014/01/31 02:29:04
Done.
| |
| 48 else: | |
| 49 child_versions = None | |
| 50 version = cloudstorage_api.stat(full_path).st_ctime | |
| 51 return StatInfo(version, child_versions) | |
| 52 except (TypeError, errors.Error) as e: | |
| 53 raise FileNotFoundError('cloudstorage.stat failed for %s: %s' % (path, e)) | |
| 54 | |
| 55 | |
| 56 class CloudStorageFileSystem(FileSystem): | |
| 57 '''FileSystem implementation which fetches resources from Google Cloud Storage | |
| 58 ''' | |
| 59 def __init__(self, bucket, debug_access_token=None, debug_bucket_prefix=None): | |
| 60 self._bucket = bucket | |
| 61 if debug_access_token: | |
| 62 logging.debug('gcs: using debug access token: %s' % debug_access_token) | |
| 63 common.set_access_token(debug_access_token) | |
| 64 if debug_bucket_prefix: | |
| 65 logging.debug('gcs: prefixing all bucket names with %s' % | |
| 66 debug_bucket_prefix) | |
| 67 self._bucket = debug_bucket_prefix + self._bucket | |
| 68 | |
| 69 def Read(self, paths): | |
| 70 def resolve(): | |
| 71 try: | |
| 72 result = {} | |
| 73 for path in paths: | |
| 74 full_path = '/%s/%s' % (self._bucket, path.lstrip('/')) | |
| 75 logging.debug('gcs: requested path %s, reading %s' % (path, full_path) ) | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
We have an 80-column limit for Chrome Python, righ
Renato Mangini (chromium)
2014/01/31 02:29:04
Done.
| |
| 76 if path == '' or path.endswith('/'): | |
| 77 result[path] = _ListDir(full_path) | |
| 78 else: | |
| 79 result[path] = _ReadFile(full_path) | |
| 80 return result | |
| 81 except errors.AuthorizationError as authError: | |
| 82 self._warnAboutAuthError() | |
| 83 raise authError | |
| 84 | |
| 85 return Future(delegate=Gettable(resolve)) | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
There's no benefit to returning a delayed Future i
Renato Mangini (chromium)
2014/01/31 02:29:04
But by splitting open and read I will loose the be
Jeffrey Yasskin
2014/02/03 22:12:56
I think so, given all the fixes Ben had to make to
| |
| 86 | |
| 87 def Refresh(self): | |
| 88 return Future(value=()) | |
| 89 | |
| 90 def Stat(self, path): | |
| 91 try: | |
| 92 return _CreateStatInfo(self._bucket, path) | |
| 93 except errors.AuthorizationError as authError: | |
| 94 self._warnAboutAuthError() | |
| 95 raise authError | |
|
Jeffrey Yasskin
2014/01/29 01:08:08
You should just use "raise" here in order to keep
Renato Mangini (chromium)
2014/01/31 02:29:04
Done.
Jeffrey Yasskin
2014/02/03 22:12:56
Not done.
| |
| 96 | |
| 97 def GetIdentity(self): | |
| 98 return '@'.join((self.__class__.__name__, StringIdentity(self._bucket))) | |
| 99 | |
| 100 def __repr__(self): | |
| 101 return 'LocalFileSystem(%s)' % self._bucket | |
| 102 | |
| 103 def _warnAboutAuthError(self): | |
| 104 logging.warn(("Authentication error on Cloud Storage. Check if your" | |
| 105 " appengine project has permissions to Read the GCS" | |
| 106 " buckets. If you are running a local appengine server," | |
| 107 " you need to set an access_token in" | |
| 108 " local_debug/gcs_debug.conf." | |
| 109 " Remember that this token expires in less than 10" | |
| 110 " minutes, so keep it updated. See" | |
| 111 " gcs_file_system_provider.py for instructions.")); | |
| OLD | NEW |