OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 '''Produces various output formats from a set of JavaScript files with | 7 '''Produces various output formats from a set of JavaScript files with |
8 closure style require/provide calls. | 8 closure style require/provide calls. |
9 | 9 |
10 Scans one or more directory trees for JavaScript files. Then, from a | 10 Scans one or more directory trees for JavaScript files. Then, from a |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 Returns: | 137 Returns: |
138 str: The corresponding output path. | 138 str: The corresponding output path. |
139 ''' | 139 ''' |
140 for in_prefix, out_prefix in self._prefix_map: | 140 for in_prefix, out_prefix in self._prefix_map: |
141 if in_path.startswith(in_prefix): | 141 if in_path.startswith(in_prefix): |
142 return os.path.join(out_prefix, in_path[len(in_prefix):]) | 142 return os.path.join(out_prefix, in_path[len(in_prefix):]) |
143 return in_path | 143 return in_path |
144 | 144 |
145 | 145 |
146 def ReadSources(roots=[], source_files=[], need_source_text=False, | 146 def ReadSources(roots=[], source_files=[], need_source_text=False, |
147 path_rewriter=PathRewriter()): | 147 path_rewriter=PathRewriter(), exclude=[]): |
148 '''Reads all source specified on the command line, including sources | 148 '''Reads all source specified on the command line, including sources |
149 included by --root options. | 149 included by --root options. |
150 ''' | 150 ''' |
151 | 151 |
152 def EnsureSourceLoaded(in_path, sources): | 152 def EnsureSourceLoaded(in_path, sources): |
153 if in_path not in sources: | 153 if in_path not in sources: |
154 out_path = path_rewriter.RewritePath(in_path) | 154 out_path = path_rewriter.RewritePath(in_path) |
155 sources[in_path] = SourceWithPaths(source.GetFileContents(in_path), | 155 sources[in_path] = SourceWithPaths(source.GetFileContents(in_path), |
156 in_path, out_path) | 156 in_path, out_path) |
157 | 157 |
158 # Only read the actual source file if we will do a dependency analysis or | 158 # Only read the actual source file if we will do a dependency analysis or |
159 # the caller asks for it. | 159 # the caller asks for it. |
160 need_source_text = need_source_text or len(roots) > 0 | 160 need_source_text = need_source_text or len(roots) > 0 |
161 sources = {} | 161 sources = {} |
162 for root in roots: | 162 for root in roots: |
163 for name in treescan.ScanTreeForJsFiles(root): | 163 for name in treescan.ScanTreeForJsFiles(root): |
| 164 if any((r.search(name) for r in exclude)): |
| 165 continue |
164 EnsureSourceLoaded(name, sources) | 166 EnsureSourceLoaded(name, sources) |
165 for path in source_files: | 167 for path in source_files: |
166 if need_source_text: | 168 if need_source_text: |
167 EnsureSourceLoaded(path, sources) | 169 EnsureSourceLoaded(path, sources) |
168 else: | 170 else: |
169 # Just add an empty representation of the source. | 171 # Just add an empty representation of the source. |
170 sources[path] = SourceWithPaths( | 172 sources[path] = SourceWithPaths( |
171 '', path, path_rewriter.RewritePath(path)) | 173 '', path, path_rewriter.RewritePath(path)) |
172 return sources | 174 return sources |
173 | 175 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 'specifying that a file whose (relative) path ' + | 296 'specifying that a file whose (relative) path ' + |
295 'name starts with the first prefix should have ' + | 297 'name starts with the first prefix should have ' + |
296 'that prefix replaced by the second prefix to ' + | 298 'that prefix replaced by the second prefix to ' + |
297 'form a path relative to the output directory.')) | 299 'form a path relative to the output directory.')) |
298 parser.add_option('-m', '--mode', type='choice', action='store', | 300 parser.add_option('-m', '--mode', type='choice', action='store', |
299 choices=['list', 'html', 'bundle', | 301 choices=['list', 'html', 'bundle', |
300 'compressed_bundle', 'copy'], | 302 'compressed_bundle', 'copy'], |
301 default='list', metavar='MODE', | 303 default='list', metavar='MODE', |
302 help=("Otput mode. One of 'list', 'html', 'bundle', " + | 304 help=("Otput mode. One of 'list', 'html', 'bundle', " + |
303 "'compressed_bundle' or 'copy'.")) | 305 "'compressed_bundle' or 'copy'.")) |
| 306 parser.add_option('-x', '--exclude', action='append', default=[], |
| 307 help=('Exclude files whose full path contains a match for ' |
| 308 'the given regular expression. Does not apply to ' |
| 309 'filenames given as arguments.')) |
304 return parser | 310 return parser |
305 | 311 |
306 | 312 |
307 def main(): | 313 def main(): |
308 options, args = CreateOptionParser().parse_args() | 314 options, args = CreateOptionParser().parse_args() |
309 if len(args) < 1: | 315 if len(args) < 1: |
310 Die('At least one top-level source file must be specified.') | 316 Die('At least one top-level source file must be specified.') |
311 will_output_source_text = options.mode in ('bundle', 'compressed_bundle') | 317 will_output_source_text = options.mode in ('bundle', 'compressed_bundle') |
312 path_rewriter = PathRewriter(options.prefix_map) | 318 path_rewriter = PathRewriter(options.prefix_map) |
| 319 exclude = [re.compile(r) for r in options.exclude] |
313 sources = ReadSources(options.roots, args, will_output_source_text, | 320 sources = ReadSources(options.roots, args, will_output_source_text, |
314 path_rewriter) | 321 path_rewriter, exclude) |
315 if will_output_source_text: | 322 if will_output_source_text: |
316 _MarkAsCompiled(sources) | 323 _MarkAsCompiled(sources) |
317 bundle = Bundle() | 324 bundle = Bundle() |
318 if len(options.roots) > 0: | 325 if len(options.roots) > 0: |
319 CalcDeps(bundle, sources, args) | 326 CalcDeps(bundle, sources, args) |
320 bundle.Add((sources[name] for name in args)) | 327 bundle.Add((sources[name] for name in args)) |
321 if options.mode == 'copy': | 328 if options.mode == 'copy': |
322 if options.dest_dir is None: | 329 if options.dest_dir is None: |
323 Die('Must specify --dest_dir when copying.') | 330 Die('Must specify --dest_dir when copying.') |
324 LinkOrCopyFiles(bundle.GetSources(), options.dest_dir) | 331 LinkOrCopyFiles(bundle.GetSources(), options.dest_dir) |
325 else: | 332 else: |
326 if options.output_file: | 333 if options.output_file: |
327 out_file = open(options.output_file, 'w') | 334 out_file = open(options.output_file, 'w') |
328 else: | 335 else: |
329 out_file = sys.stdout | 336 out_file = sys.stdout |
330 try: | 337 try: |
331 WriteOutput(bundle, options.mode, out_file, options.dest_dir) | 338 WriteOutput(bundle, options.mode, out_file, options.dest_dir) |
332 finally: | 339 finally: |
333 if options.output_file: | 340 if options.output_file: |
334 out_file.close() | 341 out_file.close() |
335 | 342 |
336 | 343 |
337 if __name__ == '__main__': | 344 if __name__ == '__main__': |
338 main() | 345 main() |
OLD | NEW |