OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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 | |
4 # found in the LICENSE file. | |
5 | |
6 import logging | |
7 import os | |
8 import sys | |
9 | |
10 # Add the original server location to sys.path so we are able to import | |
11 # modules from there. | |
12 SERVER_PATH = 'chrome/common/extensions/docs/server2' | |
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 | |
20 from api_data_source import APIDataSource | |
21 from api_list_data_source import APIListDataSource | |
22 from appengine_memcache import AppEngineMemcache | |
23 from branch_utility import BranchUtility | |
24 from example_zipper import ExampleZipper | |
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 | |
35 SVN_URL = 'http://src.chromium.org/chrome' | |
36 TRUNK_URL = SVN_URL + '/trunk' | |
37 BRANCH_URL = SVN_URL + '/branches' | |
38 | |
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' | |
58 BRANCH_UTILITY = BranchUtility(OMAHA_PROXY_URL, | |
59 DEFAULT_BRANCH, | |
60 AppEngineUrlFetcher(''), | |
61 AppEngineMemcache('branch_utility', memcache)) | |
62 | |
63 def _GetURLFromBranch(branch): | |
64 if branch == 'trunk': | |
65 return TRUNK_URL + '/src' | |
66 return BRANCH_URL + '/' + branch + '/src' | |
67 | |
68 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): | |
112 path = self.request.path | |
113 if '_ah/warmup' in path: | |
114 logging.info('Warmup request.') | |
115 self.get('/chrome/extensions/trunk/samples.html') | |
116 self.get('/chrome/extensions/dev/samples.html') | |
117 self.get('/chrome/extensions/beta/samples.html') | |
118 self.get('/chrome/extensions/stable/samples.html') | |
119 return | |
120 | |
121 # Redirect paths like "directory" to "directory/". This is so relative file | |
122 # paths will know to treat this as a directory. | |
123 if os.path.splitext(path)[1] == '' and path[-1] != '/': | |
124 self.redirect(path + '/') | |
125 path = path.replace('/chrome/extensions/', '') | |
126 path = path.strip('/') | |
127 | |
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 |