- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / fake_fetchers.py
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.
4
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.
8
9 import os
10 import re
11 import sys
12
13 import appengine_wrappers
14 import url_constants
15
16
17 class _FakeFetcher(object):
18   def __init__(self, base_path):
19     self._base_path = base_path
20
21   def _ReadFile(self, path, mode='rb'):
22     with open(os.path.join(self._base_path, path), mode) as f:
23       return f.read()
24
25   def _ListDir(self, path):
26     return os.listdir(os.path.join(self._base_path, path))
27
28   def _IsDir(self, path):
29     return os.path.isdir(os.path.join(self._base_path, path))
30
31   def _Stat(self, path):
32     return int(os.stat(os.path.join(self._base_path, path)).st_mtime)
33
34
35 class FakeOmahaProxy(_FakeFetcher):
36   def fetch(self, url):
37     return self._ReadFile(os.path.join('server2',
38                                        'test_data',
39                                        'branch_utility',
40                                        'first.json'))
41
42
43 class FakeOmahaHistory(_FakeFetcher):
44   def fetch(self, url):
45     return self._ReadFile(os.path.join('server2',
46                                        'test_data',
47                                        'branch_utility',
48                                        'second.json'))
49
50
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/(.*)')
55
56   def fetch(self, url):
57     url = url.rsplit('?', 1)[0]
58     path = os.path.join(os.pardir, self._base_pattern.match(url).group(1))
59     if self._IsDir(path):
60       html = ['<html>Revision 000000']
61       try:
62         for f in self._ListDir(path):
63           if f.startswith('.'):
64             continue
65           if self._IsDir(os.path.join(path, f)):
66             html.append('<a>' + f + '/</a>')
67           else:
68             html.append('<a>' + f + '</a>')
69         html.append('</html>')
70         return '\n'.join(html)
71       except OSError as e:
72         return None
73     try:
74       return self._ReadFile(path)
75     except IOError as e:
76       return None
77
78
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/+(.*)')
83
84   def fetch(self, url):
85     url = url.rsplit('?', 1)[0]
86     path = os.path.join(os.pardir, self._base_pattern.match(url).group(1))
87     if self._IsDir(path):
88       html = ['<table><tbody><tr>...</tr>']
89       # The version of the directory.
90       dir_stat = self._Stat(path)
91       html.append('<tr>')
92       html.append('<td>Directory revision:</td>')
93       html.append('<td><a>%s</a><a></a></td>' % dir_stat)
94       html.append('</tr>')
95       # The version of each file.
96       for f in self._ListDir(path):
97         if f.startswith('.'):
98           continue
99         html.append('<tr>')
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>')
105         html.append('</tr>')
106       html.append('</tbody></table>')
107       return '\n'.join(html)
108     try:
109       return self._ReadFile(path)
110     except IOError as e:
111       return None
112
113
114 class FakeGithubStat(_FakeFetcher):
115   def fetch(self, url):
116     return '{ "commit": { "tree": { "sha": 0} } }'
117
118
119 class FakeGithubZip(_FakeFetcher):
120   def fetch(self, url):
121     try:
122       return self._ReadFile(os.path.join('server2',
123                                          'test_data',
124                                          'github_file_system',
125                                          'apps_samples.zip'),
126                             mode='rb')
127     except IOError:
128       return None
129
130
131 class FakeRietveldAPI(_FakeFetcher):
132   def __init__(self, base_path):
133     _FakeFetcher.__init__(self, base_path)
134     self._base_pattern = re.compile(r'.*/(api/.*)')
135
136   def fetch(self, url):
137     try:
138       return self._ReadFile(
139           os.path.join('server2',
140                        'test_data',
141                        'rietveld_patcher',
142                        self._base_pattern.match(url).group(1),
143                        'json'))
144     except IOError:
145       return None
146
147
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+)')
152
153   def fetch(self, url):
154     try:
155       return self._ReadFile(
156           os.path.join('server2',
157                        'test_data',
158                        'rietveld_patcher',
159                        self._base_pattern.match(url).group(1) + '.tar.bz2'))
160     except IOError:
161       return None
162
163
164 def ConfigureFakeFetchers():
165   '''Configure the fake fetcher paths relative to the docs directory.
166   '''
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),
178   })