| 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, ToUnicode | 9 from file_system import FileNotFoundError, ToUnicode |
| 10 from future import Future | 10 from future import Future |
| 11 from patcher import Patcher | 11 from patcher import Patcher |
| 12 import svn_constants | 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 _DOCS_PATHS = [ | |
| 23 svn_constants.API_PATH, | |
| 24 svn_constants.TEMPLATE_PATH, | |
| 25 svn_constants.STATIC_PATH | |
| 26 ] | |
| 27 | 22 |
| 28 class RietveldPatcherError(Exception): | 23 class RietveldPatcherError(Exception): |
| 29 def __init__(self, message): | 24 def __init__(self, message): |
| 30 self.message = message | 25 self.message = message |
| 31 | 26 |
| 32 class _AsyncFetchFuture(object): | 27 class _AsyncFetchFuture(object): |
| 33 def __init__(self, | 28 def __init__(self, |
| 34 base_path, | |
| 35 issue, | 29 issue, |
| 36 patchset, | 30 patchset, |
| 37 files, | 31 files, |
| 38 binary, | 32 binary, |
| 39 fetcher): | 33 fetcher): |
| 40 self._base_path = base_path | |
| 41 self._issue = issue | 34 self._issue = issue |
| 42 self._patchset = patchset | 35 self._patchset = patchset |
| 43 self._files = files | 36 self._files = files |
| 44 self._binary = binary | 37 self._binary = binary |
| 45 self._tarball = fetcher.FetchAsync('tarball/%s/%s' % (issue, patchset)) | 38 self._tarball = fetcher.FetchAsync('tarball/%s/%s' % (issue, patchset)) |
| 46 | 39 |
| 47 def Get(self): | 40 def Get(self): |
| 48 tarball_result = self._tarball.Get() | 41 tarball_result = self._tarball.Get() |
| 49 if tarball_result.status_code != 200: | 42 if tarball_result.status_code != 200: |
| 50 raise RietveldPatcherError( | 43 raise RietveldPatcherError( |
| 51 'Failed to download tarball for issue %s patchset %s. Status: %s' % | 44 'Failed to download tarball for issue %s patchset %s. Status: %s' % |
| 52 (self._issue, self._patchset, tarball_result.status_code)) | 45 (self._issue, self._patchset, tarball_result.status_code)) |
| 53 | 46 |
| 54 try: | 47 try: |
| 55 tar = tarfile.open(fileobj=StringIO(tarball_result.content)) | 48 tar = tarfile.open(fileobj=StringIO(tarball_result.content)) |
| 56 except tarfile.TarError as e: | 49 except tarfile.TarError as e: |
| 57 raise RietveldPatcherError( | 50 raise RietveldPatcherError( |
| 58 'Error loading tarball for issue %s patchset %s.' % (self._issue, | 51 'Error loading tarball for issue %s patchset %s.' % (self._issue, |
| 59 self._patchset)) | 52 self._patchset)) |
| 60 | 53 |
| 61 self._value = {} | 54 self._value = {} |
| 62 for path in self._files: | 55 for path in self._files: |
| 63 if self._base_path: | 56 tar_path = 'b/%s' % path |
| 64 tar_path = 'b/%s/%s' % (self._base_path, path) | |
| 65 else: | |
| 66 tar_path = 'b/%s' % path | |
| 67 | 57 |
| 68 patched_file = None | 58 patched_file = None |
| 69 try: | 59 try: |
| 70 patched_file = tar.extractfile(tar_path) | 60 patched_file = tar.extractfile(tar_path) |
| 71 data = patched_file.read() | 61 data = patched_file.read() |
| 72 except tarfile.TarError as e: | 62 except tarfile.TarError as e: |
| 73 # Show appropriate error message in the unlikely case that the tarball | 63 # Show appropriate error message in the unlikely case that the tarball |
| 74 # is corrupted. | 64 # is corrupted. |
| 75 raise RietveldPatcherError( | 65 raise RietveldPatcherError( |
| 76 'Error extracting tarball for issue %s patchset %s file %s.' % | 66 'Error extracting tarball for issue %s patchset %s file %s.' % |
| (...skipping 10 matching lines...) Expand all Loading... |
| 87 self._value[path] = data | 77 self._value[path] = data |
| 88 else: | 78 else: |
| 89 self._value[path] = ToUnicode(data) | 79 self._value[path] = ToUnicode(data) |
| 90 | 80 |
| 91 return self._value | 81 return self._value |
| 92 | 82 |
| 93 class RietveldPatcher(Patcher): | 83 class RietveldPatcher(Patcher): |
| 94 ''' Class to fetch resources from a patchset in Rietveld. | 84 ''' Class to fetch resources from a patchset in Rietveld. |
| 95 ''' | 85 ''' |
| 96 def __init__(self, | 86 def __init__(self, |
| 97 base_path, | |
| 98 issue, | 87 issue, |
| 99 fetcher): | 88 fetcher): |
| 100 self._base_path = base_path | |
| 101 self._issue = issue | 89 self._issue = issue |
| 102 self._fetcher = fetcher | 90 self._fetcher = fetcher |
| 103 self._cache = None | 91 self._cache = None |
| 104 | 92 |
| 105 # In RietveldPatcher, the version is the latest patchset number. | 93 # In RietveldPatcher, the version is the latest patchset number. |
| 106 def GetVersion(self): | 94 def GetVersion(self): |
| 107 try: | 95 try: |
| 108 issue_json = json.loads(self._fetcher.Fetch( | 96 issue_json = json.loads(self._fetcher.Fetch( |
| 109 'api/%s' % self._issue).content) | 97 'api/%s' % self._issue).content) |
| 110 except Exception as e: | 98 except Exception as e: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 138 patchset)) | 126 patchset)) |
| 139 | 127 |
| 140 files = patchset_json.get('files') | 128 files = patchset_json.get('files') |
| 141 if files is None or not isinstance(files, dict): | 129 if files is None or not isinstance(files, dict): |
| 142 raise RietveldPatcherError('Failed to parse issue %s patchset %s.' % | 130 raise RietveldPatcherError('Failed to parse issue %s patchset %s.' % |
| 143 (self._issue, patchset)) | 131 (self._issue, patchset)) |
| 144 | 132 |
| 145 added = [] | 133 added = [] |
| 146 deleted = [] | 134 deleted = [] |
| 147 modified = [] | 135 modified = [] |
| 148 for key in files: | 136 for f in files: |
| 149 if not key.startswith(self._base_path + '/'): | 137 status = (files[f].get('status') or 'M') |
| 150 continue | 138 # status can be 'A ' or 'A + ' |
| 151 | 139 if 'A' in status: |
| 152 f = key.split(self._base_path + '/', 1)[1] | 140 added.append(f) |
| 153 if any(f.startswith(path) for path in _DOCS_PATHS): | 141 elif 'D' in status: |
| 154 status = (files[key].get('status') or 'M') | 142 deleted.append(f) |
| 155 # status can be 'A ' or 'A + ' | 143 elif 'M' in status: |
| 156 if 'A' in status: | 144 modified.append(f) |
| 157 added.append(f) | 145 else: |
| 158 elif 'D' in status: | 146 raise RietveldPatcherError('Unknown file status for file %s: "%s."' % |
| 159 deleted.append(f) | 147 (key, status)) |
| 160 elif 'M' in status: | |
| 161 modified.append(f) | |
| 162 else: | |
| 163 raise RietveldPatcherError('Unknown file status for file %s: "%s."' % | |
| 164 (key, status)) | |
| 165 | 148 |
| 166 return (added, deleted, modified) | 149 return (added, deleted, modified) |
| 167 | 150 |
| 168 def Apply(self, paths, file_system, binary, version=None): | 151 def Apply(self, paths, file_system, binary, version=None): |
| 169 if version is None: | 152 if version is None: |
| 170 version = self.GetVersion() | 153 version = self.GetVersion() |
| 171 return Future(delegate=_AsyncFetchFuture(self._base_path, | 154 return Future(delegate=_AsyncFetchFuture(self._issue, |
| 172 self._issue, | |
| 173 version, | 155 version, |
| 174 paths, | 156 paths, |
| 175 binary, | 157 binary, |
| 176 self._fetcher)) | 158 self._fetcher)) |
| 177 | 159 |
| 178 def GetIdentity(self): | 160 def GetIdentity(self): |
| 179 return self._issue | 161 return self._issue |
| OLD | NEW |