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 fname, piotool.SRC_BUILD_EXT + piotool.SRC_HEADER_EXT
): ):
continue continue
content = fs.get_file_contents(join(root, fname)) with open(join(root, fname)) as fp:
content = fp.read()
if not content: if not content:
continue continue
if "Arduino.h" in content and include_re.search(content): 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 import makedirs
from os.path import isdir, isfile, join from os.path import isdir, isfile, join
from platformio import fs
from platformio.compat import WINDOWS, hashlib_encode_data from platformio.compat import WINDOWS, hashlib_encode_data
# Windows CLI has limit with command length to 8192 # Windows CLI has limit with command length to 8192
@ -67,7 +66,8 @@ def _file_long_data(env, data):
) )
if isfile(tmp_file): if isfile(tmp_file):
return 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 return tmp_file

View File

@ -252,12 +252,14 @@ def is_prog_obsolete(prog_path):
break break
shasum.update(data) shasum.update(data)
new_digest = shasum.hexdigest() new_digest = shasum.hexdigest()
old_digest = ( old_digest = None
fs.get_file_contents(prog_hash_path) if isfile(prog_hash_path) else None if isfile(prog_hash_path):
) with open(prog_hash_path) as fp:
old_digest = fp.read()
if new_digest == old_digest: if new_digest == old_digest:
return False 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 return True

View File

@ -15,6 +15,7 @@
from __future__ import absolute_import from __future__ import absolute_import
import glob import glob
import io
import os import os
import shutil import shutil
from functools import cmp_to_key from functools import cmp_to_key
@ -66,7 +67,8 @@ class OSRPC(object):
if uri.startswith("http"): if uri.startswith("http"):
return self.fetch_content(uri, data, headers, cache_valid) return self.fetch_content(uri, data, headers, cache_valid)
if os.path.isfile(uri): 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 return None
@staticmethod @staticmethod

View File

@ -244,7 +244,8 @@ class ProjectRPC(object):
return project_dir return project_dir
if not os.path.isdir(src_dir): if not os.path.isdir(src_dir):
os.makedirs(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 return project_dir
def import_arduino(self, board, use_arduino_libs, arduino_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): def init_include_readme(include_dir):
fs.write_file_contents( with open(os.path.join(include_dir, "README"), "w") as fp:
os.path.join(include_dir, "README"), fp.write(
""" """
This directory is intended for project header files. This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions 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 https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
""", """,
) )
def init_lib_readme(lib_dir): def init_lib_readme(lib_dir):
# pylint: disable=line-too-long # pylint: disable=line-too-long
fs.write_file_contents( with open(os.path.join(lib_dir, "README"), "w") as fp:
os.path.join(lib_dir, "README"), fp.write(
""" """
This directory is intended for project specific (private) libraries. This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file. 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 More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html - https://docs.platformio.org/page/librarymanager/ldf.html
""", """,
) )
def init_test_readme(test_dir): def init_test_readme(test_dir):
fs.write_file_contents( with open(os.path.join(test_dir, "README"), "w") as fp:
os.path.join(test_dir, "README"), fp.write(
""" """
This directory is intended for PIO Unit Testing and project tests. This directory is intended for PIO Unit Testing and project tests.
Unit Testing is a software testing method by which individual units of 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: More information about PIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html - https://docs.platformio.org/page/plus/unit-testing.html
""", """,
) )
def init_ci_conf(project_dir): def init_ci_conf(project_dir):
conf_path = os.path.join(project_dir, ".travis.yml") conf_path = os.path.join(project_dir, ".travis.yml")
if os.path.isfile(conf_path): if os.path.isfile(conf_path):
return return
fs.write_file_contents( with open(conf_path, "w") as fp:
conf_path, fp.write(
"""# Continuous Integration (CI) is the practice, in software """# Continuous Integration (CI) is the practice, in software
# engineering, of merging all developer working copies with a shared mainline # engineering, of merging all developer working copies with a shared mainline
# several times a day < https://docs.platformio.org/page/ci/index.html > # several times a day < https://docs.platformio.org/page/ci/index.html >
# #
@ -378,14 +378,15 @@ def init_ci_conf(project_dir):
# script: # script:
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N # - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
""", """,
) )
def init_cvs_ignore(project_dir): def init_cvs_ignore(project_dir):
conf_path = os.path.join(project_dir, ".gitignore") conf_path = os.path.join(project_dir, ".gitignore")
if os.path.isfile(conf_path): if os.path.isfile(conf_path):
return return
fs.write_file_contents(conf_path, ".pio\n") with open(conf_path, "w") as fp:
fp.write(".pio\n")
def fill_project_envs( def fill_project_envs(

View File

@ -222,7 +222,8 @@ def device_monitor(ctx, **kwargs):
sleep(0.1) sleep(0.1)
if not t.is_alive(): if not t.is_alive():
return 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) ctx.invoke(device.device_monitor, **kwargs)
t.join(2) t.join(2)
finally: finally:

View File

@ -53,9 +53,12 @@ def clean_build_dir(build_dir, config):
if isdir(build_dir): if isdir(build_dir):
# check project structure # check project structure
if isfile(checksum_file) and fs.get_file_contents(checksum_file) == checksum: if isfile(checksum_file):
return with open(checksum_file) as fp:
if fp.read() == checksum:
return
fs.rmtree(build_dir) fs.rmtree(build_dir)
makedirs(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 import click
from platformio import exception, fs from platformio import exception
TRANSPORT_OPTIONS = { TRANSPORT_OPTIONS = {
"arduino": { "arduino": {
@ -195,6 +195,7 @@ class TestProcessorBase(object):
data = Template(tpl).substitute(baudrate=self.get_baudrate()) data = Template(tpl).substitute(baudrate=self.get_baudrate())
tmp_file = join(test_dir, "output_export.cpp") 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) atexit.register(delete_tmptest_file, tmp_file)

View File

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