OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 import sys | 8 import sys |
9 | 9 |
10 # Add the original server location to sys.path so we are able to import | 10 # Add the original server location to sys.path so we are able to import |
11 # modules from there. | 11 # modules from there. |
12 SERVER_PATH = 'chrome/common/extensions/docs/server2' | 12 SERVER_PATH = 'chrome/common/extensions/docs/server2' |
13 if os.path.abspath(SERVER_PATH) not in sys.path: | 13 if os.path.abspath(SERVER_PATH) not in sys.path: |
14 sys.path.append(os.path.abspath(SERVER_PATH)) | 14 sys.path.append(os.path.abspath(SERVER_PATH)) |
15 | 15 |
16 from google.appengine.ext import webapp | 16 from google.appengine.ext import webapp |
17 from google.appengine.api import memcache | 17 from google.appengine.api import memcache |
18 from google.appengine.api import urlfetch | |
18 from google.appengine.ext.webapp.util import run_wsgi_app | 19 from google.appengine.ext.webapp.util import run_wsgi_app |
19 | 20 |
20 from api_data_source import APIDataSource | |
21 from api_list_data_source import APIListDataSource | |
22 from appengine_memcache import AppEngineMemcache | 21 from appengine_memcache import AppEngineMemcache |
22 from appengine_url_fetcher import AppEngineUrlFetcher | |
23 from branch_utility import BranchUtility | 23 from branch_utility import BranchUtility |
24 from example_zipper import ExampleZipper | 24 import server_instance |
25 from file_system_cache import FileSystemCache | |
26 from intro_data_source import IntroDataSource | |
27 from local_file_system import LocalFileSystem | |
28 from memcache_file_system import MemcacheFileSystem | |
29 from samples_data_source import SamplesDataSource | |
30 from server_instance import ServerInstance | |
31 from subversion_file_system import SubversionFileSystem | |
32 from template_data_source import TemplateDataSource | |
33 from appengine_url_fetcher import AppEngineUrlFetcher | |
34 | 25 |
35 SVN_URL = 'http://src.chromium.org/chrome' | 26 SVN_URL = 'http://src.chromium.org/chrome' |
36 TRUNK_URL = SVN_URL + '/trunk' | 27 TRUNK_URL = SVN_URL + '/trunk' |
37 BRANCH_URL = SVN_URL + '/branches' | 28 BRANCH_URL = SVN_URL + '/branches' |
38 | 29 |
39 EXTENSIONS_PATH = 'chrome/common/extensions' | |
40 DOCS_PATH = 'docs' | |
41 API_PATH = 'api' | |
42 INTRO_PATH = DOCS_PATH + '/server2/templates/intros' | |
43 ARTICLE_PATH = DOCS_PATH + '/server2/templates/articles' | |
44 PUBLIC_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/public' | |
45 PRIVATE_TEMPLATE_PATH = DOCS_PATH + '/server2/templates/private' | |
46 EXAMPLES_PATH = DOCS_PATH + '/examples' | |
47 FULL_EXAMPLES_PATH = DOCS_PATH + '/' + EXAMPLES_PATH | |
48 | |
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 SERVER_INSTANCES = {} | |
56 | |
57 OMAHA_PROXY_URL = 'http://omahaproxy.appspot.com/json' | 30 OMAHA_PROXY_URL = 'http://omahaproxy.appspot.com/json' |
58 BRANCH_UTILITY = BranchUtility(OMAHA_PROXY_URL, | 31 BRANCH_UTILITY = BranchUtility(OMAHA_PROXY_URL, |
59 DEFAULT_BRANCH, | 32 server_instance.DEFAULT_BRANCH, |
60 AppEngineUrlFetcher(''), | 33 AppEngineUrlFetcher(''), |
61 AppEngineMemcache('branch_utility', memcache)) | 34 AppEngineMemcache('branch_utility', memcache)) |
62 | 35 |
63 def _GetURLFromBranch(branch): | 36 def _GetURLFromBranch(branch): |
64 if branch == 'trunk': | 37 if branch == 'trunk': |
65 return TRUNK_URL + '/src' | 38 return TRUNK_URL + '/src' |
66 return BRANCH_URL + '/' + branch + '/src' | 39 return BRANCH_URL + '/' + branch + '/src' |
67 | 40 |
68 class Handler(webapp.RequestHandler): | 41 class Handler(webapp.RequestHandler): |
69 def _GetInstanceForBranch(self, branch): | |
70 if branch in SERVER_INSTANCES: | |
71 return SERVER_INSTANCES[branch] | |
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 | |
111 def get(self): | 42 def get(self): |
112 path = self.request.path | 43 path = self.request.path |
113 if '_ah/warmup' in path: | 44 if '_ah/warmup' in path: |
114 logging.info('Warmup request.') | 45 logging.info('Warmup request.') |
115 self.get('/chrome/extensions/trunk/samples.html') | 46 self.get('/chrome/extensions/trunk/samples.html') |
116 self.get('/chrome/extensions/dev/samples.html') | 47 self.get('/chrome/extensions/dev/samples.html') |
117 self.get('/chrome/extensions/beta/samples.html') | 48 self.get('/chrome/extensions/beta/samples.html') |
118 self.get('/chrome/extensions/stable/samples.html') | 49 self.get('/chrome/extensions/stable/samples.html') |
119 return | 50 return |
120 | 51 |
121 # Redirect paths like "directory" to "directory/". This is so relative file | 52 # Redirect paths like "directory" to "directory/". This is so relative file |
122 # paths will know to treat this as a directory. | 53 # paths will know to treat this as a directory. |
123 if os.path.splitext(path)[1] == '' and path[-1] != '/': | 54 if os.path.splitext(path)[1] == '' and path[-1] != '/': |
124 self.redirect(path + '/') | 55 self.redirect(path + '/') |
125 path = path.replace('/chrome/extensions/', '') | 56 path = path.replace('/chrome/extensions/', '') |
126 path = path.strip('/') | 57 path = path.strip('/') |
127 | 58 |
128 channel_name, real_path = BRANCH_UTILITY.SplitChannelNameFromPath(path) | 59 channel_name, real_path = BRANCH_UTILITY.SplitChannelNameFromPath(path) |
129 branch = BRANCH_UTILITY.GetBranchNumberForChannelName(channel_name) | 60 branch = BRANCH_UTILITY.GetBranchNumberForChannelName(channel_name) |
130 if real_path == '': | 61 if real_path == '': |
131 real_path = 'index.html' | 62 real_path = 'index.html' |
132 # TODO: This leaks Server instances when branch bumps. | 63 # TODO: This leaks Server instances when branch bumps. |
133 self._GetInstanceForBranch(branch).Get(real_path, | 64 server_instance.GetInstanceForBranch(branch, |
134 self.request, | 65 memcache, |
135 self.response) | 66 urlfetch).Get(real_path, |
67 self.request, | |
68 self.response) | |
not at google - send to devlin
2012/07/30 21:19:09
why can't you put *all* of this, besides main(), i
cduvall
2012/07/30 22:48:09
Done.
| |
136 | 69 |
137 def main(): | 70 def main(): |
138 run_wsgi_app(webapp.WSGIApplication([('/.*', Handler)], debug=False)) | 71 run_wsgi_app(webapp.WSGIApplication([('/.*', Handler)], debug=False)) |
139 | 72 |
140 if __name__ == '__main__': | 73 if __name__ == '__main__': |
141 main() | 74 main() |
OLD | NEW |