Skip to content

Commit

Permalink
Cleanup file_packager python code (emscripten-core#10177)
Browse files Browse the repository at this point in the history
Also clean up the test code (test change should be NFC).
  • Loading branch information
sbc100 authored Jan 9, 2020
1 parent fd717f0 commit 5b2fc36
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
18 changes: 11 additions & 7 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from __future__ import print_function
from functools import wraps
import filecmp
import glob
import itertools
import json
Expand Down Expand Up @@ -2543,21 +2542,26 @@ def test_file_packager(self):
self.assertNotContained('below the current directory', proc2.stderr)

def clean(txt):
return [line for line in txt.split('\n') if 'PACKAGE_UUID' not in line and 'loadPackage({' not in line]
lines = txt.splitlines()
lines = [l for l in lines if 'PACKAGE_UUID' not in l and 'loadPackage({' not in l]
return ''.join(lines)

assert clean(proc.stdout) == clean(proc2.stdout)
self.assertTextDataIdentical(clean(proc.stdout), clean(proc2.stdout))

# verify '--separate-metadata' option produces separate metadata file
os.chdir('..')

run_process([PYTHON, FILE_PACKAGER, 'test.data', '--preload', 'data1.txt', '--preload', 'subdir/data2.txt', '--js-output=immutable.js', '--separate-metadata'])
assert os.path.isfile('immutable.js.metadata')
# verify js output file is immutable when metadata is separated
self.assertExists('immutable.js.metadata')
# verify js output JS file is not touched when the metadata is separated
shutil.copy2('immutable.js', 'immutable.js.copy') # copy with timestamp preserved
# ensure some time passes before running the packager again so that if it does touch the
# js file it will end up with the different timestamp.
time.sleep(1.0)
run_process([PYTHON, FILE_PACKAGER, 'test.data', '--preload', 'data1.txt', '--preload', 'subdir/data2.txt', '--js-output=immutable.js', '--separate-metadata'])
assert filecmp.cmp('immutable.js.copy', 'immutable.js')
# assert both file content and timestamp are the same as reference copy
self.assertEqual(str(os.path.getmtime('immutable.js.copy')), str(os.path.getmtime('immutable.js')))
self.assertTextDataIdentical(open('immutable.js.copy').read(), open('immutable.js').read())
self.assertEqual(os.path.getmtime('immutable.js.copy'), os.path.getmtime('immutable.js'))
# verify the content of metadata file is correct
with open('immutable.js.metadata') as f:
metadata = json.load(f)
Expand Down
43 changes: 21 additions & 22 deletions tools/file_packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ def main():
from_emcc = True
leading = ''
elif arg.startswith('--plugin'):
plugin = open(arg.split('=', 1)[1], 'r').read()
with open(arg.split('=', 1)[1]) as f:
plugin = f.read()
eval(plugin) # should append itself to plugins
leading = ''
elif leading == 'preload' or leading == 'embed':
Expand Down Expand Up @@ -391,17 +392,18 @@ def was_seen(name):
if has_preloaded:
# Bundle all datafiles into one archive. Avoids doing lots of simultaneous
# XHRs which has overhead.
data = open(data_target, 'wb')
start = 0
for file_ in data_files:
file_['data_start'] = start
curr = open(file_['srcpath'], 'rb').read()
file_['data_end'] = start + len(curr)
if AV_WORKAROUND:
curr += '\x00'
start += len(curr)
data.write(curr)
data.close()
with open(data_target, 'wb') as data:
for file_ in data_files:
file_['data_start'] = start
with open(file_['srcpath'], 'rb') as f:
curr = f.read()
file_['data_end'] = start + len(curr)
if AV_WORKAROUND:
curr += '\x00'
start += len(curr)
data.write(curr)

# TODO: sha256sum on data_target
if start > 256 * 1024 * 1024:
print('warning: file packager is creating an asset bundle of %d MB. '
Expand Down Expand Up @@ -913,20 +915,17 @@ def was_seen(name):
# differs from the current generated one, otherwise leave the file
# untouched preserving its old timestamp
if os.path.isfile(jsoutput):
f = open(jsoutput, 'r+')
old = f.read()
with open(jsoutput) as f:
old = f.read()
if old != ret:
f.seek(0)
f.write(ret)
f.truncate()
with open(jsoutput, 'w') as f:
f.write(ret)
else:
f = open(jsoutput, 'w')
f.write(ret)
f.close()
with open(jsoutput, 'w') as f:
f.write(ret)
if separate_metadata:
f = open(jsoutput + '.metadata', 'w')
json.dump(metadata, f, separators=(',', ':'))
f.close()
with open(jsoutput + '.metadata', 'w') as f:
json.dump(metadata, f, separators=(',', ':'))

return 0

Expand Down

0 comments on commit 5b2fc36

Please sign in to comment.