Use native open/io.open for file contents reading/writing

This commit is contained in:
Ivan Kravets
2020-03-05 23:52:13 +02:00
parent 3275bb59bf
commit ce6b96ea84
10 changed files with 56 additions and 39 deletions

View File

@ -82,7 +82,8 @@ class LibBuilderFactory(object):
fname, piotool.SRC_BUILD_EXT + piotool.SRC_HEADER_EXT
):
continue
content = fs.get_file_contents(join(root, fname))
with open(join(root, fname)) as fp:
content = fp.read()
if not content:
continue
if "Arduino.h" in content and include_re.search(content):

View File

@ -18,7 +18,6 @@ from hashlib import md5
from os import makedirs
from os.path import isdir, isfile, join
from platformio import fs
from platformio.compat import WINDOWS, hashlib_encode_data
# Windows CLI has limit with command length to 8192
@ -67,7 +66,8 @@ def _file_long_data(env, data):
)
if isfile(tmp_file):
return tmp_file
fs.write_file_contents(tmp_file, data)
with open(tmp_file, "w") as fp:
fp.write(data)
return tmp_file

View File

@ -252,12 +252,14 @@ def is_prog_obsolete(prog_path):
break
shasum.update(data)
new_digest = shasum.hexdigest()
old_digest = (
fs.get_file_contents(prog_hash_path) if isfile(prog_hash_path) else None
)
old_digest = None
if isfile(prog_hash_path):
with open(prog_hash_path) as fp:
old_digest = fp.read()
if new_digest == old_digest:
return False
fs.write_file_contents(prog_hash_path, new_digest)
with open(prog_hash_path, "w") as fp:
fp.write(new_digest)
return True

View File

@ -15,6 +15,7 @@
from __future__ import absolute_import
import glob
import io
import os
import shutil
from functools import cmp_to_key
@ -66,7 +67,8 @@ class OSRPC(object):
if uri.startswith("http"):
return self.fetch_content(uri, data, headers, cache_valid)
if os.path.isfile(uri):
return fs.get_file_contents(uri, encoding="utf8")
with io.open(uri, encoding="utf-8") as fp:
return fp.read()
return None
@staticmethod

View File

@ -244,7 +244,8 @@ class ProjectRPC(object):
return project_dir
if not os.path.isdir(src_dir):
os.makedirs(src_dir)
fs.write_file_contents(main_path, main_content.strip())
with open(main_path, "w") as fp:
fp.write(main_content.strip())
return project_dir
def import_arduino(self, board, use_arduino_libs, arduino_project_dir):

View File

@ -187,9 +187,9 @@ def init_base_project(project_dir):
def init_include_readme(include_dir):
fs.write_file_contents(
os.path.join(include_dir, "README"),
"""
with open(os.path.join(include_dir, "README"), "w") as fp:
fp.write(
"""
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
@ -229,14 +229,14 @@ Read more about using header files in official GCC documentation:
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
""",
)
)
def init_lib_readme(lib_dir):
# pylint: disable=line-too-long
fs.write_file_contents(
os.path.join(lib_dir, "README"),
"""
with open(os.path.join(lib_dir, "README"), "w") as fp:
fp.write(
"""
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
@ -283,13 +283,13 @@ libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
""",
)
)
def init_test_readme(test_dir):
fs.write_file_contents(
os.path.join(test_dir, "README"),
"""
with open(os.path.join(test_dir, "README"), "w") as fp:
fp.write(
"""
This directory is intended for PIO Unit Testing and project tests.
Unit Testing is a software testing method by which individual units of
@ -301,16 +301,16 @@ in the development cycle.
More information about PIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html
""",
)
)
def init_ci_conf(project_dir):
conf_path = os.path.join(project_dir, ".travis.yml")
if os.path.isfile(conf_path):
return
fs.write_file_contents(
conf_path,
"""# Continuous Integration (CI) is the practice, in software
with open(conf_path, "w") as fp:
fp.write(
"""# Continuous Integration (CI) is the practice, in software
# engineering, of merging all developer working copies with a shared mainline
# several times a day < https://docs.platformio.org/page/ci/index.html >
#
@ -378,14 +378,15 @@ def init_ci_conf(project_dir):
# script:
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
""",
)
)
def init_cvs_ignore(project_dir):
conf_path = os.path.join(project_dir, ".gitignore")
if os.path.isfile(conf_path):
return
fs.write_file_contents(conf_path, ".pio\n")
with open(conf_path, "w") as fp:
fp.write(".pio\n")
def fill_project_envs(

View File

@ -222,7 +222,8 @@ def device_monitor(ctx, **kwargs):
sleep(0.1)
if not t.is_alive():
return
kwargs["port"] = fs.get_file_contents(sock_file)
with open(sock_file) as fp:
kwargs["port"] = fp.read()
ctx.invoke(device.device_monitor, **kwargs)
t.join(2)
finally:

View File

@ -53,9 +53,12 @@ def clean_build_dir(build_dir, config):
if isdir(build_dir):
# check project structure
if isfile(checksum_file) and fs.get_file_contents(checksum_file) == checksum:
return
if isfile(checksum_file):
with open(checksum_file) as fp:
if fp.read() == checksum:
return
fs.rmtree(build_dir)
makedirs(build_dir)
fs.write_file_contents(checksum_file, checksum)
with open(checksum_file, "w") as fp:
fp.write(checksum)

View File

@ -19,7 +19,7 @@ from string import Template
import click
from platformio import exception, fs
from platformio import exception
TRANSPORT_OPTIONS = {
"arduino": {
@ -195,6 +195,7 @@ class TestProcessorBase(object):
data = Template(tpl).substitute(baudrate=self.get_baudrate())
tmp_file = join(test_dir, "output_export.cpp")
fs.write_file_contents(tmp_file, data)
with open(tmp_file, "w") as fp:
fp.write(data)
atexit.register(delete_tmptest_file, tmp_file)

View File

@ -13,6 +13,7 @@
# limitations under the License.
import inspect
import io
import json
import os
import re
@ -21,7 +22,6 @@ import requests
from platformio import util
from platformio.compat import get_class_attributes, string_types
from platformio.fs import get_file_contents
from platformio.package.exception import ManifestParserError, UnknownManifestError
from platformio.project.helpers import is_platformio_project
@ -59,24 +59,29 @@ class ManifestFileType(object):
class ManifestParserFactory(object):
@staticmethod
def new_from_file(path, remote_url=False):
def read_manifest_contents(path):
with io.open(path, encoding="utf-8") as fp:
return fp.read()
@classmethod
def new_from_file(cls, path, remote_url=False):
if not path or not os.path.isfile(path):
raise UnknownManifestError("Manifest file does not exist %s" % path)
type_from_uri = ManifestFileType.from_uri(path)
if not type_from_uri:
raise UnknownManifestError("Unknown manifest file type %s" % path)
return ManifestParserFactory.new(
get_file_contents(path, encoding="utf8"), type_from_uri, remote_url
cls.read_manifest_contents(path), type_from_uri, remote_url
)
@staticmethod
def new_from_dir(path, remote_url=None):
@classmethod
def new_from_dir(cls, path, remote_url=None):
assert os.path.isdir(path), "Invalid directory %s" % path
type_from_uri = ManifestFileType.from_uri(remote_url) if remote_url else None
if type_from_uri and os.path.isfile(os.path.join(path, type_from_uri)):
return ManifestParserFactory.new(
get_file_contents(os.path.join(path, type_from_uri), encoding="utf8"),
cls.read_manifest_contents(os.path.join(path, type_from_uri)),
type_from_uri,
remote_url=remote_url,
package_dir=path,
@ -88,7 +93,7 @@ class ManifestParserFactory(object):
"Unknown manifest file type in %s directory" % path
)
return ManifestParserFactory.new(
get_file_contents(os.path.join(path, type_from_dir), encoding="utf8"),
cls.read_manifest_contents(os.path.join(path, type_from_dir)),
type_from_dir,
remote_url=remote_url,
package_dir=path,