OLD | NEW |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # 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 |
4 # found in the LICENSE file. | 3 # found in the LICENSE file. |
5 | 4 |
6 import logging | 5 import logging |
7 import os | 6 import os |
8 import sys | 7 import sys |
9 | 8 |
10 # Add the original server location to sys.path so we are able to import | 9 from appengine_wrappers import webapp |
11 # modules from there. | 10 from appengine_wrappers import memcache |
12 SERVER_PATH = 'chrome/common/extensions/docs/server2' | 11 from appengine_wrappers import urlfetch |
13 if os.path.abspath(SERVER_PATH) not in sys.path: | |
14 sys.path.append(os.path.abspath(SERVER_PATH)) | |
15 | |
16 from google.appengine.ext import webapp | |
17 from google.appengine.api import memcache | |
18 from google.appengine.ext.webapp.util import run_wsgi_app | |
19 | 12 |
20 from api_data_source import APIDataSource | 13 from api_data_source import APIDataSource |
21 from api_list_data_source import APIListDataSource | 14 from api_list_data_source import APIListDataSource |
22 from appengine_memcache import AppEngineMemcache | 15 from appengine_memcache import AppEngineMemcache |
| 16 from appengine_url_fetcher import AppEngineUrlFetcher |
23 from branch_utility import BranchUtility | 17 from branch_utility import BranchUtility |
24 from example_zipper import ExampleZipper | 18 from example_zipper import ExampleZipper |
25 from file_system_cache import FileSystemCache | 19 from file_system_cache import FileSystemCache |
26 from intro_data_source import IntroDataSource | 20 from intro_data_source import IntroDataSource |
27 from local_file_system import LocalFileSystem | 21 from local_file_system import LocalFileSystem |
28 from memcache_file_system import MemcacheFileSystem | 22 from memcache_file_system import MemcacheFileSystem |
29 from samples_data_source import SamplesDataSource | 23 from samples_data_source import SamplesDataSource |
30 from server_instance import ServerInstance | 24 from server_instance import ServerInstance |
31 from subversion_file_system import SubversionFileSystem | 25 from subversion_file_system import SubversionFileSystem |
32 from template_data_source import TemplateDataSource | 26 from template_data_source import TemplateDataSource |
33 from appengine_url_fetcher import AppEngineUrlFetcher | 27 from appengine_url_fetcher import AppEngineUrlFetcher |
34 | 28 |
| 29 # The branch that the server will default to when no branch is specified in the |
| 30 # URL. This is necessary because it is not possible to pass flags to the script |
| 31 # handler. |
| 32 DEFAULT_BRANCH = 'local' |
| 33 |
35 SVN_URL = 'http://src.chromium.org/chrome' | 34 SVN_URL = 'http://src.chromium.org/chrome' |
36 TRUNK_URL = SVN_URL + '/trunk' | 35 TRUNK_URL = SVN_URL + '/trunk' |
37 BRANCH_URL = SVN_URL + '/branches' | 36 BRANCH_URL = SVN_URL + '/branches' |
38 | 37 |
| 38 OMAHA_PROXY_URL = 'http://omahaproxy.appspot.com/json' |
| 39 BRANCH_UTILITY = BranchUtility(OMAHA_PROXY_URL, |
| 40 DEFAULT_BRANCH, |
| 41 AppEngineUrlFetcher(''), |
| 42 AppEngineMemcache('branch_utility')) |
| 43 |
| 44 STATIC_DIR_PREFIX = 'docs/server2' |
39 EXTENSIONS_PATH = 'chrome/common/extensions' | 45 EXTENSIONS_PATH = 'chrome/common/extensions' |
40 DOCS_PATH = 'docs' | 46 DOCS_PATH = 'docs' |
41 API_PATH = 'api' | 47 API_PATH = 'api' |
42 INTRO_PATH = DOCS_PATH + '/server2/templates/intros' | 48 INTRO_PATH = DOCS_PATH + '/server2/templates/intros' |
43 ARTICLE_PATH = DOCS_PATH + '/server2/templates/articles' | 49 ARTICLE_PATH = DOCS_PATH + '/server2/templates/articles' |
44 PUBLIC_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/public' | 50 PUBLIC_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/public' |
45 PRIVATE_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/private' | 51 PRIVATE_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/private' |
46 EXAMPLES_PATH = DOCS_PATH + '/examples' | 52 EXAMPLES_PATH = DOCS_PATH + '/examples' |
47 FULL_EXAMPLES_PATH = DOCS_PATH + '/' + EXAMPLES_PATH | 53 FULL_EXAMPLES_PATH = DOCS_PATH + '/' + EXAMPLES_PATH |
48 | 54 |
49 # The branch that the server will default to when no branch is specified in the | |
50 # URL. This is necessary because it is not possible to pass flags to the script | |
51 # handler. | |
52 DEFAULT_BRANCH = 'local' | |
53 | |
54 # Global cache of instances because Handler is recreated for every request. | 55 # Global cache of instances because Handler is recreated for every request. |
55 SERVER_INSTANCES = {} | 56 SERVER_INSTANCES = {} |
56 | 57 |
57 OMAHA_PROXY_URL = 'http://omahaproxy.appspot.com/json' | 58 def _GetInstanceForBranch(branch, local_path): |
58 BRANCH_UTILITY = BranchUtility(OMAHA_PROXY_URL, | 59 if branch in SERVER_INSTANCES: |
59 DEFAULT_BRANCH, | 60 return SERVER_INSTANCES[branch] |
60 AppEngineUrlFetcher(''), | 61 if branch == 'local': |
61 AppEngineMemcache('branch_utility', memcache)) | 62 file_system = LocalFileSystem(local_path) |
| 63 else: |
| 64 fetcher = AppEngineUrlFetcher( |
| 65 _GetURLFromBranch(branch) + '/' + EXTENSIONS_PATH) |
| 66 file_system = MemcacheFileSystem(SubversionFileSystem(fetcher), |
| 67 AppEngineMemcache(branch)) |
| 68 |
| 69 cache_builder = FileSystemCache.Builder(file_system) |
| 70 api_data_source = APIDataSource(cache_builder, API_PATH) |
| 71 api_list_data_source = APIListDataSource(cache_builder, |
| 72 file_system, |
| 73 API_PATH, |
| 74 PUBLIC_TEMPLATE_PATH) |
| 75 intro_data_source = IntroDataSource(cache_builder, |
| 76 [INTRO_PATH, ARTICLE_PATH]) |
| 77 samples_data_source_factory = SamplesDataSource.Factory(branch, |
| 78 file_system, |
| 79 cache_builder, |
| 80 EXAMPLES_PATH) |
| 81 template_data_source_factory = TemplateDataSource.Factory( |
| 82 branch, |
| 83 api_data_source, |
| 84 api_list_data_source, |
| 85 intro_data_source, |
| 86 samples_data_source_factory, |
| 87 cache_builder, |
| 88 PUBLIC_TEMPLATE_PATH, |
| 89 PRIVATE_TEMPLATE_PATH) |
| 90 example_zipper = ExampleZipper(file_system, |
| 91 cache_builder, |
| 92 DOCS_PATH, |
| 93 EXAMPLES_PATH) |
| 94 SERVER_INSTANCES[branch] = ServerInstance( |
| 95 template_data_source_factory, |
| 96 example_zipper, |
| 97 cache_builder) |
| 98 return SERVER_INSTANCES[branch] |
62 | 99 |
63 def _GetURLFromBranch(branch): | 100 def _GetURLFromBranch(branch): |
64 if branch == 'trunk': | 101 if branch == 'trunk': |
65 return TRUNK_URL + '/src' | 102 return TRUNK_URL + '/src' |
66 return BRANCH_URL + '/' + branch + '/src' | 103 return BRANCH_URL + '/' + branch + '/src' |
67 | 104 |
68 class Handler(webapp.RequestHandler): | 105 class Handler(webapp.RequestHandler): |
69 def _GetInstanceForBranch(self, branch): | 106 def __init__(self, request, response, local_path=EXTENSIONS_PATH): |
70 if branch in SERVER_INSTANCES: | 107 self._local_path = local_path |
71 return SERVER_INSTANCES[branch] | 108 super(Handler, self).__init__(request, response) |
72 if branch == 'local': | |
73 file_system = LocalFileSystem(EXTENSIONS_PATH) | |
74 else: | |
75 fetcher = AppEngineUrlFetcher( | |
76 _GetURLFromBranch(branch) + '/' + EXTENSIONS_PATH) | |
77 file_system = MemcacheFileSystem(SubversionFileSystem(fetcher), | |
78 AppEngineMemcache(branch, memcache)) | |
79 | 109 |
80 cache_builder = FileSystemCache.Builder(file_system) | 110 def _NavigateToPath(self, path): |
81 api_data_source = APIDataSource(cache_builder, API_PATH) | 111 channel_name, real_path = BRANCH_UTILITY.SplitChannelNameFromPath(path) |
82 api_list_data_source = APIListDataSource(cache_builder, | 112 branch = BRANCH_UTILITY.GetBranchNumberForChannelName(channel_name) |
83 file_system, | 113 if real_path == '': |
84 API_PATH, | 114 real_path = 'index.html' |
85 PUBLIC_TEMPLATE_PATH) | 115 # TODO: This leaks Server instances when branch bumps. |
86 intro_data_source = IntroDataSource(cache_builder, | 116 _GetInstanceForBranch(branch, self._local_path).Get(real_path, |
87 [INTRO_PATH, ARTICLE_PATH]) | 117 self.request, |
88 samples_data_source_factory = SamplesDataSource.Factory(branch, | 118 self.response) |
89 file_system, | |
90 cache_builder, | |
91 EXAMPLES_PATH) | |
92 template_data_source_factory = TemplateDataSource.Factory( | |
93 branch, | |
94 api_data_source, | |
95 api_list_data_source, | |
96 intro_data_source, | |
97 samples_data_source_factory, | |
98 cache_builder, | |
99 PUBLIC_TEMPLATE_PATH, | |
100 PRIVATE_TEMPLATE_PATH) | |
101 example_zipper = ExampleZipper(file_system, | |
102 cache_builder, | |
103 DOCS_PATH, | |
104 EXAMPLES_PATH) | |
105 SERVER_INSTANCES[branch] = ServerInstance( | |
106 template_data_source_factory, | |
107 example_zipper, | |
108 cache_builder) | |
109 return SERVER_INSTANCES[branch] | |
110 | 119 |
111 def get(self): | 120 def get(self): |
112 path = self.request.path | 121 path = self.request.path |
113 if '_ah/warmup' in path: | 122 if '_ah/warmup' in path: |
114 logging.info('Warmup request.') | 123 logging.info('Warmup request.') |
115 self.get('/chrome/extensions/trunk/samples.html') | 124 self._NavigateToPath('trunk/samples.html') |
116 self.get('/chrome/extensions/dev/samples.html') | 125 self._NavigateToPath('dev/samples.html') |
117 self.get('/chrome/extensions/beta/samples.html') | 126 self._NavigateToPath('beta/samples.html') |
118 self.get('/chrome/extensions/stable/samples.html') | 127 self._NavigateToPath('stable/samples.html') |
119 return | 128 return |
120 | 129 |
121 # Redirect paths like "directory" to "directory/". This is so relative file | 130 # Redirect paths like "directory" to "directory/". This is so relative file |
122 # paths will know to treat this as a directory. | 131 # paths will know to treat this as a directory. |
123 if os.path.splitext(path)[1] == '' and path[-1] != '/': | 132 if os.path.splitext(path)[1] == '' and path[-1] != '/': |
124 self.redirect(path + '/') | 133 self.redirect(path + '/') |
125 path = path.replace('/chrome/extensions/', '') | 134 path = path.replace('/chrome/extensions/', '') |
126 path = path.strip('/') | 135 path = path.strip('/') |
127 | 136 self._NavigateToPath(path) |
128 channel_name, real_path = BRANCH_UTILITY.SplitChannelNameFromPath(path) | |
129 branch = BRANCH_UTILITY.GetBranchNumberForChannelName(channel_name) | |
130 if real_path == '': | |
131 real_path = 'index.html' | |
132 # TODO: This leaks Server instances when branch bumps. | |
133 self._GetInstanceForBranch(branch).Get(real_path, | |
134 self.request, | |
135 self.response) | |
136 | |
137 def main(): | |
138 run_wsgi_app(webapp.WSGIApplication([('/.*', Handler)], debug=False)) | |
139 | |
140 if __name__ == '__main__': | |
141 main() | |
OLD | NEW |