| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
| 4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 """Command line wrapper to run the frog compiler under the Dart VM""" | 6 """Command line wrapper to run the frog compiler under the Dart VM""" |
| 7 | 7 |
| 8 # TODO(jimhug): This is a temporary hack to enable running on the VM. We need | 8 # TODO(jimhug): This is a temporary hack to enable running on the VM. We need |
| 9 # the ability to get command-line arguments, to return exit codes, and to | 9 # the ability to get command-line arguments, to return exit codes, and to |
| 10 # communicate with spawned processes in the VM for this to go away. | 10 # communicate with spawned processes in the VM for this to go away. |
| 11 | 11 |
| 12 | 12 |
| 13 import optparse | 13 import optparse |
| 14 import os | 14 import os |
| 15 import platform | 15 import platform |
| 16 import tempfile | 16 import tempfile |
| 17 import shutil | 17 import shutil |
| 18 import subprocess | 18 import subprocess |
| 19 import sys | 19 import sys |
| 20 | 20 |
| 21 from os.path import dirname, join, realpath | 21 |
| 22 from os.path import dirname, join, realpath, exists |
| 22 | 23 |
| 23 HOME = dirname(realpath(__file__)) | 24 HOME = dirname(realpath(__file__)) |
| 24 DART = join(HOME, 'bin/dart_bin') | 25 sys.path.append(join(HOME, os.pardir, 'tools')) |
| 26 import utils |
| 27 |
| 25 CODE = '''#import('%(HOME)s/lang.dart'); | 28 CODE = '''#import('%(HOME)s/lang.dart'); |
| 26 #import('%(HOME)s/file_system_vm.dart'); | 29 #import('%(HOME)s/file_system_vm.dart'); |
| 27 | 30 |
| 28 void main() { | 31 void main() { |
| 29 if (!compile(%(HOME)r, %(args)r, new VMFileSystem())) { | 32 if (!compile(%(HOME)r, %(args)r, new VMFileSystem())) { |
| 30 throw 'frog: compilation failed.'; | 33 throw 'frog: compilation failed.'; |
| 31 } | 34 } |
| 32 } | 35 } |
| 33 ''' | 36 ''' |
| 34 | 37 |
| 35 HTML = '''<html> | 38 HTML = '''<html> |
| 36 <head><title>Frog</title><head> | 39 <head><title>Frog</title><head> |
| 37 <body> | 40 <body> |
| 38 <script type="application/javascript" src="out.js"></script> | 41 <script type="application/javascript" src="out.js"></script> |
| 39 </body> | 42 </body> |
| 40 </html> | 43 </html> |
| 41 ''' | 44 ''' |
| 42 | 45 |
| 46 def GetDart(): |
| 47 # Try a release version |
| 48 dart = utils.GetDartRunner('release', 'ia32', 'vm') |
| 49 if exists(dart): return dart |
| 50 # Try at the top level |
| 51 dart = join(os.pardir, dart) |
| 52 if exists(dart): return dart |
| 53 |
| 54 # Try a debug version |
| 55 dart = utils.GetDartRunner('debug', 'ia32', 'vm') |
| 56 if exists(dart): return dart |
| 57 # Try at the top level |
| 58 dart = join(os.pardir, dart) |
| 59 return dart |
| 60 |
| 43 | 61 |
| 44 def execute(cmd): | 62 def execute(cmd): |
| 45 """Execute a command in a subprocess. """ | 63 """Execute a command in a subprocess. """ |
| 46 try: | 64 try: |
| 47 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 65 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 48 out, err = pipe.communicate() | 66 out, err = pipe.communicate() |
| 49 if out is not None: sys.stdout.write(out) | 67 if out is not None: sys.stdout.write(out) |
| 50 if err is not None: sys.stderr.write(err) | 68 if err is not None: sys.stderr.write(err) |
| 51 return pipe.returncode | 69 return pipe.returncode |
| 52 except Exception as e: | 70 except Exception as e: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 80 | 98 |
| 81 # TODO(vsm): Hook in HtmlConverter. | 99 # TODO(vsm): Hook in HtmlConverter. |
| 82 optionParser.add_option('--html', | 100 optionParser.add_option('--html', |
| 83 action='store_true', help='Invoke this in the browser instead of node/d8.'
) | 101 action='store_true', help='Invoke this in the browser instead of node/d8.'
) |
| 84 optionParser.add_option('--browser', | 102 optionParser.add_option('--browser', |
| 85 default = None, | 103 default = None, |
| 86 metavar='FILE', help='The browser to use to run output HTML.') | 104 metavar='FILE', help='The browser to use to run output HTML.') |
| 87 | 105 |
| 88 optionParser.add_option('--verbose', | 106 optionParser.add_option('--verbose', |
| 89 help='Verbose output', default=False, action='store_true') | 107 help='Verbose output', default=False, action='store_true') |
| 90 | 108 |
| 91 optionParser.set_usage("frog <dart-script-file> [<dart-options>]") | 109 optionParser.set_usage("frog <dart-script-file> [<dart-options>]") |
| 92 return optionParser.parse_args(args) | 110 return optionParser.parse_args(args) |
| 93 | 111 |
| 94 | 112 |
| 95 def main(args): | 113 def main(args): |
| 96 if '--' in args: | 114 if '--' in args: |
| 97 index = args.index('--') | 115 index = args.index('--') |
| 98 pythonArgs = args[1:index] | 116 pythonArgs = args[1:index] |
| 99 dartArgs = ['python', args[0]] + args[index+1:] | 117 dartArgs = ['python', args[0]] + args[index+1:] |
| 100 else: | 118 else: |
| 101 pythonArgs = [] | 119 pythonArgs = [] |
| 102 dartArgs = ['python'] + args | 120 dartArgs = ['python'] + args |
| 103 | 121 |
| 104 options, extraArgs = parseOptions(pythonArgs) | 122 options, extraArgs = parseOptions(pythonArgs) |
| 105 if options.verbose: | 123 if options.verbose: |
| 106 print ("dartArgs=%s pythonArgs=%s extraArgs=%s" % | 124 print ("dartArgs=%s pythonArgs=%s extraArgs=%s" % |
| 107 (' '.join(dartArgs), ' '.join(pythonArgs), ' '.join(extraArgs))) | 125 (' '.join(dartArgs), ' '.join(pythonArgs), ' '.join(extraArgs))) |
| 108 | 126 |
| 109 if len(extraArgs) != 0: | 127 if len(extraArgs) != 0: |
| 110 optionParser.print_help() | 128 optionParser.print_help() |
| 111 return 1 | 129 return 1 |
| 112 | 130 |
| 113 if not os.path.exists(DART): | 131 dart = GetDart() |
| 114 print("Dart VM not configured in %s" % DART) | 132 if not exists(dart): |
| 133 print("Dart VM not configured in %s" % dart) |
| 115 return 1 | 134 return 1 |
| 116 | 135 |
| 117 if subprocess.call("node --help >/dev/null 2>&1", shell=True): | 136 if subprocess.call("node --help >/dev/null 2>&1", shell=True): |
| 118 print "Executing node failed. See frog/README.txt for instructions" | 137 print "Executing node failed. See frog/README.txt for instructions" |
| 119 return 1 | 138 return 1 |
| 120 | 139 |
| 121 return compileAndRun(options, dartArgs) | 140 return compileAndRun(options, dartArgs, dart) |
| 122 | 141 |
| 123 | 142 |
| 124 def compileAndRun(options, args): | 143 def compileAndRun(options, args, dart): |
| 125 nodeArgs = [] | 144 nodeArgs = [] |
| 126 for i in range(len(args)): | 145 for i in range(len(args)): |
| 127 if args[i].endswith('.dart'): | 146 if args[i].endswith('.dart'): |
| 128 nodeArgs = args[i+1:] | 147 nodeArgs = args[i+1:] |
| 129 args = args[:i+1] | 148 args = args[:i+1] |
| 130 break | 149 break |
| 131 | 150 |
| 132 if options.verbose: print "nodeArgs %s" % ' '.join(nodeArgs); | 151 if options.verbose: print "nodeArgs %s" % ' '.join(nodeArgs); |
| 133 | 152 |
| 134 workdir = options.workdir | 153 workdir = options.workdir |
| 135 cleanup = False | 154 cleanup = False |
| 136 if not workdir: | 155 if not workdir: |
| 137 if not options.html: | 156 if not options.html: |
| 138 workdir = tempfile.mkdtemp() | 157 workdir = tempfile.mkdtemp() |
| 139 if not options.keep_files: | 158 if not options.keep_files: |
| 140 cleanup = True | 159 cleanup = True |
| 141 else: | 160 else: |
| 142 # A persistent location for the browser to load. | 161 # A persistent location for the browser to load. |
| 143 workdir = 'html' | 162 workdir = 'html' |
| (...skipping 17 matching lines...) Expand all Loading... |
| 161 browser = 'google-chrome' | 180 browser = 'google-chrome' |
| 162 | 181 |
| 163 args = args[:2] + ['--out=%s' % outfile, '--libdir=%s/lib' % HOME] + args[2:] | 182 args = args[:2] + ['--out=%s' % outfile, '--libdir=%s/lib' % HOME] + args[2:] |
| 164 | 183 |
| 165 f = open(GODART, 'w') | 184 f = open(GODART, 'w') |
| 166 go_contents = CODE % { 'HOME' : HOME, 'args': args } | 185 go_contents = CODE % { 'HOME' : HOME, 'args': args } |
| 167 f.write(go_contents) | 186 f.write(go_contents) |
| 168 f.close() | 187 f.close() |
| 169 | 188 |
| 170 if options.verbose: print "Wrote wrapper to %s:\n%s" % (GODART, go_contents); | 189 if options.verbose: print "Wrote wrapper to %s:\n%s" % (GODART, go_contents); |
| 171 | 190 |
| 172 compiler_cmd = [DART] | 191 compiler_cmd = [dart] |
| 173 if options.vm_flags: | 192 if options.vm_flags: |
| 174 compiler_cmd.extend(options.vm_flags.split(' ')) | 193 compiler_cmd.extend(options.vm_flags.split(' ')) |
| 175 compiler_cmd.append(GODART); | 194 compiler_cmd.append(GODART); |
| 176 exit_code = execute(compiler_cmd) | 195 exit_code = execute(compiler_cmd) |
| 177 if exit_code: | 196 if exit_code: |
| 178 if options.verbose: print ("cmd exited with status %d" % exit_code) | 197 if options.verbose: print ("cmd exited with status %d" % exit_code) |
| 179 if cleanup: shutil.rmtree(workdir) | 198 if cleanup: shutil.rmtree(workdir) |
| 180 if exit_code < 0: | 199 if exit_code < 0: |
| 181 print("VM exited with signal %d" % (-exit_code)) | 200 print("VM exited with signal %d" % (-exit_code)) |
| 182 return exit_code | 201 return exit_code |
| 183 | 202 |
| 184 result = 0 | 203 result = 0 |
| 185 if not options.js_out: | 204 if not options.js_out: |
| 186 if not options.html: | 205 if not options.html: |
| 187 js_cmd = options.js_cmd | 206 js_cmd = options.js_cmd |
| 188 result = execute(js_cmd.split(' ') + [outfile] + nodeArgs) | 207 result = execute(js_cmd.split(' ') + [outfile] + nodeArgs) |
| 189 else: | 208 else: |
| 190 f = open(outhtml, 'w') | 209 f = open(outhtml, 'w') |
| 191 f.write(HTML) | 210 f.write(HTML) |
| 192 f.close() | 211 f.close() |
| 193 result = execute([browser, outhtml]) | 212 result = execute([browser, outhtml]) |
| 194 | 213 |
| 195 if cleanup: shutil.rmtree(workdir) | 214 if cleanup: shutil.rmtree(workdir) |
| 196 if result != 0: | 215 if result != 0: |
| 197 return 1 | 216 return 1 |
| 198 return 0 | 217 return 0 |
| 199 | 218 |
| 200 if __name__ == '__main__': | 219 if __name__ == '__main__': |
| 201 sys.exit(main(sys.argv)) | 220 sys.exit(main(sys.argv)) |
| OLD | NEW |