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 ''' Generates a deps.js file based on an input list of javascript files using | 7 ''' Generates a deps.js file based on an input list of javascript files using |
8 Closure style provide/require calls. | 8 Closure style provide/require calls. |
9 ''' | 9 ''' |
10 | 10 |
11 import optparse | 11 import optparse |
12 import os | 12 import os |
13 import sys | 13 import sys |
14 | 14 |
15 from jsbundler import PathRewriter | 15 from jsbundler import PathRewriter |
16 | 16 |
17 _SCRIPT_DIR = os.path.realpath(os.path.dirname(__file__)) | 17 _SCRIPT_DIR = os.path.realpath(os.path.dirname(__file__)) |
18 _CHROME_SOURCE = os.path.realpath( | 18 _CHROME_SOURCE = os.path.realpath( |
19 os.path.join(_SCRIPT_DIR, *[os.path.pardir] * 6)) | 19 os.path.join(_SCRIPT_DIR, *[os.path.pardir] * 6)) |
20 sys.path.insert(0, os.path.join( | 20 sys.path.insert(0, os.path.join( |
21 _CHROME_SOURCE, ('chrome/third_party/chromevox/third_party/' + | 21 _CHROME_SOURCE, ('chrome/third_party/chromevox/third_party/' + |
22 'closure-library/closure/bin/build'))) | 22 'closure-library/closure/bin/build'))) |
23 import source | 23 import source |
24 | 24 |
25 | 25 |
| 26 def _HasSameContent(filename, content): |
| 27 '''Returns true if the given file is readable and has the given content.''' |
| 28 try: |
| 29 with open(filename) as file: |
| 30 return file.read() == content |
| 31 except: |
| 32 # Ignore all errors and fall back on a safe bet. |
| 33 return False |
| 34 |
| 35 |
26 def main(): | 36 def main(): |
27 parser = optparse.OptionParser(description=__doc__) | 37 parser = optparse.OptionParser(description=__doc__) |
28 parser.add_option('-w', '--rewrite_prefix', action='append', default=[], | 38 parser.add_option('-w', '--rewrite_prefix', action='append', default=[], |
29 dest='prefix_map', metavar='SPEC', | 39 dest='prefix_map', metavar='SPEC', |
30 help=('Two path prefixes, separated by colons ' + | 40 help=('Two path prefixes, separated by colons ' + |
31 'specifying that a file whose (relative) path ' + | 41 'specifying that a file whose (relative) path ' + |
32 'name starts with the first prefix should have ' + | 42 'name starts with the first prefix should have ' + |
33 'that prefix replaced by the second prefix to ' + | 43 'that prefix replaced by the second prefix to ' + |
34 'form a path relative to the output directory. ' + | 44 'form a path relative to the output directory. ' + |
35 'The resulting path is used in the deps mapping ' + | 45 'The resulting path is used in the deps mapping ' + |
36 'file path to a list of provided and required ' + | 46 'file path to a list of provided and required ' + |
37 'namespaces.')) | 47 'namespaces.')) |
38 parser.add_option('-o', '--output_file', action='store', default=[], | 48 parser.add_option('-o', '--output_file', action='store', default=[], |
39 metavar='SPEC', | 49 metavar='SPEC', |
40 help=('Where to output the generated deps file.')) | 50 help=('Where to output the generated deps file.')) |
41 options, args = parser.parse_args() | 51 options, args = parser.parse_args() |
42 | 52 |
43 path_rewriter = PathRewriter(options.prefix_map) | 53 path_rewriter = PathRewriter(options.prefix_map) |
44 | 54 |
| 55 content = '' |
| 56 for path in args: |
| 57 js_deps = source.Source(source.GetFileContents(path)) |
| 58 path = path_rewriter.RewritePath(path) |
| 59 content += 'goog.addDependency(\'%s\', %s, %s);\n' % ( |
| 60 path, sorted(js_deps.provides), sorted(js_deps.requires)) |
| 61 if _HasSameContent(options.output_file, content): |
| 62 return |
45 # Write the generated deps file. | 63 # Write the generated deps file. |
46 with open(options.output_file, 'w') as output: | 64 with open(options.output_file, 'w') as output: |
47 for path in args: | 65 output.write(content) |
48 js_deps = source.Source(source.GetFileContents(path)) | |
49 path = path_rewriter.RewritePath(path) | |
50 line = 'goog.addDependency(\'%s\', %s, %s);\n' % ( | |
51 path, sorted(js_deps.provides), sorted(js_deps.requires)) | |
52 output.write(line) | |
53 | |
54 | 66 |
55 if __name__ == '__main__': | 67 if __name__ == '__main__': |
56 main() | 68 main() |
OLD | NEW |