Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(514)

Side by Side Diff: third_party/polymer/v1_0/find_unused_elements.py

Issue 2920003002: Polymer: Update find_unused_elements.py (Closed)
Patch Set: Remove tempfile Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/polymer/v1_0/reproduce.sh » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
14 15
16 _HERE_PATH = os.path.dirname(__file__)
17 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..'))
18 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node'))
19 import node
20 import node_modules
15 21
16 class UnusedElementsDetector(object): 22 class UnusedElementsDetector(object):
17 """Finds unused Polymer elements.""" 23 """Finds unused Polymer elements."""
18 24
19 # Unused elements to ignore because we plan to use them soon. 25 # Unused elements to ignore because we plan to use them soon.
20 __WHITELIST = ( 26 __WHITELIST = (
21 # Necessary for closure. 27 # Necessary for closure.
22 'polymer-externs', 28 'polymer-externs',
23 ) 29 )
24 30
(...skipping 26 matching lines...) Expand all
51 57
52 Returns: 58 Returns:
53 A string consisting of the minified file contents with comments and grit 59 A string consisting of the minified file contents with comments and grit
54 directives removed. 60 directives removed.
55 """ 61 """
56 with open(filename) as f: 62 with open(filename) as f:
57 text = f.read() 63 text = f.read()
58 text = re.sub('<if .*?>', '', text, flags=re.IGNORECASE) 64 text = re.sub('<if .*?>', '', text, flags=re.IGNORECASE)
59 text = re.sub('</if>', '', text, flags=re.IGNORECASE) 65 text = re.sub('</if>', '', text, flags=re.IGNORECASE)
60 66
61 proc = subprocess.Popen(['uglifyjs', filename], stdout=subprocess.PIPE) 67 return node.RunNode([node_modules.PathToUglify(), filename])
62 return proc.stdout.read()
63 68
64 @staticmethod 69 @staticmethod
65 def __StripComments(filename): 70 def __StripComments(filename):
66 """Returns the contents of a JavaScript or HTML file with comments removed. 71 """Returns the contents of a JavaScript or HTML file with comments removed.
67 72
68 Args: 73 Args:
69 filename: The name of the file to read. 74 filename: The name of the file to read.
70 75
71 Returns: 76 Returns:
72 A string consisting of the file contents processed via 77 A string consisting of the file contents processed via
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 for (dirpath, _, filenames) in os.walk(path): 131 for (dirpath, _, filenames) in os.walk(path):
127 # Ignore the element's own files. 132 # Ignore the element's own files.
128 if dirpath.startswith(os.path.join( 133 if dirpath.startswith(os.path.join(
129 self.__COMPONENTS_DIR, element_dir)): 134 self.__COMPONENTS_DIR, element_dir)):
130 continue 135 continue
131 136
132 for filename in filenames: 137 for filename in filenames:
133 if not filename.endswith('.html') and not filename.endswith('.js'): 138 if not filename.endswith('.html') and not filename.endswith('.js'):
134 continue 139 continue
135 140
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: 141 with open(os.path.join(dirpath, filename)) as f:
142 text = f.read() 142 text = f.read()
143 if not re.search('/%s/' % element_dir, text): 143 if not re.search('/%s/' % element_dir, text):
144 continue 144 continue
145 145
146 # Check the file again, ignoring comments (e.g. example imports and 146 # Check the file again, ignoring comments (e.g. example imports and
147 # scripts). 147 # scripts).
148 if re.search('/%s' % element_dir, 148 if re.search('/%s' % element_dir,
149 self.__StripComments( 149 self.__StripComments(
150 os.path.join(dirpath, filename))): 150 os.path.join(dirpath, filename))):
151 return True 151 return True
152 return False 152 return False
153 153
154 154
155 if __name__ == '__main__': 155 if __name__ == '__main__':
156 UnusedElementsDetector().Run() 156 UnusedElementsDetector().Run()
OLDNEW
« no previous file with comments | « no previous file | third_party/polymer/v1_0/reproduce.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698