Remove absl from wasm_binary (#756)

* Remove absl from wasm_binary

* Formatting fixes

* Blank line, too

* Reorder imports
This commit is contained in:
Tim Talashok
2021-03-12 22:28:55 +01:00
committed by GitHub
parent 96d7ee1c5e
commit 1a3878716f

View File

@@ -18,17 +18,11 @@ This script and its accompanying Bazel rule should allow you to extract a
WebAssembly binary into a larger web application. WebAssembly binary into a larger web application.
""" """
import argparse
import os import os
import subprocess import subprocess
import sys import sys
from absl import app
from absl import flags
FLAGS = flags.FLAGS
flags.DEFINE_string('archive', None, 'The the archive to extract from.')
flags.DEFINE_string('output_path', None, 'The path to extract into.')
def ensure(f): def ensure(f):
if not os.path.exists(f): if not os.path.exists(f):
@@ -41,44 +35,49 @@ def check(f):
raise Exception('Expected file in archive: %s' % f) raise Exception('Expected file in archive: %s' % f)
def main(argv): def main():
basename = os.path.basename(FLAGS.archive) parser = argparse.ArgumentParser()
parser.add_argument('--archive', help='The archive to extract from.')
parser.add_argument('--output_path', help='The path to extract into.')
args = parser.parse_args()
basename = os.path.basename(args.archive)
stem = basename.split('.')[0] stem = basename.split('.')[0]
# Check the type of the input file # Check the type of the input file
mimetype_bytes = subprocess.check_output(['file', '-Lb', '--mime-type', '--mime-encoding', FLAGS.archive]) mimetype_bytes = subprocess.check_output(['file', '-Lb', '--mime-type', '--mime-encoding', args.archive])
mimetype = mimetype_bytes.decode(sys.stdout.encoding) mimetype = mimetype_bytes.decode(sys.stdout.encoding)
# If we have a tar, extract all files. If we have just a single file, copy it. # If we have a tar, extract all files. If we have just a single file, copy it.
if 'tar' in mimetype: if 'tar' in mimetype:
subprocess.check_call( subprocess.check_call(
['tar', 'xf', FLAGS.archive, '-C', FLAGS.output_path]) ['tar', 'xf', args.archive, '-C', args.output_path])
elif 'binary' in mimetype: elif 'binary' in mimetype:
subprocess.check_call([ subprocess.check_call([
'cp', 'cp',
FLAGS.archive, args.archive,
os.path.join(FLAGS.output_path, stem + '.wasm')]) os.path.join(args.output_path, stem + '.wasm')])
elif 'text' in mimetype: elif 'text' in mimetype:
subprocess.check_call([ subprocess.check_call([
'cp', 'cp',
FLAGS.archive, args.archive,
os.path.join(FLAGS.output_path, stem + '.js')]) os.path.join(args.output_path, stem + '.js')])
else: else:
subprocess.check_call(['cp', FLAGS.archive, FLAGS.output_path]) subprocess.check_call(['cp', args.archive, args.output_path])
# At least one of these two files should exist at this point. # At least one of these two files should exist at this point.
ensure(os.path.join(FLAGS.output_path, stem + '.js')) ensure(os.path.join(args.output_path, stem + '.js'))
ensure(os.path.join(FLAGS.output_path, stem + '.wasm')) ensure(os.path.join(args.output_path, stem + '.wasm'))
# And can optionally contain these extra files. # And can optionally contain these extra files.
ensure(os.path.join(FLAGS.output_path, stem + '.wasm.map')) ensure(os.path.join(args.output_path, stem + '.wasm.map'))
ensure(os.path.join(FLAGS.output_path, stem + '.worker.js')) ensure(os.path.join(args.output_path, stem + '.worker.js'))
ensure(os.path.join(FLAGS.output_path, stem + '.js.mem')) ensure(os.path.join(args.output_path, stem + '.js.mem'))
ensure(os.path.join(FLAGS.output_path, stem + '.data')) ensure(os.path.join(args.output_path, stem + '.data'))
ensure(os.path.join(FLAGS.output_path, stem + '.fetch.js')) ensure(os.path.join(args.output_path, stem + '.fetch.js'))
ensure(os.path.join(FLAGS.output_path, stem + '.js.symbols')) ensure(os.path.join(args.output_path, stem + '.js.symbols'))
ensure(os.path.join(FLAGS.output_path, stem + '.wasm.debug.wasm')) ensure(os.path.join(args.output_path, stem + '.wasm.debug.wasm'))
if __name__ == '__main__': if __name__ == '__main__':
app.run(main) main()