Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python2 | |
| 2 | |
| 3 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 8 # Use of this source code is governed by a BSD-style license that can be | |
| 9 # found in the LICENSE file. | |
| 10 | |
| 11 """Identifies Polymer elements that downloaded but not used by Chrome. | |
| 12 | |
| 13 Only finds "first-order" unused elements; re-run after removing unused elements | |
| 14 to check if other elements have become unused. | |
| 15 """ | |
| 16 | |
| 17 import os | |
| 18 import pyparsing | |
|
stevenjb
2016/04/29 20:34:56
Used?
michaelpg
2016/05/02 19:31:45
Done.
| |
| 19 import re | |
| 20 import subprocess | |
| 21 import sys | |
|
stevenjb
2016/04/29 20:34:55
Used?
michaelpg
2016/05/02 19:31:45
Done.
| |
| 22 | |
| 23 | |
| 24 class UnusedElementsDetector: | |
|
Dan Beam
2016/05/03 19:22:08
this should probably inherit from (object)
michaelpg
2016/05/03 21:29:19
Done.
| |
| 25 """Finds unused Polymer elements.""" | |
| 26 | |
| 27 # Unused elements to ignore because we plan to use them soon. | |
| 28 __whitelist = ( | |
|
Dan Beam
2016/05/03 19:22:08
nit: I think it's more common to use 4\s indent in
Dan Beam
2016/05/03 19:22:08
nit: __WHITELIST (Class contant https://google.git
michaelpg
2016/05/03 21:29:19
Done.
michaelpg
2016/05/03 21:29:19
the examples in PEP-8 use 4 spaces (as with other
| |
| 29 # TODO(dschuyler): Use element or remove from whitelist. | |
| 30 'carbon-route', | |
| 31 # TODO(tsergeant): Use element or remove from whitelist. | |
| 32 'iron-scroll-threshold', | |
| 33 # Necessary for closure. | |
| 34 'polymer-externs', | |
| 35 ) | |
| 36 | |
| 37 @staticmethod | |
| 38 def __StripHtmlComments(filename): | |
| 39 """Returns the contents of an HTML file with <!-- --> comments stripped. | |
| 40 | |
| 41 Not a real parser. | |
| 42 | |
| 43 Args: | |
| 44 filename: The name of the file to read. | |
| 45 | |
| 46 Returns: | |
| 47 A string consisting of the file contents with comments removed. | |
| 48 """ | |
| 49 text = open(filename).read() | |
| 50 return re.sub('<!--.*?-->', '', text, flags=re.MULTILINE|re.DOTALL) | |
|
Dan Beam
2016/05/03 19:22:08
nit: flags=re.MULTILINE | re.DOTALL)
michaelpg
2016/05/03 21:29:19
Done. Looked weird to me inside a param=value expr
| |
| 51 | |
| 52 @staticmethod | |
| 53 def __StripJsComments(filename): | |
| 54 """Returns the minified contents of a JavaScript file with comments and | |
| 55 grit directives removed. | |
| 56 | |
| 57 Args: | |
| 58 filename: The name of the file to read. | |
| 59 | |
| 60 Returns: | |
| 61 A string consisting of the minified file contents with comments and grit | |
| 62 directives removed. | |
| 63 """ | |
| 64 text = open(filename).read() | |
| 65 text = re.sub('<if .*?>', '', text) | |
|
Dan Beam
2016/05/03 19:22:07
nit: case-insensitive maybe?
michaelpg
2016/05/03 21:29:19
Done.
| |
| 66 text = re.sub('</if>', '', text) | |
| 67 | |
| 68 proc = subprocess.Popen(['uglifyjs', filename], stdout=subprocess.PIPE) | |
| 69 return proc.stdout.read() | |
| 70 | |
| 71 @staticmethod | |
| 72 def __StripComments(filename): | |
| 73 """Returns the contents of a JavaScript or HTML file with comments removed. | |
| 74 | |
| 75 Args: | |
| 76 filename: The name of the file to read. | |
| 77 | |
| 78 Returns: | |
| 79 A string consisting of the file contents processed via | |
| 80 __StripHtmlComments or __StripJsComments. | |
| 81 """ | |
| 82 if filename.endswith('.html'): | |
| 83 text = UnusedElementsDetector.__StripHtmlComments(filename) | |
| 84 elif filename.endswith('.js'): | |
| 85 text = UnusedElementsDetector.__StripJsComments(filename) | |
| 86 else: | |
| 87 assert False, 'Invalid filename: %s' % filename | |
| 88 return text | |
| 89 | |
| 90 @staticmethod | |
| 91 def Run(): | |
| 92 """Finds unused Polymer elements and prints a summary.""" | |
| 93 proc = subprocess.Popen( | |
| 94 ['git', 'rev-parse', '--show-toplevel'], | |
| 95 stdout=subprocess.PIPE) | |
| 96 src_dir = proc.stdout.read().strip() | |
| 97 polymer_dir = os.path.dirname(os.path.realpath(__file__)) | |
| 98 components_dir = os.path.join(polymer_dir, 'components-chromium') | |
| 99 | |
| 100 elements = [] | |
| 101 for name in os.listdir(components_dir): | |
| 102 path = os.path.join(components_dir, name) | |
| 103 if os.path.isdir(path): | |
| 104 elements.append(name) | |
| 105 | |
| 106 unused_elements = [] | |
| 107 for element in elements: | |
| 108 if element in UnusedElementsDetector.__whitelist: | |
| 109 continue | |
| 110 | |
| 111 relevant_src_dirs = ( | |
| 112 os.path.join(src_dir, 'chrome'), | |
| 113 os.path.join(src_dir, 'ui'), | |
| 114 os.path.join(src_dir, 'components'), | |
| 115 components_dir | |
| 116 ) | |
| 117 used = False | |
| 118 for path in relevant_src_dirs: | |
| 119 # Find an import or script referencing the tag's directory. | |
| 120 for (dirpath, _, filenames) in os.walk(path): | |
| 121 # Ignore the element's own files. | |
| 122 if dirpath.startswith(os.path.join(components_dir, element)): | |
| 123 continue | |
| 124 | |
| 125 for filename in [f for f in filenames if f.endswith('.html') or | |
| 126 f.endswith('.js')]: | |
| 127 # Skip generated files that may include the element source. | |
| 128 if filename in ('crisper.js', 'vulcanized.html'): | |
| 129 continue | |
| 130 | |
| 131 text = open(os.path.join(dirpath, filename)).read() | |
| 132 if not re.search('/%s/' % element, text): | |
| 133 continue | |
| 134 | |
| 135 # Check the file again, ignoring comments (e.g. example imports and | |
| 136 # scripts). | |
| 137 if re.search( | |
| 138 '/%s' % element, | |
|
stevenjb
2016/04/29 20:34:56
My python formatting fu is weak, but I suspect thi
michaelpg
2016/05/02 19:31:45
Done (both are valid, but your way looks better)
| |
| 139 UnusedElementsDetector.__StripComments( | |
| 140 os.path.join(dirpath, filename))): | |
| 141 used = True | |
| 142 break | |
| 143 if used: | |
| 144 break | |
| 145 if used: | |
| 146 break | |
|
stevenjb
2016/04/29 20:34:55
This would be more readable if the entire for loop
michaelpg
2016/05/02 19:31:45
Meant to do that, forgot, done now. Thanks!
| |
| 147 if not used: | |
| 148 unused_elements.append(element) | |
| 149 | |
| 150 if len(unused_elements): | |
| 151 print 'Found unused elements: %s\nRemove from bower.json and re-run ' \ | |
| 152 'reproduce.sh, or add to whitelist in %s' % ( | |
| 153 ', '.join(unused_elements), os.path.basename(__file__)) | |
| 154 | |
| 155 | |
| 156 UnusedElementsDetector.Run() | |
|
Dan Beam
2016/05/03 19:22:07
if __name__ == '__main__':
UnusedElementDetector
michaelpg
2016/05/03 21:29:19
Done.
| |
| OLD | NEW |