Add ICU message format support
[chromium-blink-merge.git] / tools / gypv8sh.py
blob2a1fada8a5729fd033a26e368ae2546f5d3a5b28
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """This script is used by chrome_tests.gypi's js2webui action to maintain the
7 argument lists and to generate inlinable tests.
8 """
10 import json
11 import optparse
12 import os
13 import subprocess
14 import sys
15 import shutil
18 def HasSameContent(filename, content):
19 '''Returns true if the given file is readable and has the given content.'''
20 try:
21 with open(filename) as file:
22 return file.read() == content
23 except:
24 # Ignore all errors and fall back on a safe bet.
25 return False
28 def main ():
29 parser = optparse.OptionParser()
30 parser.set_usage(
31 "%prog v8_shell mock.js test_api.js js2webui.js "
32 "testtype inputfile inputrelfile cxxoutfile jsoutfile")
33 parser.add_option('-v', '--verbose', action='store_true')
34 parser.add_option('-n', '--impotent', action='store_true',
35 help="don't execute; just print (as if verbose)")
36 parser.add_option('--deps_js', action="store",
37 help=("Path to deps.js for dependency resolution, " +
38 "optional."))
39 parser.add_option('--external', action='store',
40 help="Load V8's initial snapshot from external files (y/n)")
41 (opts, args) = parser.parse_args()
43 if len(args) != 9:
44 parser.error('all arguments are required.')
45 (v8_shell, mock_js, test_api, js2webui, test_type,
46 inputfile, inputrelfile, cxxoutfile, jsoutfile) = args
47 cmd = [v8_shell]
48 icudatafile = os.path.join(os.path.dirname(v8_shell), 'icudtl.dat')
49 if os.path.exists(icudatafile):
50 cmd.extend(['--icu-data-file=%s' % icudatafile])
51 v8nativesfile = os.path.join(os.path.dirname(v8_shell), 'natives_blob.bin')
52 if opts.external == 'y' and os.path.exists(v8nativesfile):
53 cmd.extend(['--natives_blob=%s' % v8nativesfile])
54 v8snapshotfile = os.path.join(os.path.dirname(v8_shell), 'snapshot_blob.bin')
55 if opts.external == 'y' and os.path.exists(v8snapshotfile):
56 cmd.extend(['--snapshot_blob=%s' % v8snapshotfile])
57 arguments = [js2webui, inputfile, inputrelfile, opts.deps_js,
58 cxxoutfile, test_type]
59 cmd.extend(['-e', "arguments=" + json.dumps(arguments), mock_js,
60 test_api, js2webui])
61 if opts.verbose or opts.impotent:
62 print cmd
63 if not opts.impotent:
64 try:
65 p = subprocess.Popen(
66 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0)
67 out, err = p.communicate()
68 if not HasSameContent(cxxoutfile, out):
69 with open(cxxoutfile, 'wb') as f:
70 f.write(out)
71 shutil.copyfile(inputfile, jsoutfile)
72 except Exception, ex:
73 if os.path.exists(cxxoutfile):
74 os.remove(cxxoutfile)
75 if os.path.exists(jsoutfile):
76 os.remove(jsoutfile)
77 raise
80 if __name__ == '__main__':
81 sys.exit(main())