OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 json | 5 import json |
6 import tarfile | 6 import tarfile |
7 from StringIO import StringIO | 7 from StringIO import StringIO |
8 | 8 |
9 from file_system import FileNotFoundError | 9 from file_system import FileNotFoundError |
10 from future import Future | 10 from future import Future |
11 from patcher import Patcher | 11 from patcher import Patcher |
12 | 12 |
13 | 13 |
14 _CHROMIUM_REPO_BASEURLS = [ | 14 _CHROMIUM_REPO_BASEURLS = [ |
15 'https://src.chromium.org/svn/trunk/src/', | 15 'https://src.chromium.org/svn/trunk/src/', |
16 'http://src.chromium.org/svn/trunk/src/', | 16 'http://src.chromium.org/svn/trunk/src/', |
17 'svn://svn.chromium.org/chrome/trunk/src', | 17 'svn://svn.chromium.org/chrome/trunk/src', |
18 'https://chromium.googlesource.com/chromium/src.git@master', | 18 'https://chromium.googlesource.com/chromium/src.git@master', |
19 'http://git.chromium.org/chromium/src.git@master', | 19 'http://git.chromium.org/chromium/src.git@master', |
20 ] | 20 ] |
21 | 21 |
22 | 22 |
23 class RietveldPatcherError(Exception): | 23 class RietveldPatcherError(Exception): |
24 def __init__(self, message): | 24 def __init__(self, message): |
25 self.message = message | 25 self.message = message |
26 | 26 |
27 | 27 |
28 def _GetAsyncFetchCallback(issue, patchset, files, fetcher): | |
29 tarball = fetcher.FetchAsync('tarball/%s/%s' % (issue, patchset)) | |
30 | |
31 def resolve(): | |
32 tarball_result = tarball.Get() | |
33 if tarball_result.status_code != 200: | |
34 raise RietveldPatcherError( | |
35 'Failed to download tarball for issue %s patchset %s. Status: %s' % | |
36 (issue, patchset, tarball_result.status_code)) | |
37 | |
38 try: | |
39 tar = tarfile.open(fileobj=StringIO(tarball_result.content)) | |
40 except tarfile.TarError as e: | |
41 raise RietveldPatcherError( | |
42 'Error loading tarball for issue %s patchset %s.' % (issue, patchset)) | |
43 | |
44 value = {} | |
45 for path in files: | |
46 tar_path = 'b/%s' % path | |
47 | |
48 patched_file = None | |
49 try: | |
50 patched_file = tar.extractfile(tar_path) | |
51 data = patched_file.read() | |
52 except tarfile.TarError as e: | |
53 # Show appropriate error message in the unlikely case that the tarball | |
54 # is corrupted. | |
55 raise RietveldPatcherError( | |
56 'Error extracting tarball for issue %s patchset %s file %s.' % | |
57 (issue, patchset, tar_path)) | |
58 except KeyError as e: | |
59 raise FileNotFoundError( | |
60 'File %s not found in the tarball for issue %s patchset %s' % | |
61 (tar_path, issue, patchset)) | |
62 finally: | |
63 if patched_file: | |
64 patched_file.close() | |
65 | |
66 value[path] = data | |
67 | |
68 return value | |
69 | |
70 return resolve | |
71 | |
72 | |
73 class RietveldPatcher(Patcher): | 28 class RietveldPatcher(Patcher): |
74 ''' Class to fetch resources from a patchset in Rietveld. | 29 ''' Class to fetch resources from a patchset in Rietveld. |
75 ''' | 30 ''' |
76 def __init__(self, | 31 def __init__(self, |
77 issue, | 32 issue, |
78 fetcher): | 33 fetcher): |
79 self._issue = issue | 34 self._issue = issue |
80 self._fetcher = fetcher | 35 self._fetcher = fetcher |
81 self._cache = None | 36 self._cache = None |
82 | 37 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 modified.append(f) | 89 modified.append(f) |
135 else: | 90 else: |
136 raise RietveldPatcherError('Unknown file status for file %s: "%s."' % | 91 raise RietveldPatcherError('Unknown file status for file %s: "%s."' % |
137 (key, status)) | 92 (key, status)) |
138 | 93 |
139 return (added, deleted, modified) | 94 return (added, deleted, modified) |
140 | 95 |
141 def Apply(self, paths, file_system, version=None): | 96 def Apply(self, paths, file_system, version=None): |
142 if version is None: | 97 if version is None: |
143 version = self.GetVersion() | 98 version = self.GetVersion() |
144 return Future(callback=_GetAsyncFetchCallback(self._issue, | 99 |
145 version, | 100 def apply_(tarball_result): |
146 paths, | 101 if tarball_result.status_code != 200: |
147 self._fetcher)) | 102 raise RietveldPatcherError( |
| 103 'Failed to download tarball for issue %s patchset %s. Status: %s' % |
| 104 (self._issue, version, tarball_result.status_code)) |
| 105 |
| 106 try: |
| 107 tar = tarfile.open(fileobj=StringIO(tarball_result.content)) |
| 108 except tarfile.TarError as e: |
| 109 raise RietveldPatcherError( |
| 110 'Error loading tarball for issue %s patchset %s.' % (self._issue, |
| 111 version)) |
| 112 |
| 113 value = {} |
| 114 for path in paths: |
| 115 tar_path = 'b/%s' % path |
| 116 |
| 117 patched_file = None |
| 118 try: |
| 119 patched_file = tar.extractfile(tar_path) |
| 120 data = patched_file.read() |
| 121 except tarfile.TarError as e: |
| 122 # Show appropriate error message in the unlikely case that the tarball |
| 123 # is corrupted. |
| 124 raise RietveldPatcherError( |
| 125 'Error extracting tarball for issue %s patchset %s file %s.' % |
| 126 (self._issue, version, tar_path)) |
| 127 except KeyError as e: |
| 128 raise FileNotFoundError( |
| 129 'File %s not found in the tarball for issue %s patchset %s' % |
| 130 (tar_path, self._issue, version)) |
| 131 finally: |
| 132 if patched_file: |
| 133 patched_file.close() |
| 134 |
| 135 value[path] = data |
| 136 |
| 137 return value |
| 138 return self._fetcher.FetchAsync('tarball/%s/%s' % (self._issue, |
| 139 version)).Then(apply_) |
148 | 140 |
149 def GetIdentity(self): | 141 def GetIdentity(self): |
150 return self._issue | 142 return self._issue |
OLD | NEW |