Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1422)

Side by Side Diff: runtime/tools/create_snapshot_bin.py

Issue 10827250: Support generating the dart vm snapshot binary on Android (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #!/usr/bin/env python
ahe 2012/08/09 20:57:16 Since this is a new file, you should use the exter
Ivan Posva 2012/08/09 21:48:15 Peter, I disagree. This is essentially a copy of t
ahe 2012/08/09 22:29:10 Ivan, I defer to your judgement regarding the styl
2 #
3 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
ahe 2012/08/09 20:57:16 This comment is so last year.
jackpal 2012/08/13 20:19:45 Done.
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.
Emily Fortuna 2012/08/09 20:15:57 use """ ... """
Ivan Posva 2012/08/09 21:48:15 ditto from above: Emily, what is the benefit of th
8
9 import getopt
10 import optparse
11 import os
12 from os.path import abspath, basename, dirname, join
ahe 2012/08/09 20:57:16 This violates "Use imports for packages and module
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 makeString(input_file):
Emily Fortuna 2012/08/09 20:15:57 inconsistent capitalization here compared to above
Ivan Posva 2012/08/09 21:48:15 makeString use has been moved out to the other for
jackpal 2012/08/13 20:19:45 Done.
jackpal 2012/08/13 20:19:45 Done.
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 script_args = [android_executable]
102
103 # First setup the snapshot output filename.
104 filesToPull.append((android_outputBin, outputBin))
105 script_args.append(''.join([ "--snapshot=", android_outputBin]))
106
107 # Next setup all url mapping options specified.
108 for url_arg in options.url_mapping:
109 android_url_arg = join(android_workspace, basename(url_arg))
Ivan Posva 2012/08/09 21:48:15 I don't think the logic here works because you are
jackpal 2012/08/13 20:19:45 Done.
110 filesToPush.push((url_arg, android_url_arg))
111 url_mapping_argument = ''.join(["--url_mapping=", url_arg ])
112 script_args.append(url_mapping_argument)
113
114 # Finally append the script name if one is specified.
115 if options.script:
116 android_script_name = join(android_workspace, basename(options.script))
Ivan Posva 2012/08/09 21:48:15 ditto.
jackpal 2012/08/13 20:19:45 Done.
117 filesToPush.append((options.script, android_script_name))
118 script_args.append(android_script_name)
119
120 filesToPush.append((executable, android_executable))
121
122 command = ' '.join(script_args)
123
124 runHost("adb shell mkdir %s" % android_workspace)
125 try:
126 for src, dest in filesToPush:
127 runHost("adb push '%s' '%s'" % (src, dest))
128 runTarget(command)
129 for src, dest in filesToPull:
130 runHost("adb pull '%s' '%s'" % (src, dest))
131 finally:
132 for src, dest in filesToPush:
133 runHost("adb shell rm '%s'" % dest)
134 for src, dest in filesToPull:
135 runHost("adb shell rm '%s'" % src)
136
137 def Main():
138 # Parse options.
139 parser = BuildOptions()
140 (options, args) = parser.parse_args()
141 if not ProcessOptions(options):
142 parser.print_help()
143 return 1
144
145 # If there are additional arguments, report error and exit.
146 if args:
147 parser.print_help()
148 return 1
149
150 # Setup arguments to the snapshot generator binary.
151 script_args = []
152
153 # First setup the snapshot output filename.
154 script_args.append(''.join([ "--snapshot=", options.output_bin ]))
155
156 # Next setup all url mapping options specified.
157 for url_arg in options.url_mapping:
158 url_mapping_argument = ''.join(["--url_mapping=", url_arg ])
159 script_args.append(url_mapping_argument)
160
161 # Finally append the script name if one is specified.
162 if options.script:
163 script_args.append(options.script)
164
165 # Construct command line to execute the snapshot generator binary and invoke.
166 command = [ options.executable ] + script_args
167 if options.verbose:
168 print ' '.join(command)
169
170 if options.target_os == 'android':
171 try:
172 runOnAndroid(options)
173 except Exception as e:
174 print "Could not run on Android: %s" % e
175 return -1
176 else:
177 pipe = subprocess.Popen(command,
178 stdout=subprocess.PIPE,
179 stderr=subprocess.PIPE)
180 out, error = pipe.communicate()
181 if (pipe.returncode != 0):
182 print out, error
183 print "Snapshot generation failed"
184 print "(Command was: '", ' '.join(command), "')"
185 return -1
186
187 return 0
188
189 if __name__ == '__main__':
190 sys.exit(Main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698