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 from fnmatch import fnmatch | 5 from fnmatch import fnmatch |
6 import mimetypes | 6 import mimetypes |
7 import os | 7 import os |
8 | 8 |
9 from api_data_source import APIDataSource | 9 from api_data_source import APIDataSource |
10 from api_list_data_source import APIListDataSource | 10 from api_list_data_source import APIListDataSource |
11 from appengine_blobstore import AppEngineBlobstore | 11 from appengine_blobstore import AppEngineBlobstore |
12 from appengine_url_fetcher import AppEngineUrlFetcher | 12 from appengine_url_fetcher import AppEngineUrlFetcher |
13 from branch_utility import BranchUtility | 13 from branch_utility import BranchUtility |
| 14 from caching_file_system import CachingFileSystem |
14 from compiled_file_system import CompiledFileSystem | 15 from compiled_file_system import CompiledFileSystem |
15 from example_zipper import ExampleZipper | 16 from example_zipper import ExampleZipper |
16 from file_system import FileNotFoundError | 17 from file_system import FileNotFoundError |
17 from github_file_system import GithubFileSystem | 18 from github_file_system import GithubFileSystem |
18 from in_memory_object_store import InMemoryObjectStore | |
19 from intro_data_source import IntroDataSource | 19 from intro_data_source import IntroDataSource |
20 from local_file_system import LocalFileSystem | 20 from local_file_system import LocalFileSystem |
21 from caching_file_system import CachingFileSystem | |
22 from object_store_creator import ObjectStoreCreator | 21 from object_store_creator import ObjectStoreCreator |
| 22 from offline_file_system import OfflineFileSystem |
23 from path_canonicalizer import PathCanonicalizer | 23 from path_canonicalizer import PathCanonicalizer |
24 from reference_resolver import ReferenceResolver | 24 from reference_resolver import ReferenceResolver |
25 from samples_data_source import SamplesDataSource | 25 from samples_data_source import SamplesDataSource |
26 from sidenav_data_source import SidenavDataSource | 26 from sidenav_data_source import SidenavDataSource |
27 from subversion_file_system import SubversionFileSystem | 27 from subversion_file_system import SubversionFileSystem |
28 import svn_constants | 28 import svn_constants |
29 from template_data_source import TemplateDataSource | 29 from template_data_source import TemplateDataSource |
| 30 from third_party.json_schema_compiler.memoize import memoize |
30 from third_party.json_schema_compiler.model import UnixName | 31 from third_party.json_schema_compiler.model import UnixName |
31 import url_constants | 32 import url_constants |
32 | 33 |
33 def _IsBinaryMimetype(mimetype): | 34 def _IsBinaryMimetype(mimetype): |
34 return any(mimetype.startswith(prefix) | 35 return any(mimetype.startswith(prefix) |
35 for prefix in ['audio', 'image', 'video']) | 36 for prefix in ['audio', 'image', 'video']) |
36 | 37 |
37 class ServerInstance(object): | 38 class ServerInstance(object): |
38 '''Per-instance per-branch state. | 39 # Lazily create so we don't create github file systems unnecessarily in |
39 ''' | 40 # tests. |
40 _instances = {} | |
41 | |
42 branch_utility = None | 41 branch_utility = None |
43 github_file_system = None | 42 github_file_system = None |
44 | 43 |
45 @staticmethod | 44 @staticmethod |
46 def GetOrCreate(channel): | 45 @memoize |
47 # Lazily create so that we don't do unnecessary work in tests. | 46 def GetOrCreateOffline(channel): |
48 if ServerInstance.branch_utility is None: | 47 '''Gets/creates a local ServerInstance, meaning that only resources local to |
49 ServerInstance.branch_utility = BranchUtility( | 48 the server - memcache, object store, etc, are queried. This amounts to not |
50 url_constants.OMAHA_PROXY_URL, AppEngineUrlFetcher()) | 49 setting up the subversion nor github file systems. |
51 branch = ServerInstance.branch_utility.GetBranchNumberForChannelName( | 50 ''' |
52 channel) | 51 branch_utility = ServerInstance._GetOrCreateBranchUtility() |
53 | 52 branch = branch_utility.GetBranchNumberForChannelName(channel) |
54 # Use the branch as the key to |_instances| since the branch data is | 53 object_store_creator_factory = ObjectStoreCreator.Factory(branch) |
55 # predictable while the channel data (channels can swich branches) isn't. | 54 # No svn nor github file systems. Rely on the crons to fill the caches, and |
56 instance = ServerInstance._instances.get(branch) | 55 # for the caches to exist. |
57 if instance is None: | 56 return ServerInstance( |
58 instance = ServerInstance._CreateForProduction(channel, branch) | 57 channel, |
59 ServerInstance._instances[branch] = instance | 58 object_store_creator_factory, |
60 return instance | 59 CachingFileSystem(OfflineFileSystem(SubversionFileSystem), |
| 60 object_store_creator_factory), |
| 61 # TODO(kalman): convert GithubFileSystem to be wrappable in a |
| 62 # CachingFileSystem so that it can be replaced with an |
| 63 # OfflineFileSystem. Currently GFS doesn't set the child versions of |
| 64 # stat requests so it doesn't. |
| 65 ServerInstance._GetOrCreateGithubFileSystem()) |
61 | 66 |
62 @staticmethod | 67 @staticmethod |
63 def _CreateForProduction(channel, branch): | 68 @memoize |
| 69 def GetOrCreateOnline(channel): |
| 70 '''Creates/creates an online server instance, meaning that both local and |
| 71 subversion/github resources are queried. |
| 72 ''' |
| 73 branch_utility = ServerInstance._GetOrCreateBranchUtility() |
| 74 branch = branch_utility.GetBranchNumberForChannelName(channel) |
| 75 |
64 if branch == 'trunk': | 76 if branch == 'trunk': |
65 svn_url = '/'.join((url_constants.SVN_TRUNK_URL, | 77 svn_url = '/'.join((url_constants.SVN_TRUNK_URL, |
66 'src', | 78 'src', |
67 svn_constants.EXTENSIONS_PATH)) | 79 svn_constants.EXTENSIONS_PATH)) |
68 else: | 80 else: |
69 svn_url = '/'.join((url_constants.SVN_BRANCH_URL, | 81 svn_url = '/'.join((url_constants.SVN_BRANCH_URL, |
70 branch, | 82 branch, |
71 'src', | 83 'src', |
72 svn_constants.EXTENSIONS_PATH)) | 84 svn_constants.EXTENSIONS_PATH)) |
73 | 85 |
74 viewvc_url = svn_url.replace(url_constants.SVN_URL, | 86 viewvc_url = svn_url.replace(url_constants.SVN_URL, |
75 url_constants.VIEWVC_URL) | 87 url_constants.VIEWVC_URL) |
76 | 88 |
77 object_store_creator_factory = ObjectStoreCreator.Factory(branch) | 89 object_store_creator_factory = ObjectStoreCreator.Factory(branch) |
78 | 90 |
79 svn_file_system = CachingFileSystem( | 91 svn_file_system = CachingFileSystem( |
80 SubversionFileSystem(AppEngineUrlFetcher(svn_url), | 92 SubversionFileSystem(AppEngineUrlFetcher(svn_url), |
81 AppEngineUrlFetcher(viewvc_url)), | 93 AppEngineUrlFetcher(viewvc_url)), |
82 object_store_creator_factory) | 94 object_store_creator_factory) |
83 | 95 |
84 # Lazily create so we don't create github file systems unnecessarily in | |
85 # tests. | |
86 if ServerInstance.github_file_system is None: | |
87 ServerInstance.github_file_system = GithubFileSystem( | |
88 AppEngineUrlFetcher(url_constants.GITHUB_URL), | |
89 AppEngineBlobstore()) | |
90 | |
91 return ServerInstance(channel, | 96 return ServerInstance(channel, |
92 object_store_creator_factory, | 97 object_store_creator_factory, |
93 svn_file_system, | 98 svn_file_system, |
94 ServerInstance.github_file_system) | 99 ServerInstance._GetOrCreateGithubFileSystem()) |
95 | 100 |
96 @staticmethod | 101 @staticmethod |
97 def CreateForTest(file_system): | 102 def CreateForTest(file_system): |
98 return ServerInstance('test', | 103 return ServerInstance('test', |
99 ObjectStoreCreator.Factory('test'), | 104 ObjectStoreCreator.Factory('test'), |
100 file_system, | 105 file_system, |
101 None) | 106 None) |
102 | 107 |
| 108 @staticmethod |
| 109 def _GetOrCreateBranchUtility(): |
| 110 if ServerInstance.branch_utility is None: |
| 111 ServerInstance.branch_utility = BranchUtility( |
| 112 url_constants.OMAHA_PROXY_URL, |
| 113 AppEngineUrlFetcher()) |
| 114 return ServerInstance.branch_utility |
| 115 |
| 116 @staticmethod |
| 117 def _GetOrCreateGithubFileSystem(): |
| 118 if ServerInstance.github_file_system is None: |
| 119 ServerInstance.github_file_system = GithubFileSystem( |
| 120 AppEngineUrlFetcher(url_constants.GITHUB_URL), |
| 121 AppEngineBlobstore()) |
| 122 return ServerInstance.github_file_system |
| 123 |
103 def __init__(self, | 124 def __init__(self, |
104 channel, | 125 channel, |
105 object_store_creator_factory, | 126 object_store_creator_factory, |
106 svn_file_system, | 127 svn_file_system, |
107 github_file_system): | 128 github_file_system): |
108 self.svn_file_system = svn_file_system | 129 self.svn_file_system = svn_file_system |
109 | 130 |
110 self.github_file_system = github_file_system | 131 self.github_file_system = github_file_system |
111 | 132 |
112 self.compiled_fs_factory = CompiledFileSystem.Factory( | 133 self.compiled_fs_factory = CompiledFileSystem.Factory( |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 elif path.endswith('.html'): | 237 elif path.endswith('.html'): |
217 content = templates.Render(path) | 238 content = templates.Render(path) |
218 | 239 |
219 response.headers['x-frame-options'] = 'sameorigin' | 240 response.headers['x-frame-options'] = 'sameorigin' |
220 if content: | 241 if content: |
221 response.headers['cache-control'] = 'max-age=300' | 242 response.headers['cache-control'] = 'max-age=300' |
222 response.out.write(content) | 243 response.out.write(content) |
223 else: | 244 else: |
224 response.set_status(404); | 245 response.set_status(404); |
225 response.out.write(templates.Render('404')) | 246 response.out.write(templates.Render('404')) |
OLD | NEW |