Specify open file encoding explicitly.
This fixes an error on some Windows installations:
```
fetch webrtc
Traceback (most recent call last):
File "googlesource.com\depot_tools\bootstrap\bootstrap.py", line 365, in <module>
sys.exit(main(sys.argv[1:]))
File "googlesource.com\depot_tools\bootstrap\bootstrap.py", line 324, in main
git_postprocess(template, os.path.join(bootstrap_dir, 'git'))
File "googlesource.com\depot_tools\bootstrap\bootstrap.py", line 264, in git_postprocess
maybe_copy(
File "googlesource.com\depot_tools\bootstrap\bootstrap.py", line 108, in maybe_copy
content = fd.read()
File "googlesource.com\depot_tools\bootstrap-3_8_0b1_chromium_1_bin\python3\bin\lib\encodings\cp1251.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 18734: character maps to <undefined>
```
Bug: None
Change-Id: I43cf7b51879ac9a66c33566536dcdcb4c93e0fc0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1881227
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
diff --git a/bootstrap/bootstrap.py b/bootstrap/bootstrap.py
index 533bc8f..3245df9 100644
--- a/bootstrap/bootstrap.py
+++ b/bootstrap/bootstrap.py
@@ -60,7 +60,7 @@
Returns (bool): True if |dst_path| was updated, False otherwise.
"""
template_path = os.path.join(THIS_DIR, name)
- with open(template_path, 'r') as fd:
+ with open(template_path, 'r', encoding='utf8') as fd:
t = string.Template(fd.read())
return maybe_update(t.safe_substitute(self._asdict()), dst_path)
@@ -82,12 +82,12 @@
# If the path already exists and matches the new content, refrain from writing
# a new one.
if os.path.exists(dst_path):
- with open(dst_path, 'r') as fd:
+ with open(dst_path, 'r', encoding='utf-8') as fd:
if fd.read() == content:
return False
logging.debug('Updating %r', dst_path)
- with open(dst_path, 'w') as fd:
+ with open(dst_path, 'w', encoding='utf-8') as fd:
fd.write(content)
os.chmod(dst_path, 0o755)
return True
@@ -104,7 +104,7 @@
Returns (bool): True if |dst_path| was updated, False otherwise.
"""
- with open(src_path, 'r') as fd:
+ with open(src_path, 'r', encoding='utf-8') as fd:
content = fd.read()
return maybe_update(content, dst_path)
@@ -130,14 +130,14 @@
stamp_version = stamp_version.strip()
if os.path.isfile(stamp_path):
- with open(stamp_path, 'r') as fd:
+ with open(stamp_path, 'r', encoding='utf-8') as fd:
current_version = fd.read().strip()
if current_version == stamp_version:
return False
fn()
- with open(stamp_path, 'w') as fd:
+ with open(stamp_path, 'w', encoding='utf-8') as fd:
fd.write(stamp_version)
return True