Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: chrome/common/extensions/docs/server2/gcs_file_system.py

Issue 139303023: add GCS support to docs server (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing reviewer's comments Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
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 import traceback
15
16 '''See gcs_file_system_provider.py for documentation on using Google Cloud
17 Storage as a filesystem.
18 '''
19 def _ReadFile(filename):
20 try:
21 with cloudstorage_api.open(filename, 'r') as f:
22 return f.read()
23 except errors.Error as e:
24 raise FileNotFoundError('Read failed for %s: %s' % (filename,
25 traceback.format_exc()))
26
27 def _ListDir(dir_name):
28 try:
29 files = cloudstorage_api.listbucket(dir_name)
30 return [os_path.filename for os_path in files]
31 except errors.Error as e:
32 raise FileNotFoundError('cloudstorage.listbucket failed for %s: %s' %
33 (dir_name, e))
34 for os_path in files:
Jeffrey Yasskin 2014/02/03 22:12:56 I think you can remove these 3 lines now.
Renato Mangini (chromium) 2014/02/04 02:07:08 ugh, I thought I had removed them. Bad connection
35 all_files.append(os_path.filename)
36 return all_files
37
38 def _CreateStatInfo(bucket, path):
39 bucket = '/%s' % bucket
40 full_path = '/'.join( (bucket, path.lstrip('/')) )
41 try:
42 if full_path.endswith('/'):
43 child_versions = dict()
44 version = 0
45 # Fetching stats for all files under full_path, recursively. The
46 # listbucket method uses a prefix approach to simulate hierarchy,
47 # but calling it without the "delimiter" argument searches for prefix,
48 # which means, for directories, everything beneath it.
49 for _file in cloudstorage_api.listbucket(full_path):
50 if not _file.is_dir:
51 # GCS doesn't have metadata for dirs
52 child_stat = cloudstorage_api.stat('%s' % _file.filename).st_ctime
53 filename = _file.filename[len(bucket)+1:]
54 child_versions[filename] = child_stat
55 version = max(version, child_stat)
56 else:
57 child_versions = None
58 version = cloudstorage_api.stat(full_path).st_ctime
59 return StatInfo(version, child_versions)
60 except (TypeError, errors.Error) as e:
61 raise FileNotFoundError('cloudstorage.stat failed for %s: %s' % (path, e))
62
63
64 class CloudStorageFileSystem(FileSystem):
65 '''FileSystem implementation which fetches resources from Google Cloud Storage
66 '''
67 def __init__(self, bucket, debug_access_token=None, debug_bucket_prefix=None):
68 self._bucket = bucket
69 if debug_access_token:
70 logging.debug('gcs: using debug access token: %s' % debug_access_token)
71 common.set_access_token(debug_access_token)
72 if debug_bucket_prefix:
73 logging.debug('gcs: prefixing all bucket names with %s' %
74 debug_bucket_prefix)
75 self._bucket = debug_bucket_prefix + self._bucket
76
77 def Read(self, paths):
78 def resolve():
79 try:
80 result = {}
81 for path in paths:
82 full_path = '/%s/%s' % (self._bucket, path.lstrip('/'))
83 logging.debug('gcs: requested path %s, reading %s' %
84 (path, full_path))
85 if path == '' or path.endswith('/'):
86 result[path] = _ListDir(full_path)
87 else:
88 result[path] = _ReadFile(full_path)
89 return result
90 except errors.AuthorizationError as authError:
91 self._warnAboutAuthError()
92 raise
93
94 return Future(delegate=Gettable(resolve))
95
96 def Refresh(self):
97 return Future(value=())
98
99 def Stat(self, path):
100 try:
101 return _CreateStatInfo(self._bucket, path)
102 except errors.AuthorizationError as authError:
103 self._warnAboutAuthError()
104 raise authError
105
106 def GetIdentity(self):
107 return '@'.join((self.__class__.__name__, StringIdentity(self._bucket)))
108
109 def __repr__(self):
110 return 'LocalFileSystem(%s)' % self._bucket
111
112 def _warnAboutAuthError(self):
113 logging.warn(('Authentication error on Cloud Storage. Check if your'
114 ' appengine project has permissions to Read the GCS'
115 ' buckets. If you are running a local appengine server,'
116 ' you need to set an access_token in'
117 ' local_debug/gcs_debug.conf.'
118 ' Remember that this token expires in less than 10'
119 ' minutes, so keep it updated. See'
120 ' gcs_file_system_provider.py for instructions.'));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698