Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 """Identifies Polymer elements that downloaded but not used by Chrome. | 5 """Identifies Polymer elements that downloaded but not used by Chrome. |
| 6 | 6 |
| 7 Only finds "first-order" unused elements; re-run after removing unused elements | 7 Only finds "first-order" unused elements; re-run after removing unused elements |
| 8 to check if other elements have become unused. | 8 to check if other elements have become unused. |
| 9 """ | 9 """ |
| 10 | 10 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 51 | 51 |
| 52 Returns: | 52 Returns: |
| 53 A string consisting of the minified file contents with comments and grit | 53 A string consisting of the minified file contents with comments and grit |
| 54 directives removed. | 54 directives removed. |
| 55 """ | 55 """ |
| 56 with open(filename) as f: | 56 with open(filename) as f: |
| 57 text = f.read() | 57 text = f.read() |
| 58 text = re.sub('<if .*?>', '', text, flags=re.IGNORECASE) | 58 text = re.sub('<if .*?>', '', text, flags=re.IGNORECASE) |
| 59 text = re.sub('</if>', '', text, flags=re.IGNORECASE) | 59 text = re.sub('</if>', '', text, flags=re.IGNORECASE) |
| 60 | 60 |
| 61 proc = subprocess.Popen(['uglifyjs', filename], stdout=subprocess.PIPE) | 61 proc = subprocess.Popen(['uglifyjs', filename], stdout=subprocess.PIPE) |
|
dpapad
2017/06/02 18:19:26
Actually there is more issues with this script. It
dpapad
2017/06/02 19:01:53
Addressed in latest patch.
| |
| 62 return proc.stdout.read() | 62 return proc.stdout.read() |
| 63 | 63 |
| 64 @staticmethod | 64 @staticmethod |
| 65 def __StripComments(filename): | 65 def __StripComments(filename): |
| 66 """Returns the contents of a JavaScript or HTML file with comments removed. | 66 """Returns the contents of a JavaScript or HTML file with comments removed. |
| 67 | 67 |
| 68 Args: | 68 Args: |
| 69 filename: The name of the file to read. | 69 filename: The name of the file to read. |
| 70 | 70 |
| 71 Returns: | 71 Returns: |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 for (dirpath, _, filenames) in os.walk(path): | 126 for (dirpath, _, filenames) in os.walk(path): |
| 127 # Ignore the element's own files. | 127 # Ignore the element's own files. |
| 128 if dirpath.startswith(os.path.join( | 128 if dirpath.startswith(os.path.join( |
| 129 self.__COMPONENTS_DIR, element_dir)): | 129 self.__COMPONENTS_DIR, element_dir)): |
| 130 continue | 130 continue |
| 131 | 131 |
| 132 for filename in filenames: | 132 for filename in filenames: |
| 133 if not filename.endswith('.html') and not filename.endswith('.js'): | 133 if not filename.endswith('.html') and not filename.endswith('.js'): |
| 134 continue | 134 continue |
| 135 | 135 |
| 136 # Skip generated files that may include the element source. | |
| 137 if filename in ('crisper.js', 'vulcanized.html', | |
| 138 'app.crisper.js', 'app.vulcanized.html'): | |
| 139 continue | |
| 140 | |
| 141 with open(os.path.join(dirpath, filename)) as f: | 136 with open(os.path.join(dirpath, filename)) as f: |
| 142 text = f.read() | 137 text = f.read() |
| 143 if not re.search('/%s/' % element_dir, text): | 138 if not re.search('/%s/' % element_dir, text): |
| 144 continue | 139 continue |
| 145 | 140 |
| 146 # Check the file again, ignoring comments (e.g. example imports and | 141 # Check the file again, ignoring comments (e.g. example imports and |
| 147 # scripts). | 142 # scripts). |
| 148 if re.search('/%s' % element_dir, | 143 if re.search('/%s' % element_dir, |
| 149 self.__StripComments( | 144 self.__StripComments( |
| 150 os.path.join(dirpath, filename))): | 145 os.path.join(dirpath, filename))): |
| 151 return True | 146 return True |
| 152 return False | 147 return False |
| 153 | 148 |
| 154 | 149 |
| 155 if __name__ == '__main__': | 150 if __name__ == '__main__': |
| 156 UnusedElementsDetector().Run() | 151 UnusedElementsDetector().Run() |
| OLD | NEW |