OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 4 # for details. All rights reserved. Use of this source code is governed by a |
| 5 # BSD-style license that can be found in the LICENSE file. |
| 6 |
| 7 """Script to create snapshot bin file.""" |
| 8 |
| 9 import getopt |
| 10 import optparse |
| 11 import os |
| 12 from os.path import abspath, basename, dirname, join |
| 13 import string |
| 14 import subprocess |
| 15 import sys |
| 16 import tempfile |
| 17 import utils |
| 18 |
| 19 |
| 20 HOST_OS = utils.GuessOS() |
| 21 HOST_CPUS = utils.GuessCpus() |
| 22 |
| 23 |
| 24 def BuildOptions(): |
| 25 result = optparse.OptionParser() |
| 26 result.add_option("--executable", |
| 27 action="store", type="string", |
| 28 help="path to snapshot generator executable") |
| 29 result.add_option("--output_bin", |
| 30 action="store", type="string", |
| 31 help="output file name into which snapshot in binary form is generated") |
| 32 result.add_option("--script", |
| 33 action="store", type="string", |
| 34 help="Dart script for which snapshot is to be generated") |
| 35 result.add_option("--url_mapping", |
| 36 default=[], |
| 37 action="append", |
| 38 help="mapping from url to file name, used when generating snapshots") |
| 39 result.add_option("-v", "--verbose", |
| 40 help='Verbose output.', |
| 41 default=False, action="store_true") |
| 42 result.add_option("--target_os", |
| 43 action="store", type="string", |
| 44 help="Which os to run the executable on") |
| 45 return result |
| 46 |
| 47 |
| 48 def ProcessOptions(options): |
| 49 if not options.executable: |
| 50 sys.stderr.write('--executable not specified\n') |
| 51 return False |
| 52 if not options.output_bin: |
| 53 sys.stderr.write('--output_bin not specified\n') |
| 54 return False |
| 55 return True |
| 56 |
| 57 |
| 58 def RunHost(command): |
| 59 print "command %s" % command |
| 60 pipe = subprocess.Popen(args=command, |
| 61 shell=True, |
| 62 stdout=subprocess.PIPE, |
| 63 stderr=subprocess.PIPE) |
| 64 out, error = pipe.communicate() |
| 65 if (pipe.returncode != 0): |
| 66 print out, error |
| 67 print "command failed" |
| 68 print "(Command was: '", ' '.join(command), "')" |
| 69 raise Exception("Failed") |
| 70 |
| 71 |
| 72 def RunTarget(command): |
| 73 RunHost("adb shell %s" % command) |
| 74 |
| 75 |
| 76 def RunOnAndroid(options): |
| 77 outputBin = options.output_bin |
| 78 |
| 79 android_workspace = os.getenv("ANDROID_DART", "/data/local/dart") |
| 80 android_outputBin = join(android_workspace, basename(outputBin)) |
| 81 |
| 82 executable = options.executable |
| 83 android_executable = join(android_workspace, basename(executable)) |
| 84 |
| 85 filesToPush = [] # (src, dest) |
| 86 filesToPull = [] # (src, dest) |
| 87 |
| 88 # Setup arguments to the snapshot generator binary. |
| 89 script_args = [android_executable] |
| 90 |
| 91 # First setup the snapshot output filename. |
| 92 filesToPull.append((android_outputBin, outputBin)) |
| 93 script_args.append(''.join([ "--snapshot=", android_outputBin])) |
| 94 |
| 95 # We don't know what source files are needed to fully satisfy a dart script, |
| 96 # so we can't support the general case of url mapping or script inclusion. |
| 97 if options.url_mapping: |
| 98 raise Exception("--url_mapping is not supported when building for Android") |
| 99 |
| 100 if options.script: |
| 101 raise Exception("--script is not supported when building for Android") |
| 102 |
| 103 filesToPush.append((executable, android_executable)) |
| 104 |
| 105 command = ' '.join(script_args) |
| 106 |
| 107 RunHost("adb shell mkdir %s" % android_workspace) |
| 108 try: |
| 109 for src, dest in filesToPush: |
| 110 RunHost("adb push '%s' '%s'" % (src, dest)) |
| 111 RunTarget(command) |
| 112 for src, dest in filesToPull: |
| 113 RunHost("adb pull '%s' '%s'" % (src, dest)) |
| 114 finally: |
| 115 for src, dest in filesToPush: |
| 116 RunHost("adb shell rm '%s'" % dest) |
| 117 for src, dest in filesToPull: |
| 118 RunHost("adb shell rm '%s'" % src) |
| 119 |
| 120 |
| 121 def Main(): |
| 122 # Parse options. |
| 123 parser = BuildOptions() |
| 124 (options, args) = parser.parse_args() |
| 125 if not ProcessOptions(options): |
| 126 parser.print_help() |
| 127 return 1 |
| 128 |
| 129 # If there are additional arguments, report error and exit. |
| 130 if args: |
| 131 parser.print_help() |
| 132 return 1 |
| 133 |
| 134 # Setup arguments to the snapshot generator binary. |
| 135 script_args = [] |
| 136 |
| 137 # First setup the snapshot output filename. |
| 138 script_args.append(''.join([ "--snapshot=", options.output_bin ])) |
| 139 |
| 140 # Next setup all url mapping options specified. |
| 141 for url_arg in options.url_mapping: |
| 142 url_mapping_argument = ''.join(["--url_mapping=", url_arg ]) |
| 143 script_args.append(url_mapping_argument) |
| 144 |
| 145 # Finally append the script name if one is specified. |
| 146 if options.script: |
| 147 script_args.append(options.script) |
| 148 |
| 149 # Construct command line to execute the snapshot generator binary and invoke. |
| 150 command = [ options.executable ] + script_args |
| 151 if options.verbose: |
| 152 print ' '.join(command) |
| 153 |
| 154 if options.target_os == 'android': |
| 155 try: |
| 156 RunOnAndroid(options) |
| 157 except Exception as e: |
| 158 print "Could not run on Android: %s" % e |
| 159 return -1 |
| 160 else: |
| 161 pipe = subprocess.Popen(command, |
| 162 stdout=subprocess.PIPE, |
| 163 stderr=subprocess.PIPE) |
| 164 out, error = pipe.communicate() |
| 165 if (pipe.returncode != 0): |
| 166 print out, error |
| 167 print "Snapshot generation failed" |
| 168 print "(Command was: '", ' '.join(command), "')" |
| 169 return -1 |
| 170 |
| 171 return 0 |
| 172 |
| 173 |
| 174 if __name__ == '__main__': |
| 175 sys.exit(Main()) |
OLD | NEW |