Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 from collections import defaultdict | 5 from collections import defaultdict |
| 6 import os | 6 import os |
| 7 import re | 7 import re |
| 8 import subprocess | 8 import subprocess |
| 9 import sys | 9 import sys |
| 10 import tempfile | 10 import tempfile |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 file = os.path.abspath(file) | 24 file = os.path.abspath(file) |
| 25 self._cache[file] = self._cache[file] or open(file, "r").read() | 25 self._cache[file] = self._cache[file] or open(file, "r").read() |
| 26 return self._cache[file] | 26 return self._cache[file] |
| 27 | 27 |
| 28 @staticmethod | 28 @staticmethod |
| 29 def read(file): | 29 def read(file): |
| 30 return FileCache()._read(file) | 30 return FileCache()._read(file) |
| 31 | 31 |
| 32 | 32 |
| 33 class Flattener(object): | 33 class Flattener(object): |
| 34 _IF_TAGS_REG = "</?if[^>]*?>" | |
| 34 _INCLUDE_REG = "<include[^>]+src=['\"]([^>]*)['\"]>" | 35 _INCLUDE_REG = "<include[^>]+src=['\"]([^>]*)['\"]>" |
| 35 | 36 |
| 36 def __init__(self, file): | 37 def __init__(self, file): |
| 37 self.index = 0 | 38 self.index = 0 |
| 38 self.lines = self._get_file(file) | 39 self.lines = self._get_file(file) |
| 39 | 40 |
| 40 while self.index < len(self.lines): | 41 while self.index < len(self.lines): |
| 41 current_line = self.lines[self.index] | 42 current_line = self.lines[self.index] |
| 42 match = re.search(self._INCLUDE_REG, current_line[2]) | 43 match = re.search(self._INCLUDE_REG, current_line[2]) |
| 43 if match: | 44 if match: |
| 44 file_dir = os.path.dirname(current_line[0]) | 45 file_dir = os.path.dirname(current_line[0]) |
| 45 self._inline_file(os.path.join(file_dir, match.group(1))) | 46 self._inline_file(os.path.join(file_dir, match.group(1))) |
| 46 else: | 47 else: |
| 47 self.index += 1 | 48 self.index += 1 |
| 48 | 49 |
| 50 self.lines = map(self._remove_if_operators, self.lines) | |
|
Dan Beam
2014/08/01 20:39:54
for line in self.lines:
line[2] = re.sub(self._I
Vitaly Pavlenko
2014/08/01 20:52:37
line[2] = re.sub(self._IF_TAGS_REG, "", line[2])
T
Dan Beam
2014/08/01 20:59:53
^ that's fine
| |
| 51 | |
| 49 self.contents = "\n".join(l[2] for l in self.lines) | 52 self.contents = "\n".join(l[2] for l in self.lines) |
| 50 | 53 |
| 54 def _remove_if_operators(self, line): | |
| 55 # Replace every occurrence of tags like <if expr="..."> and </if> | |
| 56 # with an empty string. | |
| 57 file, lnum, contents = line | |
| 58 contents = re.sub(self._IF_TAGS_REG, '', contents) | |
| 59 return (file, lnum, contents) | |
| 60 | |
| 51 # Returns a list of tuples in the format: (file, line number, line contents). | 61 # Returns a list of tuples in the format: (file, line number, line contents). |
| 52 def _get_file(self, file): | 62 def _get_file(self, file): |
| 53 lines = FileCache.read(file).splitlines() | 63 lines = FileCache.read(file).splitlines() |
| 54 return [(file, lnum + 1, line) for lnum, line in enumerate(lines)] | 64 return [(file, lnum + 1, line) for lnum, line in enumerate(lines)] |
| 55 | 65 |
| 56 def _inline_file(self, file): | 66 def _inline_file(self, file): |
| 57 lines = self._get_file(file) | 67 lines = self._get_file(file) |
| 58 self.lines = self.lines[:self.index] + lines + self.lines[self.index + 1:] | 68 self.lines = self.lines[:self.index] + lines + self.lines[self.index + 1:] |
| 59 | 69 |
| 60 def get_file_from_line(self, line_number): | 70 def get_file_from_line(self, line_number): |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 output = self._format_errors(map(self._fix_up_error, errors)) | 226 output = self._format_errors(map(self._fix_up_error, errors)) |
| 217 | 227 |
| 218 if runner_cmd.returncode: | 228 if runner_cmd.returncode: |
| 219 self._error("Error in: " + file + ("\n" + output if output else "")) | 229 self._error("Error in: " + file + ("\n" + output if output else "")) |
| 220 elif output: | 230 elif output: |
| 221 self._debug("Output: " + output) | 231 self._debug("Output: " + output) |
| 222 | 232 |
| 223 self._clean_up() | 233 self._clean_up() |
| 224 | 234 |
| 225 return runner_cmd.returncode, output | 235 return runner_cmd.returncode, output |
| OLD | NEW |