Commit 323de0c9 authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

IDL compiler: Let web_idl.file_io make intermediate directory

web_idl.file_io reads/writes in binary mode so that it produces
the same result on Windows.  Also makes intermediate directories
if necessary.

Bug: 839389
Change-Id: I9743af13acfaa0cefaed6f4b76d6fac28ae3e608
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1980606Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#728413}
parent 445113d3
...@@ -156,5 +156,4 @@ def write_code_node_to_file(code_node, filepath): ...@@ -156,5 +156,4 @@ def write_code_node_to_file(code_node, filepath):
format_result = clang_format(rendered_text, filename=filepath) format_result = clang_format(rendered_text, filename=filepath)
with open(filepath, "w") as output_file: web_idl.file_io.write_to_file_if_changed(filepath, format_result.contents)
output_file.write(format_result.contents)
...@@ -29,6 +29,7 @@ def _setup_sys_path(): ...@@ -29,6 +29,7 @@ def _setup_sys_path():
_setup_sys_path() _setup_sys_path()
from . import file_io
from .ast_group import AstGroup from .ast_group import AstGroup
from .attribute import Attribute from .attribute import Attribute
from .callback_function import CallbackFunction from .callback_function import CallbackFunction
......
...@@ -10,7 +10,7 @@ def read_pickle_file(filepath): ...@@ -10,7 +10,7 @@ def read_pickle_file(filepath):
""" """
Reads the content of the file as a pickled object. Reads the content of the file as a pickled object.
""" """
with open(filepath, 'r') as file_obj: with open(filepath, 'rb') as file_obj:
return pickle.load(file_obj) return pickle.load(file_obj)
...@@ -30,7 +30,7 @@ def write_to_file_if_changed(filepath, contents): ...@@ -30,7 +30,7 @@ def write_to_file_if_changed(filepath, contents):
Returns True if the data is written to the file, and False if skipped. Returns True if the data is written to the file, and False if skipped.
""" """
try: try:
with open(filepath, 'r') as file_obj: with open(filepath, 'rb') as file_obj:
old_contents = file_obj.read() old_contents = file_obj.read()
except (OSError, EnvironmentError): except (OSError, EnvironmentError):
pass pass
...@@ -38,6 +38,10 @@ def write_to_file_if_changed(filepath, contents): ...@@ -38,6 +38,10 @@ def write_to_file_if_changed(filepath, contents):
if contents == old_contents: if contents == old_contents:
return False return False
os.remove(filepath) os.remove(filepath)
with open(filepath, 'w') as file_obj:
if not os.path.exists(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
with open(filepath, 'wb') as file_obj:
file_obj.write(contents) file_obj.write(contents)
return True return True
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