Autofill: Add WalletIntegrationAvailable() to components.
[chromium-blink-merge.git] / native_client_sdk / src / tools / create_html.py
blob8647b46a373f99fd855e5c41691bea04bb6556dc
1 #!/usr/bin/env python
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
15 the html file.
16 """
18 import argparse
19 import os
20 import sys
21 import subprocess
23 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
24 HTML_TEMPLATE = '''\
25 <!DOCTYPE html>
26 <!--
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
34 the output textarea.
35 -->
36 <html>
37 <head>
38 <meta http-equiv="Pragma" content="no-cache">
39 <meta http-equiv="Expires" content="-1">
40 <title>%(title)s</title>
42 </head>
43 <body>
44 <h2>Native Client Module: %(module_name)s</h2>
45 <p>Status: <code id="status">Loading</code></p>
47 <div id="listener">
48 <embed id="nacl_module" name="%(module_name)s" src="%(nmf)s"
49 type="application/x-nacl" width=640 height=480
50 PS_TTY_PREFIX="tty:"
51 PS_STDOUT="/dev/tty"
52 PS_STDERR="/dev/tty" >/
53 </div>
55 <p>Standard output/error:</p>
56 <textarea id="stdout" rows="25" cols="80">
57 </textarea>
59 <script>
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;
75 var prefix = "tty:";
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);
92 </script>
93 </body>
94 </html>
95 '''
98 class Error(Exception):
99 pass
102 def Log(msg):
103 if Log.enabled:
104 sys.stderr.write(str(msg) + '\n')
105 Log.enabled = False
108 def CreateHTML(filenames, options):
109 nmf = None
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)
122 if ext == '.nmf':
123 if len(filenames) > 1:
124 raise Error('Only one .nmf argument can be specified')
125 nmf = filename
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
131 if not htmlfile:
132 htmlfile = basename + '.html'
133 basename = os.path.splitext(os.path.basename(htmlfile))[0]
135 if not nmf:
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)
140 if not staging:
141 staging = '.'
142 cmd = [create_nmf, '-s', staging, '-o', nmf] + filenames
143 if options.verbose:
144 cmd.append('-v')
145 if options.debug_libs:
146 cmd.append('--debug-libs')
147 Log(cmd)
148 try:
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:
155 args = {}
156 args['title'] = basename
157 args['module_name'] = basename
158 args['nmf'] = os.path.basename(nmf)
159 outfile.write(HTML_TEMPLATE % args)
162 def main(argv):
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)',
171 metavar='FILE')
172 parser.add_argument('exe', metavar='EXE_OR_NMF', nargs='+',
173 help='Executable (.nexe/.pexe) or nmf file to generate '
174 'html for.')
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
178 try:
179 import optcomplete
180 optcomplete.autocomplete(parser)
181 except ImportError:
182 pass
184 options = parser.parse_args(argv)
186 if options.verbose:
187 Log.enabled = True
189 CreateHTML(options.exe, options)
190 return 0
193 if __name__ == '__main__':
194 try:
195 rtn = main(sys.argv[1:])
196 except Error, e:
197 sys.stderr.write('%s: %s\n' % (os.path.basename(__file__), e))
198 rtn = 1
199 except KeyboardInterrupt:
200 sys.stderr.write('%s: interrupted\n' % os.path.basename(__file__))
201 rtn = 1
202 sys.exit(rtn)