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 logging |
5 import re | 6 import re |
| 7 import time |
6 import xml.dom.minidom as xml | 8 import xml.dom.minidom as xml |
7 from xml.parsers.expat import ExpatError | 9 from xml.parsers.expat import ExpatError |
8 | 10 |
9 import file_system | 11 import file_system |
10 from future import Future | 12 from future import Future |
11 | 13 |
| 14 class _AsyncFetchFuture(object): |
| 15 def __init__(self, paths, fetcher, binary): |
| 16 # A list of tuples of the form (path, Future). |
| 17 self._fetches = [(path, fetcher.FetchAsync(path)) for path in paths] |
| 18 self._value = {} |
| 19 self._error = None |
| 20 self._binary = binary |
| 21 self._start_time = time.time() |
| 22 |
| 23 def _ListDir(self, directory): |
| 24 dom = xml.parseString(directory) |
| 25 files = [elem.childNodes[0].data for elem in dom.getElementsByTagName('a')] |
| 26 files.remove('..') |
| 27 return files |
| 28 |
| 29 def Get(self): |
| 30 try: |
| 31 return self._DoGet() |
| 32 finally: |
| 33 logging.info("Fetching %s took %sms", |
| 34 [fetch[0] for fetch in self._fetches], |
| 35 (time.time() - self._start_time) * 1000) |
| 36 |
| 37 def _DoGet(self): |
| 38 for path, future in self._fetches: |
| 39 result = future.Get() |
| 40 if result.status_code == 404: |
| 41 raise file_system.FileNotFoundError(path) |
| 42 elif path.endswith('/'): |
| 43 self._value[path] = self._ListDir(result.content) |
| 44 elif not self._binary: |
| 45 self._value[path] = file_system._ProcessFileData(result.content, path) |
| 46 else: |
| 47 self._value[path] = result.content |
| 48 if self._error is not None: |
| 49 raise self._error |
| 50 return self._value |
| 51 |
12 class SubversionFileSystem(file_system.FileSystem): | 52 class SubversionFileSystem(file_system.FileSystem): |
13 """Class to fetch resources from src.chromium.org. | 53 """Class to fetch resources from src.chromium.org. |
14 """ | 54 """ |
15 def __init__(self, fetcher, stat_fetcher): | 55 def __init__(self, fetcher, stat_fetcher): |
16 self._fetcher = fetcher | 56 self._fetcher = fetcher |
17 self._stat_fetcher = stat_fetcher | 57 self._stat_fetcher = stat_fetcher |
18 | 58 |
19 def Read(self, paths, binary=False): | 59 def Read(self, paths, binary=False): |
20 return Future(delegate=_AsyncFetchFuture(paths, self._fetcher, binary)) | 60 return Future(delegate=_AsyncFetchFuture(paths, self._fetcher, binary)) |
21 | 61 |
(...skipping 29 matching lines...) Expand all Loading... |
51 if i + 1 >= len(a_list): | 91 if i + 1 >= len(a_list): |
52 break | 92 break |
53 next_a = a_list[i + 1] | 93 next_a = a_list[i + 1] |
54 name = a.getAttribute('name') | 94 name = a.getAttribute('name') |
55 if name: | 95 if name: |
56 rev = next_a.getElementsByTagName('strong')[0] | 96 rev = next_a.getElementsByTagName('strong')[0] |
57 if 'file' in next_a.getAttribute('title'): | 97 if 'file' in next_a.getAttribute('title'): |
58 child_revisions[name] = rev.firstChild.nodeValue | 98 child_revisions[name] = rev.firstChild.nodeValue |
59 else: | 99 else: |
60 child_revisions[name + '/'] = rev.firstChild.nodeValue | 100 child_revisions[name + '/'] = rev.firstChild.nodeValue |
61 return self.StatInfo(dir_revision, child_revisions) | 101 return file_system.StatInfo(dir_revision, child_revisions) |
62 | 102 |
63 def Stat(self, path): | 103 def Stat(self, path): |
| 104 start_time = time.time() |
| 105 try: |
| 106 return self._DoStat(path) |
| 107 finally: |
| 108 logging.info("Stat %s took %sms", path, (time.time() - start_time) * 1000) |
| 109 |
| 110 def _DoStat(self, path): |
64 directory = path.rsplit('/', 1)[0] | 111 directory = path.rsplit('/', 1)[0] |
65 result = self._stat_fetcher.Fetch(directory + '/') | 112 result = self._stat_fetcher.Fetch(directory + '/') |
66 if result.status_code == 404: | 113 if result.status_code == 404: |
67 raise file_system.FileNotFoundError(path) | 114 raise file_system.FileNotFoundError(path) |
68 stat_info = self._CreateStatInfo(result.content) | 115 stat_info = self._CreateStatInfo(result.content) |
69 if not path.endswith('/'): | 116 if not path.endswith('/'): |
70 filename = path.rsplit('/', 1)[-1] | 117 filename = path.rsplit('/', 1)[-1] |
71 if filename not in stat_info.child_versions: | 118 if filename not in stat_info.child_versions: |
72 raise file_system.FileNotFoundError(path) | 119 raise file_system.FileNotFoundError(path) |
73 stat_info.version = stat_info.child_versions[filename] | 120 stat_info.version = stat_info.child_versions[filename] |
74 return stat_info | 121 return stat_info |
75 | |
76 class _AsyncFetchFuture(object): | |
77 def __init__(self, paths, fetcher, binary): | |
78 # A list of tuples of the form (path, Future). | |
79 self._fetches = [] | |
80 self._value = {} | |
81 self._error = None | |
82 self._fetches = [(path, fetcher.FetchAsync(path)) for path in paths] | |
83 self._binary = binary | |
84 | |
85 def _ListDir(self, directory): | |
86 dom = xml.parseString(directory) | |
87 files = [elem.childNodes[0].data for elem in dom.getElementsByTagName('a')] | |
88 if '..' in files: | |
89 files.remove('..') | |
90 return files | |
91 | |
92 def Get(self): | |
93 for path, future in self._fetches: | |
94 result = future.Get() | |
95 if result.status_code == 404: | |
96 raise file_system.FileNotFoundError(path) | |
97 elif path.endswith('/'): | |
98 self._value[path] = self._ListDir(result.content) | |
99 elif not self._binary: | |
100 self._value[path] = file_system._ProcessFileData(result.content, path) | |
101 else: | |
102 self._value[path] = result.content | |
103 if self._error is not None: | |
104 raise self._error | |
105 return self._value | |
106 | |
OLD | NEW |