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 import sys | 7 import sys |
8 | 8 |
9 from appengine_wrappers import webapp | 9 from appengine_wrappers import webapp |
10 from appengine_wrappers import memcache | 10 from appengine_wrappers import memcache |
(...skipping 30 matching lines...) Expand all Loading... | |
41 DEFAULT_BRANCH, | 41 DEFAULT_BRANCH, |
42 AppEngineUrlFetcher(''), | 42 AppEngineUrlFetcher(''), |
43 BRANCH_UTILITY_MEMCACHE) | 43 BRANCH_UTILITY_MEMCACHE) |
44 | 44 |
45 STATIC_DIR_PREFIX = 'docs/server2' | 45 STATIC_DIR_PREFIX = 'docs/server2' |
46 EXTENSIONS_PATH = 'chrome/common/extensions' | 46 EXTENSIONS_PATH = 'chrome/common/extensions' |
47 DOCS_PATH = 'docs' | 47 DOCS_PATH = 'docs' |
48 API_PATH = 'api' | 48 API_PATH = 'api' |
49 INTRO_PATH = DOCS_PATH + '/server2/templates/intros' | 49 INTRO_PATH = DOCS_PATH + '/server2/templates/intros' |
50 ARTICLE_PATH = DOCS_PATH + '/server2/templates/articles' | 50 ARTICLE_PATH = DOCS_PATH + '/server2/templates/articles' |
51 PUBLIC_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/public' | 51 APPS_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/apps' |
52 EXTENSIONS_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/extensions' | |
not at google - send to devlin
2012/08/03 20:05:29
why do we need to split it like this, can't they b
cduvall
2012/08/03 22:38:00
Done.
| |
52 PRIVATE_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/private' | 53 PRIVATE_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/private' |
53 EXAMPLES_PATH = DOCS_PATH + '/examples' | 54 EXAMPLES_PATH = DOCS_PATH + '/examples' |
54 FULL_EXAMPLES_PATH = DOCS_PATH + '/' + EXAMPLES_PATH | 55 FULL_EXAMPLES_PATH = DOCS_PATH + '/' + EXAMPLES_PATH |
55 | 56 |
56 # Global cache of instances because Handler is recreated for every request. | 57 # Global cache of instances because Handler is recreated for every request. |
57 SERVER_INSTANCES = {} | 58 SERVER_INSTANCES = { 'extensions': {}, 'apps': {} } |
58 | 59 |
59 def _GetInstanceForBranch(branch, local_path): | 60 def _GetInstanceForBranch(branch, local_path, doc_type): |
60 if branch in SERVER_INSTANCES: | 61 if branch in SERVER_INSTANCES[doc_type]: |
61 return SERVER_INSTANCES[branch] | 62 return SERVER_INSTANCES[doc_type][branch] |
62 if branch == 'local': | 63 if branch == 'local': |
63 file_system = LocalFileSystem(local_path) | 64 file_system = LocalFileSystem(local_path) |
64 else: | 65 else: |
65 fetcher = AppEngineUrlFetcher( | 66 fetcher = AppEngineUrlFetcher( |
66 _GetURLFromBranch(branch) + '/' + EXTENSIONS_PATH) | 67 _GetURLFromBranch(branch) + '/' + EXTENSIONS_PATH) |
67 file_system = MemcacheFileSystem(SubversionFileSystem(fetcher), | 68 file_system = MemcacheFileSystem(SubversionFileSystem(fetcher), |
68 AppEngineMemcache(branch)) | 69 AppEngineMemcache(branch)) |
69 | 70 |
71 if doc_type == 'apps': | |
72 public_path = APPS_TEMPLATE_PATH | |
73 else: | |
74 public_path = EXTENSIONS_TEMPLATE_PATH | |
70 cache_builder = FileSystemCache.Builder(file_system) | 75 cache_builder = FileSystemCache.Builder(file_system) |
71 api_list_data_source = APIListDataSource(cache_builder, | 76 api_list_data_source = APIListDataSource(cache_builder, |
72 file_system, | 77 file_system, |
73 API_PATH, | 78 API_PATH, |
74 PUBLIC_TEMPLATE_PATH) | 79 public_path) |
75 intro_data_source = IntroDataSource(cache_builder, | 80 intro_data_source = IntroDataSource(cache_builder, |
76 [INTRO_PATH, ARTICLE_PATH]) | 81 [INTRO_PATH, ARTICLE_PATH]) |
77 samples_data_source_factory = SamplesDataSource.Factory(branch, | 82 samples_data_source_factory = SamplesDataSource.Factory(branch, |
78 file_system, | 83 file_system, |
79 cache_builder, | 84 cache_builder, |
80 EXAMPLES_PATH) | 85 EXAMPLES_PATH) |
81 api_data_source_factory = APIDataSource.Factory(cache_builder, | 86 api_data_source_factory = APIDataSource.Factory(cache_builder, |
82 API_PATH, | 87 API_PATH, |
83 samples_data_source_factory) | 88 samples_data_source_factory) |
84 template_data_source_factory = TemplateDataSource.Factory( | 89 template_data_source_factory = TemplateDataSource.Factory( |
85 branch, | 90 branch, |
91 doc_type, | |
86 api_data_source_factory, | 92 api_data_source_factory, |
87 api_list_data_source, | 93 api_list_data_source, |
88 intro_data_source, | 94 intro_data_source, |
89 samples_data_source_factory, | 95 samples_data_source_factory, |
90 cache_builder, | 96 cache_builder, |
91 PUBLIC_TEMPLATE_PATH, | 97 public_path, |
92 PRIVATE_TEMPLATE_PATH) | 98 PRIVATE_TEMPLATE_PATH) |
93 example_zipper = ExampleZipper(file_system, | 99 example_zipper = ExampleZipper(file_system, |
94 cache_builder, | 100 cache_builder, |
95 DOCS_PATH, | 101 DOCS_PATH, |
96 EXAMPLES_PATH) | 102 EXAMPLES_PATH) |
97 SERVER_INSTANCES[branch] = ServerInstance( | 103 SERVER_INSTANCES[doc_type][branch] = ServerInstance( |
98 template_data_source_factory, | 104 template_data_source_factory, |
99 example_zipper, | 105 example_zipper, |
100 cache_builder) | 106 cache_builder) |
101 return SERVER_INSTANCES[branch] | 107 return SERVER_INSTANCES[doc_type][branch] |
102 | 108 |
103 def _GetURLFromBranch(branch): | 109 def _GetURLFromBranch(branch): |
104 if branch == 'trunk': | 110 if branch == 'trunk': |
105 return TRUNK_URL + '/src' | 111 return TRUNK_URL + '/src' |
106 return BRANCH_URL + '/' + branch + '/src' | 112 return BRANCH_URL + '/' + branch + '/src' |
107 | 113 |
108 class Handler(webapp.RequestHandler): | 114 class Handler(webapp.RequestHandler): |
109 def __init__(self, request, response, local_path=EXTENSIONS_PATH): | 115 def __init__(self, request, response, local_path=EXTENSIONS_PATH): |
110 self._local_path = local_path | 116 self._local_path = local_path |
111 super(Handler, self).__init__(request, response) | 117 super(Handler, self).__init__(request, response) |
112 | 118 |
113 def _NavigateToPath(self, path): | 119 def _NavigateToPath(self, path, doc_type): |
114 channel_name, real_path = BRANCH_UTILITY.SplitChannelNameFromPath(path) | 120 channel_name, real_path = BRANCH_UTILITY.SplitChannelNameFromPath(path) |
115 branch = BRANCH_UTILITY.GetBranchNumberForChannelName(channel_name) | 121 branch = BRANCH_UTILITY.GetBranchNumberForChannelName(channel_name) |
116 if real_path == '': | 122 if real_path == '': |
117 real_path = 'index.html' | 123 real_path = 'index.html' |
118 # TODO: This leaks Server instances when branch bumps. | 124 # TODO: This leaks Server instances when branch bumps. |
119 _GetInstanceForBranch(branch, self._local_path).Get(real_path, | 125 _GetInstanceForBranch(branch, self._local_path, doc_type).Get(real_path, |
120 self.request, | 126 self.request, |
121 self.response) | 127 self.response) |
122 | 128 |
123 def get(self): | 129 def get(self): |
124 path = self.request.path | 130 path = self.request.path |
125 if '_ah/warmup' in path: | 131 if '_ah/warmup' in path: |
126 logging.info('Warmup request.') | 132 logging.info('Warmup request.') |
127 if DEFAULT_BRANCH != 'local': | 133 if DEFAULT_BRANCH != 'local': |
128 self._NavigateToPath('trunk/samples.html') | 134 self._NavigateToPath('trunk/samples.html', 'extensions') |
129 self._NavigateToPath('dev/samples.html') | 135 self._NavigateToPath('dev/samples.html', 'extensions') |
130 self._NavigateToPath('beta/samples.html') | 136 self._NavigateToPath('beta/samples.html', 'extensions') |
131 self._NavigateToPath('stable/samples.html') | 137 self._NavigateToPath('stable/samples.html', 'extensions') |
132 return | 138 return |
133 | 139 |
134 # Redirect paths like "directory" to "directory/". This is so relative file | 140 # Redirect paths like "directory" to "directory/". This is so relative file |
135 # paths will know to treat this as a directory. | 141 # paths will know to treat this as a directory. |
136 if os.path.splitext(path)[1] == '' and path[-1] != '/': | 142 if os.path.splitext(path)[1] == '' and path[-1] != '/': |
137 self.redirect(path + '/') | 143 self.redirect(path + '/') |
138 path = path.replace('/chrome/extensions/', '') | 144 path = path.replace('/chrome/', '').lstrip('/') |
145 if path.startswith('extensions/'): | |
146 path = path[len('extensions/'):] | |
147 doc_type = 'extensions' | |
148 elif path.startswith('apps/'): | |
149 path = path[len('apps/'):] | |
150 doc_type = 'apps' | |
151 else: | |
152 doc_type = 'extensions' | |
153 | |
139 path = path.strip('/') | 154 path = path.strip('/') |
140 self._NavigateToPath(path) | 155 self._NavigateToPath(path, doc_type) |
OLD | NEW |