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

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

Issue 2920003002: Polymer: Update find_unused_elements.py (Closed)
Patch Set: Rebase 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
15 import tempfile
michaelpg 2017/06/06 23:34:40 remove
dpapad 2017/06/07 00:16:13 Done.
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'))
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
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 return node.RunNode([node_modules.PathToUglify(), filename])
62 return proc.stdout.read()
63 69
64 @staticmethod 70 @staticmethod
65 def __StripComments(filename): 71 def __StripComments(filename):
66 """Returns the contents of a JavaScript or HTML file with comments removed. 72 """Returns the contents of a JavaScript or HTML file with comments removed.
67 73
68 Args: 74 Args:
69 filename: The name of the file to read. 75 filename: The name of the file to read.
70 76
71 Returns: 77 Returns:
72 A string consisting of the file contents processed via 78 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): 132 for (dirpath, _, filenames) in os.walk(path):
127 # Ignore the element's own files. 133 # Ignore the element's own files.
128 if dirpath.startswith(os.path.join( 134 if dirpath.startswith(os.path.join(
129 self.__COMPONENTS_DIR, element_dir)): 135 self.__COMPONENTS_DIR, element_dir)):
130 continue 136 continue
131 137
132 for filename in filenames: 138 for filename in filenames:
133 if not filename.endswith('.html') and not filename.endswith('.js'): 139 if not filename.endswith('.html') and not filename.endswith('.js'):
134 continue 140 continue
135 141
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: 142 with open(os.path.join(dirpath, filename)) as f:
142 text = f.read() 143 text = f.read()
143 if not re.search('/%s/' % element_dir, text): 144 if not re.search('/%s/' % element_dir, text):
144 continue 145 continue
145 146
146 # Check the file again, ignoring comments (e.g. example imports and 147 # Check the file again, ignoring comments (e.g. example imports and
147 # scripts). 148 # scripts).
148 if re.search('/%s' % element_dir, 149 if re.search('/%s' % element_dir,
149 self.__StripComments( 150 self.__StripComments(
150 os.path.join(dirpath, filename))): 151 os.path.join(dirpath, filename))):
151 return True 152 return True
152 return False 153 return False
153 154
154 155
155 if __name__ == '__main__': 156 if __name__ == '__main__':
156 UnusedElementsDetector().Run() 157 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