Skip to content

Commit 5b2fc36

Browse files
authored
Cleanup file_packager python code (#10177)
Also clean up the test code (test change should be NFC).
1 parent fd717f0 commit 5b2fc36

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

tests/test_other.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from __future__ import print_function
1010
from functools import wraps
11-
import filecmp
1211
import glob
1312
import itertools
1413
import json
@@ -2543,21 +2542,26 @@ def test_file_packager(self):
25432542
self.assertNotContained('below the current directory', proc2.stderr)
25442543

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

2548-
assert clean(proc.stdout) == clean(proc2.stdout)
2549+
self.assertTextDataIdentical(clean(proc.stdout), clean(proc2.stdout))
25492550

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

25532554
run_process([PYTHON, FILE_PACKAGER, 'test.data', '--preload', 'data1.txt', '--preload', 'subdir/data2.txt', '--js-output=immutable.js', '--separate-metadata'])
2554-
assert os.path.isfile('immutable.js.metadata')
2555-
# verify js output file is immutable when metadata is separated
2555+
self.assertExists('immutable.js.metadata')
2556+
# verify js output JS file is not touched when the metadata is separated
25562557
shutil.copy2('immutable.js', 'immutable.js.copy') # copy with timestamp preserved
2558+
# ensure some time passes before running the packager again so that if it does touch the
2559+
# js file it will end up with the different timestamp.
2560+
time.sleep(1.0)
25572561
run_process([PYTHON, FILE_PACKAGER, 'test.data', '--preload', 'data1.txt', '--preload', 'subdir/data2.txt', '--js-output=immutable.js', '--separate-metadata'])
2558-
assert filecmp.cmp('immutable.js.copy', 'immutable.js')
25592562
# assert both file content and timestamp are the same as reference copy
2560-
self.assertEqual(str(os.path.getmtime('immutable.js.copy')), str(os.path.getmtime('immutable.js')))
2563+
self.assertTextDataIdentical(open('immutable.js.copy').read(), open('immutable.js').read())
2564+
self.assertEqual(os.path.getmtime('immutable.js.copy'), os.path.getmtime('immutable.js'))
25612565
# verify the content of metadata file is correct
25622566
with open('immutable.js.metadata') as f:
25632567
metadata = json.load(f)

tools/file_packager.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ def main():
232232
from_emcc = True
233233
leading = ''
234234
elif arg.startswith('--plugin'):
235-
plugin = open(arg.split('=', 1)[1], 'r').read()
235+
with open(arg.split('=', 1)[1]) as f:
236+
plugin = f.read()
236237
eval(plugin) # should append itself to plugins
237238
leading = ''
238239
elif leading == 'preload' or leading == 'embed':
@@ -391,17 +392,18 @@ def was_seen(name):
391392
if has_preloaded:
392393
# Bundle all datafiles into one archive. Avoids doing lots of simultaneous
393394
# XHRs which has overhead.
394-
data = open(data_target, 'wb')
395395
start = 0
396-
for file_ in data_files:
397-
file_['data_start'] = start
398-
curr = open(file_['srcpath'], 'rb').read()
399-
file_['data_end'] = start + len(curr)
400-
if AV_WORKAROUND:
401-
curr += '\x00'
402-
start += len(curr)
403-
data.write(curr)
404-
data.close()
396+
with open(data_target, 'wb') as data:
397+
for file_ in data_files:
398+
file_['data_start'] = start
399+
with open(file_['srcpath'], 'rb') as f:
400+
curr = f.read()
401+
file_['data_end'] = start + len(curr)
402+
if AV_WORKAROUND:
403+
curr += '\x00'
404+
start += len(curr)
405+
data.write(curr)
406+
405407
# TODO: sha256sum on data_target
406408
if start > 256 * 1024 * 1024:
407409
print('warning: file packager is creating an asset bundle of %d MB. '
@@ -913,20 +915,17 @@ def was_seen(name):
913915
# differs from the current generated one, otherwise leave the file
914916
# untouched preserving its old timestamp
915917
if os.path.isfile(jsoutput):
916-
f = open(jsoutput, 'r+')
917-
old = f.read()
918+
with open(jsoutput) as f:
919+
old = f.read()
918920
if old != ret:
919-
f.seek(0)
920-
f.write(ret)
921-
f.truncate()
921+
with open(jsoutput, 'w') as f:
922+
f.write(ret)
922923
else:
923-
f = open(jsoutput, 'w')
924-
f.write(ret)
925-
f.close()
924+
with open(jsoutput, 'w') as f:
925+
f.write(ret)
926926
if separate_metadata:
927-
f = open(jsoutput + '.metadata', 'w')
928-
json.dump(metadata, f, separators=(',', ':'))
929-
f.close()
927+
with open(jsoutput + '.metadata', 'w') as f:
928+
json.dump(metadata, f, separators=(',', ':'))
930929

931930
return 0
932931

0 commit comments

Comments
 (0)