OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 from third_party.cloudstorage import cloudstorage_api | 5 from third_party.cloudstorage import cloudstorage_api |
6 from third_party.cloudstorage import common | 6 from third_party.cloudstorage import common |
7 from third_party.cloudstorage import errors | 7 from third_party.cloudstorage import errors |
8 | 8 |
9 from docs_server_utils import StringIdentity | 9 from docs_server_utils import StringIdentity |
10 from file_system import FileSystem, FileNotFoundError, StatInfo | 10 from file_system import FileSystem, FileNotFoundError, StatInfo |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 def _ReadFile(filename): | 30 def _ReadFile(filename): |
31 AssertIsFile(filename) | 31 AssertIsFile(filename) |
32 try: | 32 try: |
33 with cloudstorage_api.open('/' + filename, 'r') as f: | 33 with cloudstorage_api.open('/' + filename, 'r') as f: |
34 return f.read() | 34 return f.read() |
35 except errors.Error: | 35 except errors.Error: |
36 raise FileNotFoundError('Read failed for %s: %s' % (filename, | 36 raise FileNotFoundError('Read failed for %s: %s' % (filename, |
37 traceback.format_exc())) | 37 traceback.format_exc())) |
38 | 38 |
39 def _ListDir(dir_name): | 39 def _ListDir(dir_name, recursive=False): |
40 AssertIsDirectory(dir_name) | 40 AssertIsDirectory(dir_name) |
41 try: | 41 try: |
42 files = cloudstorage_api.listbucket('/' + dir_name) | 42 # The listbucket method uses a prefix approach to simulate hierarchy. |
43 return [os_path.filename.lstrip('/') for os_path in files] | 43 # Calling it with the "delimiter" argument set to '/' gets only files |
| 44 # directly inside the directory, not all recursive content. |
| 45 delimiter = None if recursive else '/' |
| 46 files = cloudstorage_api.listbucket('/' + dir_name, delimiter=delimiter) |
| 47 return [os_path.filename.lstrip('/')[len(dir_name):] for os_path in files] |
44 except errors.Error: | 48 except errors.Error: |
45 raise FileNotFoundError('cloudstorage.listbucket failed for %s: %s' % | 49 raise FileNotFoundError('cloudstorage.listbucket failed for %s: %s' % |
46 (dir_name, traceback.format_exc())) | 50 (dir_name, traceback.format_exc())) |
47 | 51 |
48 def _CreateStatInfo(bucket, path): | 52 def _CreateStatInfo(bucket, path): |
49 full_path = Join(bucket, path) | 53 full_path = Join(bucket, path) |
50 last_commit_file = Join(bucket, LAST_COMMIT_HASH_FILENAME) | 54 last_commit_file = Join(bucket, LAST_COMMIT_HASH_FILENAME) |
51 try: | 55 try: |
52 last_commit = _ReadFile(last_commit_file) | 56 last_commit = _ReadFile(last_commit_file) |
53 if IsDirectory(full_path): | 57 if IsDirectory(full_path): |
54 child_versions = dict() | 58 child_versions = dict((filename, last_commit) |
55 # Fetching stats for all files under full_path, recursively. The | 59 for filename in _ListDir(full_path)) |
56 # listbucket method uses a prefix approach to simulate hierarchy, | |
57 # but calling it without the "delimiter" argument searches for prefix, | |
58 # which means, for directories, everything beneath it. | |
59 for _file in cloudstorage_api.listbucket('/' + full_path): | |
60 filename = _file.filename.lstrip('/')[len(full_path):] | |
61 child_versions[filename] = last_commit | |
62 else: | 60 else: |
63 child_versions = None | 61 child_versions = None |
64 return StatInfo(last_commit, child_versions) | 62 return StatInfo(last_commit, child_versions) |
65 except (TypeError, errors.Error): | 63 except (TypeError, errors.Error): |
66 raise FileNotFoundError('cloudstorage.stat failed for %s: %s' % (path, | 64 raise FileNotFoundError('cloudstorage.stat failed for %s: %s' % (path, |
67 traceback.format_exc())) | 65 traceback.format_exc())) |
68 | 66 |
69 class CloudStorageFileSystem(FileSystem): | 67 class CloudStorageFileSystem(FileSystem): |
70 '''FileSystem implementation which fetches resources from Google Cloud | 68 '''FileSystem implementation which fetches resources from Google Cloud |
71 Storage. | 69 Storage. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 try: | 106 try: |
109 return _CreateStatInfo(self._bucket, path) | 107 return _CreateStatInfo(self._bucket, path) |
110 except errors.AuthorizationError: | 108 except errors.AuthorizationError: |
111 self._warnAboutAuthError() | 109 self._warnAboutAuthError() |
112 raise | 110 raise |
113 | 111 |
114 def GetIdentity(self): | 112 def GetIdentity(self): |
115 return '@'.join((self.__class__.__name__, StringIdentity(self._bucket))) | 113 return '@'.join((self.__class__.__name__, StringIdentity(self._bucket))) |
116 | 114 |
117 def __repr__(self): | 115 def __repr__(self): |
118 return 'LocalFileSystem(%s)' % self._bucket | 116 return 'CloudStorageFileSystem(%s)' % self._bucket |
119 | 117 |
120 def _warnAboutAuthError(self): | 118 def _warnAboutAuthError(self): |
121 logging.warn(('Authentication error on Cloud Storage. Check if your' | 119 logging.warn(('Authentication error on Cloud Storage. Check if your' |
122 ' appengine project has permissions to Read the GCS' | 120 ' appengine project has permissions to Read the GCS' |
123 ' buckets. If you are running a local appengine server,' | 121 ' buckets. If you are running a local appengine server,' |
124 ' you need to set an access_token in' | 122 ' you need to set an access_token in' |
125 ' local_debug/gcs_debug.conf.' | 123 ' local_debug/gcs_debug.conf.' |
126 ' Remember that this token expires in less than 10' | 124 ' Remember that this token expires in less than 10' |
127 ' minutes, so keep it updated. See' | 125 ' minutes, so keep it updated. See' |
128 ' gcs_file_system_provider.py for instructions.')); | 126 ' gcs_file_system_provider.py for instructions.')); |
129 logging.debug(traceback.format_exc()) | 127 logging.debug(traceback.format_exc()) |
OLD | NEW |