def _CompileContent(self, path, text): assert text is not None, path mimetype = mimetypes.guess_type(path)[0] if mimetype is None: content = text mimetype = 'text/plain' elif mimetype == 'text/html': content = ToUnicode(text) if self._supports_templates: content = Handlebar(content) elif (mimetype.startswith('text/') or mimetype in ('application/javascript', 'application/json')): content = ToUnicode(text) else: content = text return ContentAndType(content, mimetype)
def Get(self): for path, future in self._fetches: try: result = future.Get() except Exception as e: raise FileSystemError('Error fetching %s for Get: %s' % (path, traceback.format_exc())) if result.status_code == 404: raise FileNotFoundError( 'Got 404 when fetching %s for Get, content %s' % (path, result.content)) if result.status_code != 200: raise FileSystemError( 'Got %s when fetching %s for Get, content %s' % (result.status_code, path, result.content)) if path.endswith('/'): self._value[path] = self._ListDir(result.content) elif not self._binary: self._value[path] = ToUnicode(result.content) else: self._value[path] = result.content if self._error is not None: raise self._error return self._value
def _ReadFile(self, filename, binary): try: mode = 'rb' if binary else 'r' with open(os.path.join(self._base_path, filename), mode) as f: contents = f.read() if binary: return contents return ToUnicode(contents) except IOError as e: raise FileNotFoundError('Read failed for %s: %s' % (filename, e))
def Get(self): tarball_result = self._tarball.Get() if tarball_result.status_code != 200: raise RietveldPatcherError( 'Failed to download tarball for issue %s patchset %s. Status: %s' % (self._issue, self._patchset, tarball_result.status_code)) try: tar = tarfile.open(fileobj=StringIO(tarball_result.content)) except tarfile.TarError as e: raise RietveldPatcherError( 'Error loading tarball for issue %s patchset %s.' % (self._issue, self._patchset)) self._value = {} for path in self._files: if self._base_path: tar_path = 'b/%s/%s' % (self._base_path, path) else: tar_path = 'b/%s' % path patched_file = None try: patched_file = tar.extractfile(tar_path) data = patched_file.read() except tarfile.TarError as e: # Show appropriate error message in the unlikely case that the tarball # is corrupted. raise RietveldPatcherError( 'Error extracting tarball for issue %s patchset %s file %s.' % (self._issue, self._patchset, tar_path)) except KeyError as e: raise FileNotFoundError( 'File %s not found in the tarball for issue %s patchset %s' % (tar_path, self._issue, self._patchset)) finally: if patched_file: patched_file.close() if self._binary: self._value[path] = data else: self._value[path] = ToUnicode(data) return self._value
def Get(self): for path, future in self._fetches: try: result = future.Get() except Exception as e: raise FileNotFoundError('Error when fetching %s for Get: %s' % (path, e)) if result.status_code == 404: raise FileNotFoundError('Got 404 when fetching %s for Get' % path) elif path.endswith('/'): self._value[path] = self._ListDir(result.content) elif not self._binary: self._value[path] = ToUnicode(result.content) else: self._value[path] = result.content if self._error is not None: raise self._error return self._value
def _HandleBinary(data, binary): return data if binary else ToUnicode(data)