OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright (c) 2011, 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 | |
Emily Fortuna
2012/08/08 01:28:37
extra newline here and a few other places?
Emily Fortuna
2012/08/08 01:33:47
edit: I see you're following the format from creat
jackpal
2012/08/08 23:06:45
Done.
| |
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 makeString(input_file): | |
59 result = ' ' | |
60 fileHandle = open(input_file, 'rb') | |
61 lineCounter = 0 | |
62 for byte in fileHandle.read(): | |
63 result += ' %d,' % ord(byte) | |
64 lineCounter += 1 | |
65 if lineCounter == 10: | |
66 result += '\n ' | |
67 lineCounter = 0 | |
68 if lineCounter != 0: | |
69 result += '\n ' | |
70 return result | |
71 | |
72 def runHost(command): | |
73 print "command %s" % command | |
74 pipe = subprocess.Popen(args=command, | |
75 shell=True, | |
76 stdout=subprocess.PIPE, | |
77 stderr=subprocess.PIPE) | |
78 out, error = pipe.communicate() | |
79 if (pipe.returncode != 0): | |
80 print out, error | |
81 print "command failed" | |
82 print "(Command was: '", ' '.join(command), "')" | |
83 raise Exception("Failed") | |
84 | |
85 def runTarget(command): | |
86 runHost("adb shell %s" % command) | |
87 | |
88 def runOnAndroid(options): | |
89 outputBin = options.output_bin | |
90 | |
91 android_workspace = os.getenv("ANDROID_DART", "/data/local/dart") | |
92 android_outputBin = join(android_workspace, basename(outputBin)) | |
93 | |
94 executable = options.executable | |
95 android_executable = join(android_workspace, basename(executable)) | |
96 | |
97 filesToPush = [] # (src, dest) | |
98 filesToPull = [] # (src, dest) | |
99 | |
100 # Setup arguments to the snapshot generator binary. | |
101 | |
Emily Fortuna
2012/08/08 01:33:47
okay this seems like random newlines...?
jackpal
2012/08/08 23:06:45
Done.
| |
102 | |
103 script_args = [android_executable] | |
104 | |
105 # First setup the snapshot output filename. | |
106 filesToPull.append((android_outputBin, outputBin)) | |
107 script_args.append(''.join([ "--snapshot=", android_outputBin])) | |
108 | |
109 # Next setup all url mapping options specified. | |
110 for url_arg in options.url_mapping: | |
111 android_url_arg = join(android_workspace, basename(url_arg)) | |
112 filesToPush.push((url_arg, android_url_arg)) | |
113 url_mapping_argument = ''.join(["--url_mapping=", url_arg ]) | |
114 script_args.append(url_mapping_argument) | |
115 | |
116 # Finally append the script name if one is specified. | |
117 if options.script: | |
118 android_script_name = join(android_workspace, basename(options.script)) | |
119 filesToPush.append((options.script, android_script_name)) | |
120 script_args.append(android_script_name) | |
121 | |
122 filesToPush.append((executable, android_executable)) | |
123 | |
124 command = ' '.join(script_args) | |
125 | |
126 runHost("adb shell mkdir %s" % android_workspace) | |
127 try: | |
128 for src, dest in filesToPush: | |
129 runHost("adb push '%s' '%s'" % (src, dest)) | |
130 runTarget(command) | |
131 for src, dest in filesToPull: | |
132 runHost("adb pull '%s' '%s'" % (src, dest)) | |
133 finally: | |
134 for src, dest in filesToPush: | |
135 runHost("adb shell rm '%s'" % dest) | |
136 for src, dest in filesToPull: | |
137 runHost("adb shell rm '%s'" % src) | |
138 | |
139 def Main(): | |
140 # Parse options. | |
141 parser = BuildOptions() | |
142 (options, args) = parser.parse_args() | |
143 if not ProcessOptions(options): | |
144 parser.print_help() | |
145 return 1 | |
146 | |
147 # If there are additional arguments, report error and exit. | |
148 if args: | |
149 parser.print_help() | |
150 return 1 | |
151 | |
152 # Setup arguments to the snapshot generator binary. | |
153 script_args = [] | |
154 | |
155 # First setup the snapshot output filename. | |
156 script_args.append(''.join([ "--snapshot=", options.output_bin ])) | |
157 | |
158 # Next setup all url mapping options specified. | |
159 for url_arg in options.url_mapping: | |
160 url_mapping_argument = ''.join(["--url_mapping=", url_arg ]) | |
161 script_args.append(url_mapping_argument) | |
162 | |
163 # Finally append the script name if one is specified. | |
164 if options.script: | |
165 script_args.append(options.script) | |
166 | |
167 # Construct command line to execute the snapshot generator binary and invoke. | |
168 command = [ options.executable ] + script_args | |
169 if options.verbose: | |
170 print ' '.join(command) | |
171 | |
172 if options.target_os == 'android': | |
173 try: | |
174 runOnAndroid(options) | |
175 except Exception as e: | |
176 print "Could not run on Android: %s" % e | |
177 return -1 | |
178 else: | |
179 pipe = subprocess.Popen(command, | |
180 stdout=subprocess.PIPE, | |
181 stderr=subprocess.PIPE) | |
182 out, error = pipe.communicate() | |
183 if (pipe.returncode != 0): | |
184 print out, error | |
185 print "Snapshot generation failed" | |
186 print "(Command was: '", ' '.join(command), "')" | |
187 return -1 | |
188 | |
189 return 0 | |
190 | |
191 if __name__ == '__main__': | |
192 sys.exit(Main()) | |
OLD | NEW |