OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 import logging | 5 import logging |
6 import os | 6 import os |
7 from StringIO import StringIO | 7 from StringIO import StringIO |
8 import sys | 8 import sys |
9 | 9 |
10 from appengine_wrappers import webapp | 10 from appengine_wrappers import webapp |
11 from appengine_wrappers import memcache | 11 from appengine_wrappers import memcache |
12 from appengine_wrappers import urlfetch | 12 from appengine_wrappers import urlfetch |
13 | 13 |
14 from api_data_source import APIDataSource | 14 from api_data_source import APIDataSource |
15 from api_list_data_source import APIListDataSource | 15 from api_list_data_source import APIListDataSource |
16 from appengine_blobstore import AppEngineBlobstore | 16 from appengine_blobstore import AppEngineBlobstore |
17 from in_memory_object_store import InMemoryObjectStore | |
18 from appengine_url_fetcher import AppEngineUrlFetcher | 17 from appengine_url_fetcher import AppEngineUrlFetcher |
18 from availability_data_source import AvailabilityDataSource | |
19 from branch_utility import BranchUtility | 19 from branch_utility import BranchUtility |
20 from example_zipper import ExampleZipper | 20 from chrome_version_data_source import ChromeVersionDataSource |
21 from compiled_file_system import CompiledFileSystem | 21 from compiled_file_system import CompiledFileSystem |
22 import compiled_file_system as compiled_fs | 22 import compiled_file_system as compiled_fs |
23 from example_zipper import ExampleZipper | |
23 from github_file_system import GithubFileSystem | 24 from github_file_system import GithubFileSystem |
25 from in_memory_object_store import InMemoryObjectStore | |
24 from intro_data_source import IntroDataSource | 26 from intro_data_source import IntroDataSource |
25 from known_issues_data_source import KnownIssuesDataSource | 27 from known_issues_data_source import KnownIssuesDataSource |
26 from local_file_system import LocalFileSystem | 28 from local_file_system import LocalFileSystem |
27 from memcache_file_system import MemcacheFileSystem | 29 from memcache_file_system import MemcacheFileSystem |
28 from reference_resolver import ReferenceResolver | 30 from reference_resolver import ReferenceResolver |
29 from samples_data_source import SamplesDataSource | 31 from samples_data_source import SamplesDataSource |
30 from server_instance import ServerInstance | 32 from server_instance import ServerInstance |
31 from sidenav_data_source import SidenavDataSource | 33 from sidenav_data_source import SidenavDataSource |
32 from subversion_file_system import SubversionFileSystem | 34 from subversion_file_system import SubversionFileSystem |
33 from template_data_source import TemplateDataSource | 35 from template_data_source import TemplateDataSource |
34 from third_party.json_schema_compiler.model import UnixName | 36 from third_party.json_schema_compiler.model import UnixName |
35 import url_constants | 37 import url_constants |
36 | 38 |
37 # Increment this version to force the server to reload all pages in the first | 39 # Increment this version to force the server to reload all pages in the first |
38 # cron job that is run. | 40 # cron job that is run. |
39 _VERSION = 1 | 41 _VERSION = 2 |
40 | 42 |
41 # The default channel to serve docs for if no channel is specified. | 43 # The default channel to serve docs for if no channel is specified. |
42 _DEFAULT_CHANNEL = 'stable' | 44 _DEFAULT_CHANNEL = 'stable' |
43 | 45 |
44 BRANCH_UTILITY_MEMCACHE = InMemoryObjectStore('branch_utility') | 46 BRANCH_UTILITY_MEMCACHE = InMemoryObjectStore('branch_utility') |
45 BRANCH_UTILITY = BranchUtility(url_constants.OMAHA_PROXY_URL, | 47 BRANCH_UTILITY = BranchUtility(url_constants.OMAHA_PROXY_URL, |
46 AppEngineUrlFetcher(None), | 48 AppEngineUrlFetcher(None), |
47 BRANCH_UTILITY_MEMCACHE) | 49 BRANCH_UTILITY_MEMCACHE) |
48 | 50 |
51 AVAILABILITY_DATA_SOURCE_MEMCACHE = InMemoryObjectStore( | |
52 'availability_data_source') | |
53 | |
49 GITHUB_MEMCACHE = InMemoryObjectStore('github') | 54 GITHUB_MEMCACHE = InMemoryObjectStore('github') |
50 GITHUB_FILE_SYSTEM = GithubFileSystem( | 55 GITHUB_FILE_SYSTEM = GithubFileSystem( |
51 AppEngineUrlFetcher(url_constants.GITHUB_URL), | 56 AppEngineUrlFetcher(url_constants.GITHUB_URL), |
52 GITHUB_MEMCACHE, | 57 GITHUB_MEMCACHE, |
53 AppEngineBlobstore()) | 58 AppEngineBlobstore()) |
54 GITHUB_COMPILED_FILE_SYSTEM = CompiledFileSystem.Factory(GITHUB_FILE_SYSTEM, | 59 GITHUB_COMPILED_FILE_SYSTEM = CompiledFileSystem.Factory(GITHUB_FILE_SYSTEM, |
55 GITHUB_MEMCACHE) | 60 GITHUB_MEMCACHE) |
56 | 61 |
57 EXTENSIONS_PATH = 'chrome/common/extensions' | 62 EXTENSIONS_PATH = 'chrome/common/extensions' |
58 DOCS_PATH = 'docs' | 63 DOCS_PATH = 'docs' |
(...skipping 18 matching lines...) Expand all Loading... | |
77 return [UnixName(os.path.splitext(f.split('/')[-1])[0]) for f in files] | 82 return [UnixName(os.path.splitext(f.split('/')[-1])[0]) for f in files] |
78 | 83 |
79 def _CreateMemcacheFileSystem(branch, branch_memcache): | 84 def _CreateMemcacheFileSystem(branch, branch_memcache): |
80 svn_url = _GetURLFromBranch(branch) + '/' + EXTENSIONS_PATH | 85 svn_url = _GetURLFromBranch(branch) + '/' + EXTENSIONS_PATH |
81 stat_fetcher = AppEngineUrlFetcher( | 86 stat_fetcher = AppEngineUrlFetcher( |
82 svn_url.replace(url_constants.SVN_URL, url_constants.VIEWVC_URL)) | 87 svn_url.replace(url_constants.SVN_URL, url_constants.VIEWVC_URL)) |
83 fetcher = AppEngineUrlFetcher(svn_url) | 88 fetcher = AppEngineUrlFetcher(svn_url) |
84 return MemcacheFileSystem(SubversionFileSystem(fetcher, stat_fetcher), | 89 return MemcacheFileSystem(SubversionFileSystem(fetcher, stat_fetcher), |
85 branch_memcache) | 90 branch_memcache) |
86 | 91 |
87 _default_branch = BRANCH_UTILITY.GetBranchNumberForChannelName(_DEFAULT_CHANNEL) | 92 def _CreateObjectsForBranch(branch_number, channel_info=None): |
cduvall
2013/03/27 23:01:10
I think this would be more clear if you had two mo
epeterson
2013/03/28 00:53:51
Done.
| |
93 """Passed to the global instance of chrome_version_data_source, and also used | |
94 in _GetInstanceForBranch. Allows for the creation of api_data_sources linked | |
95 to |branch_number|, which is the maximum branch number from a given version of | |
96 chrome, and the creation of a branch instance. | |
97 """ | |
98 channel_name = _DEFAULT_CHANNEL | |
99 availability_data_source = None | |
100 instance = None | |
101 | |
102 if channel_info is not None: | |
103 channel_name = channel_info['name'] | |
104 availability_data_source = AvailabilityDataSource( | |
105 CHROME_VERSION_DATA_SOURCE, | |
106 channel_info['version'], | |
107 AVAILABILITY_DATA_SOURCE_MEMCACHE) | |
108 | |
109 branch_memcache = InMemoryObjectStore(branch_number) | |
110 file_system = _CreateMemcacheFileSystem(branch_number, branch_memcache) | |
111 cache_factory = CompiledFileSystem.Factory(file_system, branch_memcache) | |
112 api_list_data_source_factory = APIListDataSource.Factory( | |
113 cache_factory, | |
114 file_system, | |
115 API_PATH, | |
116 PUBLIC_TEMPLATE_PATH) | |
117 api_data_source_factory = APIDataSource.Factory(cache_factory, | |
118 API_PATH, | |
119 availability_data_source) | |
120 # Give the ReferenceResolver a memcache, to speed up the lookup of | |
121 # duplicate $refs. | |
122 ref_resolver_factory = ReferenceResolver.Factory( | |
123 api_data_source_factory, | |
124 api_list_data_source_factory, | |
125 branch_memcache) | |
126 api_data_source_factory.SetReferenceResolverFactory(ref_resolver_factory) | |
127 samples_data_source_factory = SamplesDataSource.Factory( | |
128 channel_name, | |
129 file_system, | |
130 GITHUB_FILE_SYSTEM, | |
131 cache_factory, | |
132 GITHUB_COMPILED_FILE_SYSTEM, | |
133 ref_resolver_factory, | |
134 EXAMPLES_PATH) | |
135 api_data_source_factory.SetSamplesDataSourceFactory( | |
136 samples_data_source_factory) | |
137 | |
138 if channel_info is not None: | |
139 intro_data_source_factory = IntroDataSource.Factory( | |
140 cache_factory, | |
141 ref_resolver_factory, | |
142 [INTRO_PATH, ARTICLE_PATH]) | |
143 sidenav_data_source_factory = SidenavDataSource.Factory(cache_factory, | |
144 JSON_PATH) | |
145 template_data_source_factory = TemplateDataSource.Factory( | |
146 channel_name, | |
147 api_data_source_factory, | |
148 api_list_data_source_factory, | |
149 intro_data_source_factory, | |
150 samples_data_source_factory, | |
151 KNOWN_ISSUES_DATA_SOURCE, | |
152 sidenav_data_source_factory, | |
153 cache_factory, | |
154 ref_resolver_factory, | |
155 PUBLIC_TEMPLATE_PATH, | |
156 PRIVATE_TEMPLATE_PATH, | |
157 BRANCH_UTILITY.GetChannelInfoForAllChannels()) | |
158 example_zipper = ExampleZipper(file_system, | |
159 cache_factory, | |
160 DOCS_PATH) | |
161 instance = ServerInstance(template_data_source_factory, | |
162 example_zipper, | |
163 cache_factory) | |
164 return instance | |
165 return api_data_source_factory.Create(None) | |
166 | |
167 _default_branch = BRANCH_UTILITY.GetChannelInfoForChannelName( | |
168 _DEFAULT_CHANNEL)['branch'] | |
88 APPS_MEMCACHE = InMemoryObjectStore(_default_branch) | 169 APPS_MEMCACHE = InMemoryObjectStore(_default_branch) |
89 APPS_FILE_SYSTEM = _CreateMemcacheFileSystem(_default_branch, APPS_MEMCACHE) | 170 APPS_FILE_SYSTEM = _CreateMemcacheFileSystem(_default_branch, APPS_MEMCACHE) |
90 APPS_COMPILED_FILE_SYSTEM = CompiledFileSystem.Factory( | 171 APPS_COMPILED_FILE_SYSTEM = CompiledFileSystem.Factory( |
91 APPS_FILE_SYSTEM, | 172 APPS_FILE_SYSTEM, |
92 APPS_MEMCACHE).Create(_SplitFilenameUnix, compiled_fs.APPS_FS) | 173 APPS_MEMCACHE).Create(_SplitFilenameUnix, compiled_fs.APPS_FS) |
93 | 174 |
94 EXTENSIONS_MEMCACHE = InMemoryObjectStore(_default_branch) | 175 EXTENSIONS_MEMCACHE = InMemoryObjectStore(_default_branch) |
95 EXTENSIONS_FILE_SYSTEM = _CreateMemcacheFileSystem(_default_branch, | 176 EXTENSIONS_FILE_SYSTEM = _CreateMemcacheFileSystem(_default_branch, |
96 EXTENSIONS_MEMCACHE) | 177 EXTENSIONS_MEMCACHE) |
97 EXTENSIONS_COMPILED_FILE_SYSTEM = CompiledFileSystem.Factory( | 178 EXTENSIONS_COMPILED_FILE_SYSTEM = CompiledFileSystem.Factory( |
98 EXTENSIONS_FILE_SYSTEM, | 179 EXTENSIONS_FILE_SYSTEM, |
99 EXTENSIONS_MEMCACHE).Create(_SplitFilenameUnix, compiled_fs.EXTENSIONS_FS) | 180 EXTENSIONS_MEMCACHE).Create(_SplitFilenameUnix, compiled_fs.EXTENSIONS_FS) |
100 | 181 |
101 KNOWN_ISSUES_DATA_SOURCE = KnownIssuesDataSource( | 182 KNOWN_ISSUES_DATA_SOURCE = KnownIssuesDataSource( |
102 InMemoryObjectStore('KnownIssues'), | 183 InMemoryObjectStore('KnownIssues'), |
103 AppEngineUrlFetcher(None)) | 184 AppEngineUrlFetcher(None)) |
104 | 185 |
186 CHROME_VERSION_DATA_SOURCE_MEMCACHE = InMemoryObjectStore( | |
187 'chrome_version_data_source') | |
188 CHROME_VERSION_DATA_SOURCE = ChromeVersionDataSource( | |
189 url_constants.OMAHA_BETA_HISTORY, | |
190 AppEngineUrlFetcher(None), | |
191 CHROME_VERSION_DATA_SOURCE_MEMCACHE, | |
192 _CreateObjectsForBranch) | |
193 | |
105 def _MakeInstanceKey(branch, number): | 194 def _MakeInstanceKey(branch, number): |
106 return '%s/%s' % (branch, number) | 195 return '%s/%s' % (branch, number) |
107 | 196 |
108 def _GetInstanceForBranch(channel_name, local_path): | 197 def _GetInstanceForBranch(channel_name, local_path): |
109 branch = BRANCH_UTILITY.GetBranchNumberForChannelName(channel_name) | 198 channel_info = BRANCH_UTILITY.GetChannelInfoForChannelName( |
199 channel_name) | |
200 branch = channel_info['branch'] | |
110 | 201 |
111 # The key for the server is a tuple of |channel_name| with |branch|, since | 202 # The key for the server is a tuple of |channel_name| with |branch|, since |
112 # sometimes stable and beta point to the same branch. | 203 # sometimes stable and beta point to the same branch. |
113 instance_key = _MakeInstanceKey(channel_name, branch) | 204 instance_key = _MakeInstanceKey(channel_name, branch) |
114 instance = SERVER_INSTANCES.get(instance_key, None) | 205 instance = SERVER_INSTANCES.get(instance_key, None) |
115 if instance is not None: | 206 if instance is not None: |
116 return instance | 207 return instance |
117 | 208 |
118 branch_memcache = InMemoryObjectStore(branch) | 209 instance = _CreateObjectsForBranch(branch, channel_info) |
119 file_system = _CreateMemcacheFileSystem(branch, branch_memcache) | 210 SERVER_INSTANCES[instance_key] = instance |
120 cache_factory = CompiledFileSystem.Factory(file_system, branch_memcache) | |
121 api_list_data_source_factory = APIListDataSource.Factory(cache_factory, | |
122 file_system, | |
123 API_PATH, | |
124 PUBLIC_TEMPLATE_PATH) | |
125 api_data_source_factory = APIDataSource.Factory( | |
126 cache_factory, | |
127 API_PATH) | |
128 | 211 |
129 # Give the ReferenceResolver a memcache, to speed up the lookup of | |
130 # duplicate $refs. | |
131 ref_resolver_factory = ReferenceResolver.Factory( | |
132 api_data_source_factory, | |
133 api_list_data_source_factory, | |
134 branch_memcache) | |
135 api_data_source_factory.SetReferenceResolverFactory(ref_resolver_factory) | |
136 samples_data_source_factory = SamplesDataSource.Factory( | |
137 channel_name, | |
138 file_system, | |
139 GITHUB_FILE_SYSTEM, | |
140 cache_factory, | |
141 GITHUB_COMPILED_FILE_SYSTEM, | |
142 ref_resolver_factory, | |
143 EXAMPLES_PATH) | |
144 api_data_source_factory.SetSamplesDataSourceFactory( | |
145 samples_data_source_factory) | |
146 intro_data_source_factory = IntroDataSource.Factory( | |
147 cache_factory, | |
148 ref_resolver_factory, | |
149 [INTRO_PATH, ARTICLE_PATH]) | |
150 sidenav_data_source_factory = SidenavDataSource.Factory(cache_factory, | |
151 JSON_PATH) | |
152 template_data_source_factory = TemplateDataSource.Factory( | |
153 channel_name, | |
154 api_data_source_factory, | |
155 api_list_data_source_factory, | |
156 intro_data_source_factory, | |
157 samples_data_source_factory, | |
158 KNOWN_ISSUES_DATA_SOURCE, | |
159 sidenav_data_source_factory, | |
160 cache_factory, | |
161 ref_resolver_factory, | |
162 PUBLIC_TEMPLATE_PATH, | |
163 PRIVATE_TEMPLATE_PATH) | |
164 example_zipper = ExampleZipper(file_system, | |
165 cache_factory, | |
166 DOCS_PATH) | |
167 | |
168 instance = ServerInstance(template_data_source_factory, | |
169 example_zipper, | |
170 cache_factory) | |
171 SERVER_INSTANCES[instance_key] = instance | |
172 return instance | 212 return instance |
173 | 213 |
174 def _CleanBranches(): | 214 def _CleanBranches(): |
175 keys = [_MakeInstanceKey(branch, number) | 215 keys = [_MakeInstanceKey(branch, number) |
176 for branch, number in BRANCH_UTILITY.GetAllBranchNumbers()] | 216 for branch, number in BRANCH_UTILITY.GetAllBranchNumbers()] |
177 for key in SERVER_INSTANCES.keys(): | 217 for key in SERVER_INSTANCES.keys(): |
178 if key not in keys: | 218 if key not in keys: |
179 SERVER_INSTANCES.pop(key) | 219 SERVER_INSTANCES.pop(key) |
180 | 220 |
181 class _MockResponse(object): | 221 class _MockResponse(object): |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 | 259 |
220 _CleanBranches() | 260 _CleanBranches() |
221 | 261 |
222 # Yes, do this after it's passed to RedirectBadPaths. That needs to know | 262 # Yes, do this after it's passed to RedirectBadPaths. That needs to know |
223 # whether or not a branch was specified. | 263 # whether or not a branch was specified. |
224 if channel_name is None: | 264 if channel_name is None: |
225 channel_name = _DEFAULT_CHANNEL | 265 channel_name = _DEFAULT_CHANNEL |
226 _GetInstanceForBranch(channel_name, self._local_path).Get(real_path, | 266 _GetInstanceForBranch(channel_name, self._local_path).Get(real_path, |
227 self.request, | 267 self.request, |
228 self.response) | 268 self.response) |
229 | |
230 def _Render(self, files, channel): | 269 def _Render(self, files, channel): |
231 original_response = self.response | 270 original_response = self.response |
232 for f in files: | 271 for f in files: |
233 if f.endswith('404.html'): | 272 if f.endswith('404.html'): |
234 continue | 273 continue |
235 path = channel + f.split(PUBLIC_TEMPLATE_PATH)[-1] | 274 path = channel + f.split(PUBLIC_TEMPLATE_PATH)[-1] |
236 self.request = _MockRequest(path) | 275 self.request = _MockRequest(path) |
237 self.response = _MockResponse() | 276 self.response = _MockResponse() |
238 try: | 277 try: |
239 self._HandleGet(path) | 278 self._HandleGet(path) |
(...skipping 19 matching lines...) Expand all Loading... | |
259 # We could list all files in PUBLIC_TEMPLATE_PATH then render them. However, | 298 # We could list all files in PUBLIC_TEMPLATE_PATH then render them. However, |
260 # this would be inefficient in the common case where files haven't changed | 299 # this would be inefficient in the common case where files haven't changed |
261 # since the last cron. | 300 # since the last cron. |
262 # | 301 # |
263 # Instead, let the CompiledFileSystem give us clues when to re-render: we | 302 # Instead, let the CompiledFileSystem give us clues when to re-render: we |
264 # use the CFS to check whether the templates, examples, or API folders have | 303 # use the CFS to check whether the templates, examples, or API folders have |
265 # been changed. If there has been a change, the compilation function will | 304 # been changed. If there has been a change, the compilation function will |
266 # be called. The same is then done separately with the apps samples page, | 305 # be called. The same is then done separately with the apps samples page, |
267 # since it pulls its data from Github. | 306 # since it pulls its data from Github. |
268 channel = path.split('/')[-1] | 307 channel = path.split('/')[-1] |
269 branch = BRANCH_UTILITY.GetBranchNumberForChannelName(channel) | 308 branch = BRANCH_UTILITY.GetChannelInfoForChannelName(channel)['branch'] |
270 logging.info('Running cron job for %s.' % branch) | 309 logging.info('Running cron job for %s.' % branch) |
271 branch_memcache = InMemoryObjectStore(branch) | 310 branch_memcache = InMemoryObjectStore(branch) |
272 file_system = _CreateMemcacheFileSystem(branch, branch_memcache) | 311 file_system = _CreateMemcacheFileSystem(branch, branch_memcache) |
273 factory = CompiledFileSystem.Factory(file_system, branch_memcache) | 312 factory = CompiledFileSystem.Factory(file_system, branch_memcache) |
274 | 313 |
275 needs_render = self._ValueHolder(False) | 314 needs_render = self._ValueHolder(False) |
276 invalidation_cache = factory.Create(lambda _, __: needs_render.Set(True), | 315 invalidation_cache = factory.Create(lambda _, __: needs_render.Set(True), |
277 compiled_fs.CRON_INVALIDATION, | 316 compiled_fs.CRON_INVALIDATION, |
278 version=_VERSION) | 317 version=_VERSION) |
279 for path in [TEMPLATE_PATH, EXAMPLES_PATH, API_PATH]: | 318 for path in [TEMPLATE_PATH, EXAMPLES_PATH, API_PATH]: |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 | 406 |
368 # Redirect paths like "directory" to "directory/". This is so relative | 407 # Redirect paths like "directory" to "directory/". This is so relative |
369 # file paths will know to treat this as a directory. | 408 # file paths will know to treat this as a directory. |
370 if os.path.splitext(path)[1] == '' and path[-1] != '/': | 409 if os.path.splitext(path)[1] == '' and path[-1] != '/': |
371 self.redirect(path + '/') | 410 self.redirect(path + '/') |
372 return | 411 return |
373 | 412 |
374 path = path.strip('/') | 413 path = path.strip('/') |
375 if not self._RedirectFromCodeDotGoogleDotCom(path): | 414 if not self._RedirectFromCodeDotGoogleDotCom(path): |
376 self._HandleGet(path) | 415 self._HandleGet(path) |
OLD | NEW |