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 |
| 11 import os | 11 import os |
| 12 import re | 12 import re |
| 13 import subprocess | 13 import subprocess |
| 14 import sys | |
| 15 import tempfile | |
| 14 | 16 |
| 17 _HERE_PATH = os.path.dirname(__file__) | |
| 18 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) | |
| 19 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node')) | |
|
michaelpg
2017/06/05 19:22:36
remove uglify check in reproduce.sh
dpapad
2017/06/05 19:42:02
Done.
| |
| 20 import node | |
| 21 import node_modules | |
| 15 | 22 |
| 16 class UnusedElementsDetector(object): | 23 class UnusedElementsDetector(object): |
| 17 """Finds unused Polymer elements.""" | 24 """Finds unused Polymer elements.""" |
| 18 | 25 |
| 19 # Unused elements to ignore because we plan to use them soon. | 26 # Unused elements to ignore because we plan to use them soon. |
| 20 __WHITELIST = ( | 27 __WHITELIST = ( |
| 21 # Necessary for closure. | 28 # Necessary for closure. |
| 22 'polymer-externs', | 29 'polymer-externs', |
| 23 ) | 30 ) |
| 24 | 31 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 51 | 58 |
| 52 Returns: | 59 Returns: |
| 53 A string consisting of the minified file contents with comments and grit | 60 A string consisting of the minified file contents with comments and grit |
| 54 directives removed. | 61 directives removed. |
| 55 """ | 62 """ |
| 56 with open(filename) as f: | 63 with open(filename) as f: |
| 57 text = f.read() | 64 text = f.read() |
| 58 text = re.sub('<if .*?>', '', text, flags=re.IGNORECASE) | 65 text = re.sub('<if .*?>', '', text, flags=re.IGNORECASE) |
| 59 text = re.sub('</if>', '', text, flags=re.IGNORECASE) | 66 text = re.sub('</if>', '', text, flags=re.IGNORECASE) |
| 60 | 67 |
| 61 proc = subprocess.Popen(['uglifyjs', filename], stdout=subprocess.PIPE) | 68 # Need to write the output to a temporary file to avoid |
|
Dan Beam
2017/06/02 21:49:08
can we change to subprocess2 and use stderr=subpro
dpapad
2017/06/05 19:22:09
We probably could (have not tried yet), but import
dpapad
2017/06/06 21:39:55
This is not necessary anymore. I rebased this CL a
| |
| 62 return proc.stdout.read() | 69 # https://github.com/mishoo/UglifyJS2/issues/641 which has been fixed after |
| 70 # version 2.4.21. | |
| 71 # TODO(dpapad): Chromium currently uses 2.4.10, update to latest version. | |
| 72 with tempfile.NamedTemporaryFile(mode='wt+') as tmp: | |
| 73 node.RunNode([ | |
| 74 node_modules.PathToUglifyJs(), filename, '--output', tmp.name]) | |
| 75 output = tmp.read() | |
| 76 | |
| 77 return output | |
| 63 | 78 |
| 64 @staticmethod | 79 @staticmethod |
| 65 def __StripComments(filename): | 80 def __StripComments(filename): |
| 66 """Returns the contents of a JavaScript or HTML file with comments removed. | 81 """Returns the contents of a JavaScript or HTML file with comments removed. |
| 67 | 82 |
| 68 Args: | 83 Args: |
| 69 filename: The name of the file to read. | 84 filename: The name of the file to read. |
| 70 | 85 |
| 71 Returns: | 86 Returns: |
| 72 A string consisting of the file contents processed via | 87 A string consisting of the file contents processed via |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 for (dirpath, _, filenames) in os.walk(path): | 141 for (dirpath, _, filenames) in os.walk(path): |
| 127 # Ignore the element's own files. | 142 # Ignore the element's own files. |
| 128 if dirpath.startswith(os.path.join( | 143 if dirpath.startswith(os.path.join( |
| 129 self.__COMPONENTS_DIR, element_dir)): | 144 self.__COMPONENTS_DIR, element_dir)): |
| 130 continue | 145 continue |
| 131 | 146 |
| 132 for filename in filenames: | 147 for filename in filenames: |
| 133 if not filename.endswith('.html') and not filename.endswith('.js'): | 148 if not filename.endswith('.html') and not filename.endswith('.js'): |
| 134 continue | 149 continue |
| 135 | 150 |
| 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: | 151 with open(os.path.join(dirpath, filename)) as f: |
| 142 text = f.read() | 152 text = f.read() |
| 143 if not re.search('/%s/' % element_dir, text): | 153 if not re.search('/%s/' % element_dir, text): |
| 144 continue | 154 continue |
| 145 | 155 |
| 146 # Check the file again, ignoring comments (e.g. example imports and | 156 # Check the file again, ignoring comments (e.g. example imports and |
| 147 # scripts). | 157 # scripts). |
| 148 if re.search('/%s' % element_dir, | 158 if re.search('/%s' % element_dir, |
| 149 self.__StripComments( | 159 self.__StripComments( |
| 150 os.path.join(dirpath, filename))): | 160 os.path.join(dirpath, filename))): |
| 151 return True | 161 return True |
| 152 return False | 162 return False |
| 153 | 163 |
| 154 | 164 |
| 155 if __name__ == '__main__': | 165 if __name__ == '__main__': |
| 156 UnusedElementsDetector().Run() | 166 UnusedElementsDetector().Run() |
| OLD | NEW |