Commit cdbdbc5a authored by Joel Hockey's avatar Joel Hockey Committed by Chromium LUCI CQ

Files app: Fix create_test_main.py to run as python3

Python3 does automatic utf-8 decode/encode when reading/writing files
and it throws an exception when reading binary files in normal 'r' mode.
When we detect such an error, we will read and write the file as binary
'rb' / 'wb' without doing any text substitutions.

The script is tested locally and works for python2 and python3

Bug: 1163386
Change-Id: I46f53dcafbdb43a367e5418bab84cf7a0ebd84a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2617408
Commit-Queue: Joel Hockey <joelhockey@chromium.org>
Auto-Submit: Joel Hockey <joelhockey@chromium.org>
Reviewed-by: default avatarLuciano Pacheco <lucmult@chromium.org>
Cr-Commit-Position: refs/heads/master@{#841385}
parent 31d0cfee
......@@ -4,7 +4,7 @@
import("//third_party/closure_compiler/compile_js.gni")
python2_action("create_test_main") {
action("create_test_main") {
script = "//ui/file_manager/file_manager/test/scripts/create_test_main.py"
output = "$target_gen_dir/../test.html"
sources = [
......
......@@ -45,16 +45,16 @@ GENERATED = 'Generated at %s by: %s' % (time.ctime(), sys.path[0])
GENERATED_HTML = '<!-- %s -->\n\n' % GENERATED
def read(path):
with open(os.path.join(SRC, path)) as f:
def read(path, mode='r'):
with open(os.path.join(SRC, path), mode) as f:
return f.read()
def write(path, content):
def write(path, content, mode='w'):
fullpath = os.path.join(GEN, path)
if not os.path.exists(os.path.dirname(fullpath)):
os.makedirs(os.path.dirname(fullpath))
with open(fullpath, 'w') as f:
with open(fullpath, mode) as f:
f.write(content)
......@@ -130,7 +130,11 @@ def copyresources(src_dir, dst_dir):
srcf = os.path.join(root[len(SRC):], f)
dstf = R_GEN + dst_dir + srcf[len(src_dir):]
relpath = os.path.relpath(R_GEN, os.path.dirname(dstf)) + '/'
write(dstf, i18n(read(srcf).replace('chrome://resources/', relpath)))
try:
write(dstf, i18n(read(srcf).replace('chrome://resources/', relpath)))
except UnicodeDecodeError:
# Binary files get utf-8 codec errors in py3, copy them as binary.
write(dstf, read(srcf, 'rb'), 'wb')
# Copy any files required in chrome://resources/... into test/gen/resources.
copyresources('ui/webui/resources/', '')
......@@ -256,5 +260,5 @@ for filename, substitutions in (
main_html = replaceline(main_html, filename,
['<script src="test/gen/%s"></script>' % filename])
test_html = GENERATED_HTML + '\n'.join(main_html).encode('utf-8')
write('test.html', test_html)
test_html = (GENERATED_HTML + '\n'.join(main_html)).encode('utf-8')
write('test.html', test_html, 'wb')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment