OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import re | 5 import re |
6 import xml.dom.minidom as xml | 6 import xml.dom.minidom as xml |
7 from xml.parsers.expat import ExpatError | |
7 | 8 |
8 import file_system | 9 import file_system |
9 from future import Future | 10 from future import Future |
10 | 11 |
11 class SubversionFileSystem(file_system.FileSystem): | 12 class SubversionFileSystem(file_system.FileSystem): |
12 """Class to fetch resources from src.chromium.org. | 13 """Class to fetch resources from src.chromium.org. |
13 """ | 14 """ |
14 def __init__(self, fetcher): | 15 def __init__(self, fetcher, stat_fetcher): |
15 self._fetcher = fetcher | 16 self._fetcher = fetcher |
17 self._stat_fetcher = stat_fetcher | |
16 | 18 |
17 def Read(self, paths, binary=False): | 19 def Read(self, paths, binary=False): |
18 return Future(delegate=_AsyncFetchFuture(paths, self._fetcher, binary)) | 20 return Future(delegate=_AsyncFetchFuture(paths, self._fetcher, binary)) |
19 | 21 |
22 def _ParseHTML(self, html): | |
23 """Unfortunately, the viewvc page has a stray </div> tag, so this takes care | |
24 of all mismatched tags. | |
25 """ | |
26 try: | |
27 return xml.parseString(html) | |
28 except ExpatError as e: | |
29 new_html = [] | |
30 for lineno, line in enumerate(html.split('\n')): | |
31 if e.lineno != lineno + 1: | |
32 new_html.append(line) | |
33 return self._ParseHTML('\n'.join(new_html)) | |
34 | |
35 def _CreateStatInfo(self, html): | |
36 dom = self._ParseHTML(html) | |
37 # Brace yourself, this is about to get ugly. The page returned from viewvc | |
38 # was not the prettiest. | |
not at google - send to devlin
2012/08/10 06:42:24
heh. Let's hope it doesn't change; but if it does,
| |
39 tds = dom.getElementsByTagName('td') | |
40 a_list = [] | |
41 found = False | |
42 dir_revision = None | |
43 for td in tds: | |
44 if found: | |
45 dir_revision = td.getElementsByTagName('a')[0].firstChild.nodeValue | |
46 found = False | |
47 a_list.extend(td.getElementsByTagName('a')) | |
48 if (td.firstChild is not None and | |
49 td.firstChild.nodeValue == 'Directory revision:'): | |
50 found = True | |
51 child_revisions = {} | |
52 for i in range(len(a_list)): | |
53 name = a_list[i].getAttribute('name') | |
54 if name: | |
55 rev = a_list[i + 1].getElementsByTagName('strong')[0] | |
56 if 'file' in a_list[i + 1].getAttribute('title'): | |
57 child_revisions[name] = rev.firstChild.nodeValue | |
58 else: | |
59 child_revisions[name + '/'] = rev.firstChild.nodeValue | |
60 return self.StatInfo(dir_revision, child_revisions) | |
61 | |
20 def Stat(self, path): | 62 def Stat(self, path): |
21 directory = path.rsplit('/', 1)[0] | 63 directory = path.rsplit('/', 1)[0] |
22 dir_html = self._fetcher.Fetch(directory + '/').content | 64 dir_html = self._stat_fetcher.Fetch(directory + '/').content |
23 return self.StatInfo(int(re.search('([0-9]+)', dir_html).group(0))) | 65 return self._CreateStatInfo(dir_html) |
24 | 66 |
25 class _AsyncFetchFuture(object): | 67 class _AsyncFetchFuture(object): |
26 def __init__(self, paths, fetcher, binary): | 68 def __init__(self, paths, fetcher, binary): |
27 # A list of tuples of the form (path, Future). | 69 # A list of tuples of the form (path, Future). |
28 self._fetches = [] | 70 self._fetches = [] |
29 self._value = {} | 71 self._value = {} |
30 self._error = None | 72 self._error = None |
31 self._fetches = [(path, fetcher.FetchAsync(path)) for path in paths] | 73 self._fetches = [(path, fetcher.FetchAsync(path)) for path in paths] |
32 self._binary = binary | 74 self._binary = binary |
33 | 75 |
(...skipping 12 matching lines...) Expand all Loading... | |
46 elif path.endswith('/'): | 88 elif path.endswith('/'): |
47 self._value[path] = self._ListDir(result.content) | 89 self._value[path] = self._ListDir(result.content) |
48 elif not self._binary: | 90 elif not self._binary: |
49 self._value[path] = file_system._ProcessFileData(result.content, path) | 91 self._value[path] = file_system._ProcessFileData(result.content, path) |
50 else: | 92 else: |
51 self._value[path] = result.content | 93 self._value[path] = result.content |
52 if self._error is not None: | 94 if self._error is not None: |
53 raise self._error | 95 raise self._error |
54 return self._value | 96 return self._value |
55 | 97 |
OLD | NEW |