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
3 # found in the LICENSE file.
5 # These are fake fetchers that are used for testing and the preview server.
6 # They return canned responses for URLs. appengine_wrappers.py uses the fake
7 # fetchers if the App Engine imports fail.
13 import appengine_wrappers
17 class _FakeFetcher(object):
18 def __init__(self, base_path):
19 self._base_path = base_path
21 def _ReadFile(self, path, mode='rb'):
22 with open(os.path.join(self._base_path, path), mode) as f:
25 def _ListDir(self, path):
26 return os.listdir(os.path.join(self._base_path, path))
28 def _IsDir(self, path):
29 return os.path.isdir(os.path.join(self._base_path, path))
31 def _Stat(self, path):
32 return int(os.stat(os.path.join(self._base_path, path)).st_mtime)
35 class FakeOmahaProxy(_FakeFetcher):
37 return self._ReadFile(os.path.join('server2',
43 class FakeOmahaHistory(_FakeFetcher):
45 return self._ReadFile(os.path.join('server2',
51 class FakeSubversionServer(_FakeFetcher):
52 def __init__(self, base_path):
53 _FakeFetcher.__init__(self, base_path)
54 self._base_pattern = re.compile(r'.*chrome/common/extensions/(.*)')
57 url = url.rsplit('?', 1)[0]
58 path = os.path.join(os.pardir, self._base_pattern.match(url).group(1))
60 html = ['<html>Revision 000000']
62 for f in self._ListDir(path):
65 if self._IsDir(os.path.join(path, f)):
66 html.append('<a>' + f + '/</a>')
68 html.append('<a>' + f + '</a>')
69 html.append('</html>')
70 return '\n'.join(html)
74 return self._ReadFile(path)
79 class FakeViewvcServer(_FakeFetcher):
80 def __init__(self, base_path):
81 _FakeFetcher.__init__(self, base_path)
82 self._base_pattern = re.compile(r'.*chrome/common/extensions/+(.*)')
85 url = url.rsplit('?', 1)[0]
86 path = os.path.join(os.pardir, self._base_pattern.match(url).group(1))
88 html = ['<table><tbody><tr>...</tr>']
89 # The version of the directory.
90 dir_stat = self._Stat(path)
92 html.append('<td>Directory revision:</td>')
93 html.append('<td><a>%s</a><a></a></td>' % dir_stat)
95 # The version of each file.
96 for f in self._ListDir(path):
100 html.append(' <td><a>%s%s</a></td>' % (
101 f, '/' if self._IsDir(os.path.join(path, f)) else ''))
102 stat = self._Stat(os.path.join(path, f))
103 html.append(' <td><a><strong>%s</strong></a></td>' % stat)
104 html.append('<td></td><td></td><td></td>')
106 html.append('</tbody></table>')
107 return '\n'.join(html)
109 return self._ReadFile(path)
114 class FakeGithubStat(_FakeFetcher):
115 def fetch(self, url):
116 return '{ "commit": { "tree": { "sha": 0} } }'
119 class FakeGithubZip(_FakeFetcher):
120 def fetch(self, url):
122 return self._ReadFile(os.path.join('server2',
124 'github_file_system',
131 class FakeRietveldAPI(_FakeFetcher):
132 def __init__(self, base_path):
133 _FakeFetcher.__init__(self, base_path)
134 self._base_pattern = re.compile(r'.*/(api/.*)')
136 def fetch(self, url):
138 return self._ReadFile(
139 os.path.join('server2',
142 self._base_pattern.match(url).group(1),
148 class FakeRietveldTarball(_FakeFetcher):
149 def __init__(self, base_path):
150 _FakeFetcher.__init__(self, base_path)
151 self._base_pattern = re.compile(r'.*/(tarball/\d+/\d+)')
153 def fetch(self, url):
155 return self._ReadFile(
156 os.path.join('server2',
159 self._base_pattern.match(url).group(1) + '.tar.bz2'))
164 def ConfigureFakeFetchers():
165 '''Configure the fake fetcher paths relative to the docs directory.
167 docs = '/'.join((sys.path[0], os.pardir))
168 appengine_wrappers.ConfigureFakeUrlFetch({
169 url_constants.OMAHA_PROXY_URL: FakeOmahaProxy(docs),
170 re.escape(url_constants.OMAHA_DEV_HISTORY): FakeOmahaHistory(docs),
171 '%s/.*' % url_constants.SVN_URL: FakeSubversionServer(docs),
172 '%s/.*' % url_constants.VIEWVC_URL: FakeViewvcServer(docs),
173 '%s/.*/commits/.*' % url_constants.GITHUB_REPOS: FakeGithubStat(docs),
174 '%s/.*/zipball' % url_constants.GITHUB_REPOS: FakeGithubZip(docs),
175 '%s/api/.*' % url_constants.CODEREVIEW_SERVER: FakeRietveldAPI(docs),
176 '%s/tarball/.*' % url_constants.CODEREVIEW_SERVER:
177 FakeRietveldTarball(docs),