2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Creates simple HTML for running a NaCl module.
8 This script is designed to make the process of creating running
9 Native Client executables in the browers simple by creating
10 boilderplate a .html (and optionally a .nmf) file for a given
11 Native Client executable (.nexe).
13 If the script if given a .nexe file it will produce both html
14 the nmf files. If it is given an nmf it will only create
23 SCRIPT_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
27 Sample html container for embedded NaCl module. This file was auto-generated
28 by the create_html tool which is part of the NaCl SDK.
30 The embed tag is setup with PS_STDOUT, PS_STDERR and PS_TTY_PREFIX attributes
31 which, for applications linked with ppapi_simple, will cause stdout and stderr
32 to be sent to javascript via postMessage. Also, the postMessage listener
33 assumes that all messages sent via postMessage are strings to be displayed in
38 <meta http-equiv="Pragma" content="no-cache">
39 <meta http-equiv="Expires" content="-1">
40 <title>%(title)s</title>
44 <h2>Native Client Module: %(module_name)s</h2>
45 <p>Status: <code id="status">Loading</code></p>
48 <embed id="nacl_module" name="%(module_name)s" src="%(nmf)s"
49 type="application/x-nacl" width=640 height=480
52 PS_STDERR="/dev/tty" >/
55 <p>Standard output/error:</p>
56 <textarea id="stdout" rows="25" cols="80">
60 listenerDiv = document.getElementById("listener")
61 stdout = document.getElementById("stdout")
62 nacl_module = document.getElementById("nacl_module")
64 function updateStatus(message) {
65 document.getElementById("status").innerHTML = message
68 function addToStdout(message) {
69 stdout.value += message;
70 stdout.scrollTop = stdout.scrollHeight;
73 function handleMessage(message) {
74 var payload = message.data;
76 if (typeof(payload) == 'string' && payload.indexOf(prefix) == 0) {
77 addToStdout(payload.slice(prefix.length));
81 function handleCrash(event) {
82 updateStatus("Crashed/exited with status: " + nacl_module.exitStatus)
85 function handleLoad(event) {
86 updateStatus("Loaded")
89 listenerDiv.addEventListener("load", handleLoad, true);
90 listenerDiv.addEventListener("message", handleMessage, true);
91 listenerDiv.addEventListener("crash", handleCrash, true);
98 class Error(Exception):
104 sys
.stderr
.write(str(msg
) + '\n')
108 def CreateHTML(filenames
, options
):
111 for filename
in filenames
:
112 if not os
.path
.exists(filename
):
113 raise Error('file not found: %s' % filename
)
115 if not os
.path
.isfile(filename
):
116 raise Error('specified input is not a file: %s' % filename
)
118 basename
, ext
= os
.path
.splitext(filename
)
119 if ext
not in ('.nexe', '.pexe', '.nmf'):
120 raise Error('input file must be .nexe, .pexe or .nmf: %s' % filename
)
123 if len(filenames
) > 1:
124 raise Error('Only one .nmf argument can be specified')
126 elif len(filenames
) > 1 and not options
.output
:
127 raise Error('When specifying muliple input files -o must'
128 ' also be specified.')
130 htmlfile
= options
.output
132 htmlfile
= basename
+ '.html'
133 basename
= os
.path
.splitext(os
.path
.basename(htmlfile
))[0]
136 nmf
= os
.path
.splitext(htmlfile
)[0] + '.nmf'
137 Log('creating nmf: %s' % nmf
)
138 create_nmf
= os
.path
.join(SCRIPT_DIR
, 'create_nmf.py')
139 staging
= os
.path
.dirname(nmf
)
142 cmd
= [create_nmf
, '-s', staging
, '-o', nmf
] + filenames
145 if options
.debug_libs
:
146 cmd
.append('--debug-libs')
149 subprocess
.check_call(cmd
)
150 except subprocess
.CalledProcessError
:
151 raise Error('create_nmf failed')
153 Log('creating html: %s' % htmlfile
)
154 with
open(htmlfile
, 'w') as outfile
:
156 args
['title'] = basename
157 args
['module_name'] = basename
158 args
['nmf'] = os
.path
.basename(nmf
)
159 outfile
.write(HTML_TEMPLATE
% args
)
163 parser
= argparse
.ArgumentParser(description
=__doc__
)
164 parser
.add_argument('-v', '--verbose', action
='store_true',
165 help='Verbose output')
166 parser
.add_argument('-d', '--debug-libs', action
='store_true',
167 help='When calling create_nmf request debug libaries')
168 parser
.add_argument('-o', '--output', dest
='output',
169 help='Name of html file to write (default is '
170 'input name with .html extension)',
172 parser
.add_argument('exe', metavar
='EXE_OR_NMF', nargs
='+',
173 help='Executable (.nexe/.pexe) or nmf file to generate '
175 # To enable bash completion for this command first install optcomplete
176 # and then add this line to your .bashrc:
177 # complete -F _optcomplete create_html.py
180 optcomplete
.autocomplete(parser
)
184 options
= parser
.parse_args(argv
)
189 CreateHTML(options
.exe
, options
)
193 if __name__
== '__main__':
195 rtn
= main(sys
.argv
[1:])
197 sys
.stderr
.write('%s: %s\n' % (os
.path
.basename(__file__
), e
))
199 except KeyboardInterrupt:
200 sys
.stderr
.write('%s: interrupted\n' % os
.path
.basename(__file__
))