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