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('', urlfetch), | |
42 AppEngineMemcache('branch_utility', memcache)) | |
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 urlfetch) | |
67 file_system = MemcacheFileSystem(SubversionFileSystem(fetcher), | |
68 AppEngineMemcache(branch, memcache)) | |
69 | |
70 cache_builder = FileSystemCache.Builder(file_system) | |
71 api_data_source = APIDataSource(cache_builder, API_PATH) | |
72 api_list_data_source = APIListDataSource(cache_builder, | |
73 file_system, | |
74 API_PATH, | |
75 PUBLIC_TEMPLATE_PATH) | |
76 intro_data_source = IntroDataSource(cache_builder, | |
77 [INTRO_PATH, ARTICLE_PATH]) | |
78 samples_data_source_factory = SamplesDataSource.Factory(branch, | |
79 file_system, | |
80 cache_builder, | |
81 EXAMPLES_PATH) | |
82 template_data_source_factory = TemplateDataSource.Factory( | |
83 branch, | |
84 api_data_source, | |
85 api_list_data_source, | |
86 intro_data_source, | |
87 samples_data_source_factory, | |
88 cache_builder, | |
89 PUBLIC_TEMPLATE_PATH, | |
90 PRIVATE_TEMPLATE_PATH) | |
91 example_zipper = ExampleZipper(file_system, | |
92 cache_builder, | |
93 DOCS_PATH, | |
94 EXAMPLES_PATH) | |
95 SERVER_INSTANCES[branch] = ServerInstance( | |
96 template_data_source_factory, | |
97 example_zipper, | |
98 cache_builder) | |
99 return SERVER_INSTANCES[branch] | |
62 | 100 |
63 def _GetURLFromBranch(branch): | 101 def _GetURLFromBranch(branch): |
64 if branch == 'trunk': | 102 if branch == 'trunk': |
65 return TRUNK_URL + '/src' | 103 return TRUNK_URL + '/src' |
66 return BRANCH_URL + '/' + branch + '/src' | 104 return BRANCH_URL + '/' + branch + '/src' |
67 | 105 |
68 class Handler(webapp.RequestHandler): | 106 class Handler(webapp.RequestHandler): |
69 def _GetInstanceForBranch(self, branch): | 107 def __init__(self, request, response, local_path=EXTENSIONS_PATH): |
70 if branch in SERVER_INSTANCES: | 108 self._local_path = local_path |
71 return SERVER_INSTANCES[branch] | 109 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 | |
80 cache_builder = FileSystemCache.Builder(file_system) | |
81 api_data_source = APIDataSource(cache_builder, API_PATH) | |
82 api_list_data_source = APIListDataSource(cache_builder, | |
83 file_system, | |
84 API_PATH, | |
85 PUBLIC_TEMPLATE_PATH) | |
86 intro_data_source = IntroDataSource(cache_builder, | |
87 [INTRO_PATH, ARTICLE_PATH]) | |
88 samples_data_source_factory = SamplesDataSource.Factory(branch, | |
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 | 110 |
111 def get(self): | 111 def get(self): |
112 path = self.request.path | 112 path = self.request.path |
113 if '_ah/warmup' in path: | 113 if '_ah/warmup' in path: |
114 logging.info('Warmup request.') | 114 logging.info('Warmup request.') |
115 self.get('/chrome/extensions/trunk/samples.html') | 115 self.request.path = '/chrome/extensions/trunk/samples.html' |
116 self.get('/chrome/extensions/dev/samples.html') | 116 self.get() |
117 self.get('/chrome/extensions/beta/samples.html') | 117 self.request.path = '/chrome/extensions/dev/samples.html' |
118 self.get('/chrome/extensions/stable/samples.html') | 118 self.get() |
119 self.request.path = '/chrome/extensions/beta/samples.html' | |
120 self.get() | |
121 self.request.path = '/chrome/extensions/stable/samples.html' | |
122 self.get() | |
not at google - send to devlin
2012/07/30 23:00:44
recursively calling get..?
Wow I didn't even noti
cduvall
2012/07/30 23:38:18
Done.
| |
119 return | 123 return |
120 | 124 |
121 # Redirect paths like "directory" to "directory/". This is so relative file | 125 # Redirect paths like "directory" to "directory/". This is so relative file |
122 # paths will know to treat this as a directory. | 126 # paths will know to treat this as a directory. |
123 if os.path.splitext(path)[1] == '' and path[-1] != '/': | 127 if os.path.splitext(path)[1] == '' and path[-1] != '/': |
124 self.redirect(path + '/') | 128 self.redirect(path + '/') |
125 path = path.replace('/chrome/extensions/', '') | 129 path = path.replace('/chrome/extensions/', '') |
126 path = path.strip('/') | 130 path = path.strip('/') |
127 | 131 |
128 channel_name, real_path = BRANCH_UTILITY.SplitChannelNameFromPath(path) | 132 channel_name, real_path = BRANCH_UTILITY.SplitChannelNameFromPath(path) |
129 branch = BRANCH_UTILITY.GetBranchNumberForChannelName(channel_name) | 133 branch = BRANCH_UTILITY.GetBranchNumberForChannelName(channel_name) |
130 if real_path == '': | 134 if real_path == '': |
131 real_path = 'index.html' | 135 real_path = 'index.html' |
132 # TODO: This leaks Server instances when branch bumps. | 136 # TODO: This leaks Server instances when branch bumps. |
133 self._GetInstanceForBranch(branch).Get(real_path, | 137 _GetInstanceForBranch(branch, self._local_path).Get(real_path, |
134 self.request, | 138 self.request, |
135 self.response) | 139 self.response) |
136 | |
137 def main(): | |
138 run_wsgi_app(webapp.WSGIApplication([('/.*', Handler)], debug=False)) | |
139 | |
140 if __name__ == '__main__': | |
141 main() | |
OLD | NEW |