#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""This script is used by chrome_tests.gypi's js2webui action to maintain the
argument lists and to generate inlinable tests.
"""

import json
import optparse
import os
import subprocess
import sys
import shutil


def main ():
  parser = optparse.OptionParser()
  parser.set_usage(
      "%prog v8_shell mock.js test_api.js js2webui.js "
      "testtype inputfile inputrelfile cxxoutfile jsoutfile")
  parser.add_option('-v', '--verbose', action='store_true')
  parser.add_option('-n', '--impotent', action='store_true',
                    help="don't execute; just print (as if verbose)")
  parser.add_option('--deps_js', action="store",
                    help=("Path to deps.js for dependency resolution, " +
                          "optional."))
  (opts, args) = parser.parse_args()

  if len(args) != 9:
    parser.error('all arguments are required.')
  (v8_shell, mock_js, test_api, js2webui, test_type,
      inputfile, inputrelfile, cxxoutfile, jsoutfile) = args
  cmd = [v8_shell]
  icudatafile = os.path.join(os.path.dirname(v8_shell), 'icudtl.dat')
  if os.path.exists(icudatafile):
    cmd.extend(['--icu-data-file=%s' % icudatafile])
  arguments = [js2webui, inputfile, inputrelfile, opts.deps_js,
               cxxoutfile, test_type]
  cmd.extend(['-e', "arguments=" + json.dumps(arguments), mock_js,
         test_api, js2webui])
  if opts.verbose or opts.impotent:
    print cmd
  if not opts.impotent:
    try:
      p = subprocess.Popen(
          cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0)
      out, err = p.communicate()
      if p.returncode:
        # TODO(jochen): Remove once crbug.com/370551 is resolved.
        if sys.platform == 'darwin':
          sys.path.insert(0, '/Developer/Library/PrivateFrameworks/'
                             'LLDB.framework/Resources/Python')
          try:
            import lldb
          except:
            raise Exception("Could not load lldb module")
          debugger = lldb.SBDebugger.Create()
          debugger.SetAsync(False)
          target = debugger.CreateTargetWithFileAndArch(
              cmd[0], lldb.LLDB_ARCH_DEFAULT)
          if not target:
            raise Exception("Failed to create d8 target")
          process = target.LaunchSimple(cmd[1:], None, os.getcwd())
          if not process:
            raise Exception("Failed to start d8")
          if process.GetState() == lldb.eStateStopped:
            for thread in process:
              print "Thread (id %d)" % thread.GetThreadID()
              for frame in thread:
                print frame
              print ""
            raise Exception(
                "d8 crashed, please report this at http://crbug.com/370551")
          else:
            # For some reason d8 worked this time...
            out = ''
            while True:
              s = process.GetSTDOUT(4096)
              if s == '':
                break
              out += s

      with open(cxxoutfile, 'wb') as f:
        f.write(out)
      shutil.copyfile(inputfile, jsoutfile)
    except Exception, ex:
      if os.path.exists(cxxoutfile):
        os.remove(cxxoutfile)
      if os.path.exists(jsoutfile):
        os.remove(jsoutfile)
      raise


if __name__ == '__main__':
 sys.exit(main())

@@ -1754,1363 +1754,8 @@
 e()%0A
-      if p.returncode:%0A        # TODO(jochen): Remove once crbug.com/370551 is resolved.%0A        if sys.platform == 'darwin':%0A          sys.path.insert(0, '/Developer/Library/PrivateFrameworks/'%0A                             'LLDB.framework/Resources/Python')%0A          try:%0A            import lldb%0A          except:%0A            raise Exception(%22Could not load lldb module%22)%0A          debugger = lldb.SBDebugger.Create()%0A          debugger.SetAsync(False)%0A          target = debugger.CreateTargetWithFileAndArch(%0A              cmd%5B0%5D, lldb.LLDB_ARCH_DEFAULT)%0A          if not target:%0A            raise Exception(%22Failed to create d8 target%22)%0A          process = target.LaunchSimple(cmd%5B1:%5D, None, os.getcwd())%0A          if not process:%0A            raise Exception(%22Failed to start d8%22)%0A          if process.GetState() == lldb.eStateStopped:%0A            for thread in process:%0A              print %22Thread (id %25d)%22 %25 thread.GetThreadID()%0A              for frame in thread:%0A                print frame%0A              print %22%22%0A            raise Exception(%0A                %22d8 crashed, please report this at http://crbug.com/370551%22)%0A          else:%0A            # For some reason d8 worked this time...%0A            out = ''%0A            while True:%0A              s = process.GetSTDOUT(4096)%0A              if s == '':%0A                break%0A              out += s%0A%0A
     

Remove debugging code from gypv8sh.py