[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | 4f6b1db0 | 2012-05-18 18:15:19 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """This script is used by chrome_tests.gypi's js2webui action to maintain the |
| 7 | argument lists and to generate inlinable tests. |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 8 | """ |
| 9 | |
Raul Tambre | f3d9412e | 2019-09-24 05:31:44 | [diff] [blame] | 10 | from __future__ import print_function |
| 11 | |
[email protected] | 4f6b1db0 | 2012-05-18 18:15:19 | [diff] [blame] | 12 | import json |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 13 | import optparse |
| 14 | import os |
| 15 | import subprocess |
| 16 | import sys |
[email protected] | 4ba1dbc | 2011-10-22 01:46:03 | [diff] [blame] | 17 | import shutil |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 18 | |
[email protected] | 4f6b1db0 | 2012-05-18 18:15:19 | [diff] [blame] | 19 | |
plundblad | 9e0db32 | 2015-05-11 18:12:39 | [diff] [blame] | 20 | def HasSameContent(filename, content): |
| 21 | '''Returns true if the given file is readable and has the given content.''' |
| 22 | try: |
| 23 | with open(filename) as file: |
| 24 | return file.read() == content |
| 25 | except: |
| 26 | # Ignore all errors and fall back on a safe bet. |
| 27 | return False |
| 28 | |
| 29 | |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 30 | def main (): |
| 31 | parser = optparse.OptionParser() |
[email protected] | bf8feaa | 2011-07-12 00:17:36 | [diff] [blame] | 32 | parser.set_usage( |
[email protected] | b566418 | 2014-06-05 22:16:59 | [diff] [blame] | 33 | "%prog v8_shell mock.js test_api.js js2webui.js " |
A Olsen | 0cbed54 | 2019-04-23 10:12:14 | [diff] [blame] | 34 | "testtype inputfile srcrootdir cxxoutfile jsoutfile") |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 35 | parser.add_option('-v', '--verbose', action='store_true') |
| 36 | parser.add_option('-n', '--impotent', action='store_true', |
| 37 | help="don't execute; just print (as if verbose)") |
[email protected] | b566418 | 2014-06-05 22:16:59 | [diff] [blame] | 38 | parser.add_option('--deps_js', action="store", |
| 39 | help=("Path to deps.js for dependency resolution, " + |
| 40 | "optional.")) |
baixo | 3a3c88a | 2014-10-28 11:52:21 | [diff] [blame] | 41 | parser.add_option('--external', action='store', |
| 42 | help="Load V8's initial snapshot from external files (y/n)") |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 43 | (opts, args) = parser.parse_args() |
| 44 | |
[email protected] | b566418 | 2014-06-05 22:16:59 | [diff] [blame] | 45 | if len(args) != 9: |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 46 | parser.error('all arguments are required.') |
[email protected] | b566418 | 2014-06-05 22:16:59 | [diff] [blame] | 47 | (v8_shell, mock_js, test_api, js2webui, test_type, |
A Olsen | 0cbed54 | 2019-04-23 10:12:14 | [diff] [blame] | 48 | inputfile, srcrootdir, cxxoutfile, jsoutfile) = args |
[email protected] | b6ae0be | 2014-05-22 11:39:51 | [diff] [blame] | 49 | cmd = [v8_shell] |
A Olsen | 0cbed54 | 2019-04-23 10:12:14 | [diff] [blame] | 50 | arguments = [js2webui, inputfile, srcrootdir, opts.deps_js, |
[email protected] | b566418 | 2014-06-05 22:16:59 | [diff] [blame] | 51 | cxxoutfile, test_type] |
[email protected] | b6ae0be | 2014-05-22 11:39:51 | [diff] [blame] | 52 | cmd.extend(['-e', "arguments=" + json.dumps(arguments), mock_js, |
[email protected] | b566418 | 2014-06-05 22:16:59 | [diff] [blame] | 53 | test_api, js2webui]) |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 54 | if opts.verbose or opts.impotent: |
Raul Tambre | f3d9412e | 2019-09-24 05:31:44 | [diff] [blame] | 55 | print(cmd) |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 56 | if not opts.impotent: |
| 57 | try: |
[email protected] | abb2ada | 2014-07-02 16:33:08 | [diff] [blame] | 58 | p = subprocess.Popen( |
| 59 | cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) |
[email protected] | 46dd51e9 | 2014-07-01 18:01:30 | [diff] [blame] | 60 | out, err = p.communicate() |
plundblad | 2cadf1f | 2015-08-27 19:58:40 | [diff] [blame] | 61 | if p.returncode != 0: |
| 62 | sys.stderr.write(out + err); |
| 63 | return 1 |
plundblad | 9e0db32 | 2015-05-11 18:12:39 | [diff] [blame] | 64 | if not HasSameContent(cxxoutfile, out): |
| 65 | with open(cxxoutfile, 'wb') as f: |
| 66 | f.write(out) |
[email protected] | 4ba1dbc | 2011-10-22 01:46:03 | [diff] [blame] | 67 | shutil.copyfile(inputfile, jsoutfile) |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 68 | except Exception, ex: |
[email protected] | 4f6b1db0 | 2012-05-18 18:15:19 | [diff] [blame] | 69 | if os.path.exists(cxxoutfile): |
| 70 | os.remove(cxxoutfile) |
| 71 | if os.path.exists(jsoutfile): |
| 72 | os.remove(jsoutfile) |
| 73 | raise |
| 74 | |
[email protected] | 98289949 | 2011-07-01 20:46:17 | [diff] [blame] | 75 | |
| 76 | if __name__ == '__main__': |
plundblad | 9e0db32 | 2015-05-11 18:12:39 | [diff] [blame] | 77 | sys.exit(main()) |