feat(tools): Enforce utf-8 encoding with open() function

This commit is contained in:
Marek Fiala
2024-07-23 15:59:09 +02:00
committed by BOT
parent 305f1c1e5b
commit 2c814ef2fa
40 changed files with 115 additions and 124 deletions

View File

@@ -1575,7 +1575,7 @@ class ENVState:
if cls.deactivate_file_path:
try:
with open(cls.deactivate_file_path, 'r') as fp:
with open(cls.deactivate_file_path, 'r', encoding='utf-8') as fp:
env_state_obj.idf_variables = json.load(fp)
except (IOError, OSError, ValueError):
pass
@@ -1585,7 +1585,7 @@ class ENVState:
try:
if self.deactivate_file_path and os.path.basename(self.deactivate_file_path).endswith(f'idf_{str(os.getppid())}'):
# If exported file path/name exists and belongs to actual opened shell
with open(self.deactivate_file_path, 'w') as w:
with open(self.deactivate_file_path, 'w', encoding='utf-8') as w:
json.dump(self.idf_variables, w, ensure_ascii=False, indent=4) # type: ignore
else:
with tempfile.NamedTemporaryFile(delete=False, suffix=f'idf_{str(os.getppid())}') as fp:
@@ -1604,7 +1604,7 @@ def load_tools_info() -> Dict[str, IDFTool]:
tool_versions_file_name = g.tools_json
with open(tool_versions_file_name, 'r') as f: # type: ignore
with open(tool_versions_file_name, 'r', encoding='utf-8') as f: # type: ignore
tools_info = json.load(f)
return parse_tools_info_json(tools_info) # type: ignore
@@ -1666,7 +1666,7 @@ def get_idf_version() -> str:
version_file_path = os.path.join(g.idf_path, 'version.txt')
if os.path.exists(version_file_path):
with open(version_file_path, 'r') as version_file:
with open(version_file_path, 'r', encoding='utf-8') as version_file:
idf_version_str = version_file.read()
match = re.match(r'^v([0-9]+\.[0-9]+).*', idf_version_str)
@@ -1675,7 +1675,7 @@ def get_idf_version() -> str:
if idf_version is None:
try:
with open(os.path.join(g.idf_path, 'components', 'esp_common', 'include', 'esp_idf_version.h')) as f:
with open(os.path.join(g.idf_path, 'components', 'esp_common', 'include', 'esp_idf_version.h'), encoding='utf-8') as f:
m = re.search(r'^#define\s+ESP_IDF_VERSION_MAJOR\s+(\d+).+?^#define\s+ESP_IDF_VERSION_MINOR\s+(\d+)',
f.read(), re.DOTALL | re.MULTILINE)
if m:
@@ -2136,7 +2136,7 @@ def process_tool(
def check_python_venv_compatibility(idf_python_env_path: str, idf_version: str) -> None:
try:
with open(os.path.join(idf_python_env_path, VENV_VER_FILE), 'r') as f:
with open(os.path.join(idf_python_env_path, VENV_VER_FILE), 'r', encoding='utf-8') as f:
read_idf_version = f.read().strip()
if read_idf_version != idf_version:
fatal(f'Python environment is set to {idf_python_env_path} which was generated for '
@@ -2643,7 +2643,7 @@ def action_install_python_env(args): # type: ignore
stdout=sys.stdout, stderr=sys.stderr)
try:
with open(os.path.join(idf_python_env_path, VENV_VER_FILE), 'w') as f:
with open(os.path.join(idf_python_env_path, VENV_VER_FILE), 'w', encoding='utf-8') as f:
f.write(idf_version)
except OSError as e:
warn(f'The following issue occurred while generating the ESP-IDF version file in the Python environment: {e}. '
@@ -2781,7 +2781,7 @@ class ChecksumFileParser():
sha256_file = sha256_file_tmp
download(url, sha256_file)
with open(sha256_file, 'r') as f:
with open(sha256_file, 'r', encoding='utf-8') as f:
self.checksum = f.read().splitlines()
# remove temp file
@@ -2867,7 +2867,7 @@ def action_add_version(args: Any) -> None:
json_str = dump_tools_json(tools_info)
if not args.output:
args.output = os.path.join(g.idf_path, TOOLS_FILE_NEW) # type: ignore
with open(args.output, 'w') as f:
with open(args.output, 'w', encoding='utf-8') as f:
f.write(json_str)
f.write('\n')
info(f'Wrote output to {args.output}')
@@ -2881,7 +2881,7 @@ def action_rewrite(args): # type: ignore
json_str = dump_tools_json(tools_info)
if not args.output:
args.output = os.path.join(g.idf_path, TOOLS_FILE_NEW)
with open(args.output, 'w') as f:
with open(args.output, 'w', encoding='utf-8') as f:
f.write(json_str)
f.write('\n')
info(f'Wrote output to {args.output}')
@@ -2974,10 +2974,10 @@ def action_validate(args): # type: ignore
fatal('You need to install jsonschema package to use validate command')
raise SystemExit(1)
with open(os.path.join(g.idf_path, TOOLS_FILE), 'r') as tools_file:
with open(os.path.join(g.idf_path, TOOLS_FILE), 'r', encoding='utf-8') as tools_file:
tools_json = json.load(tools_file)
with open(os.path.join(g.idf_path, TOOLS_SCHEMA_FILE), 'r') as schema_file:
with open(os.path.join(g.idf_path, TOOLS_SCHEMA_FILE), 'r', encoding='utf-8') as schema_file:
schema_json = json.load(schema_file)
jsonschema.validate(tools_json, schema_json)
# on failure, this will raise an exception with a fairly verbose diagnostic message