mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 18:17:13 +02:00
Refactor to os.path
This commit is contained in:
@ -23,7 +23,6 @@ import io
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from os.path import basename, commonprefix, isdir, isfile, join, realpath, sep
|
|
||||||
|
|
||||||
import click
|
import click
|
||||||
import SCons.Scanner # pylint: disable=import-error
|
import SCons.Scanner # pylint: disable=import-error
|
||||||
@ -49,7 +48,7 @@ class LibBuilderFactory(object):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def new(env, path, verbose=int(ARGUMENTS.get("PIOVERBOSE", 0))):
|
def new(env, path, verbose=int(ARGUMENTS.get("PIOVERBOSE", 0))):
|
||||||
clsname = "UnknownLibBuilder"
|
clsname = "UnknownLibBuilder"
|
||||||
if isfile(join(path, "library.json")):
|
if os.path.isfile(os.path.join(path, "library.json")):
|
||||||
clsname = "PlatformIOLibBuilder"
|
clsname = "PlatformIOLibBuilder"
|
||||||
else:
|
else:
|
||||||
used_frameworks = LibBuilderFactory.get_used_frameworks(env, path)
|
used_frameworks = LibBuilderFactory.get_used_frameworks(env, path)
|
||||||
@ -66,12 +65,12 @@ class LibBuilderFactory(object):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def get_used_frameworks(env, path):
|
def get_used_frameworks(env, path):
|
||||||
if any(
|
if any(
|
||||||
isfile(join(path, fname))
|
os.path.isfile(os.path.join(path, fname))
|
||||||
for fname in ("library.properties", "keywords.txt")
|
for fname in ("library.properties", "keywords.txt")
|
||||||
):
|
):
|
||||||
return ["arduino"]
|
return ["arduino"]
|
||||||
|
|
||||||
if isfile(join(path, "module.json")):
|
if os.path.isfile(os.path.join(path, "module.json")):
|
||||||
return ["mbed"]
|
return ["mbed"]
|
||||||
|
|
||||||
include_re = re.compile(
|
include_re = re.compile(
|
||||||
@ -87,7 +86,7 @@ class LibBuilderFactory(object):
|
|||||||
fname, piotool.SRC_BUILD_EXT + piotool.SRC_HEADER_EXT
|
fname, piotool.SRC_BUILD_EXT + piotool.SRC_HEADER_EXT
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
with io.open(join(root, fname), errors="ignore") as fp:
|
with io.open(os.path.join(root, fname), errors="ignore") as fp:
|
||||||
content = fp.read()
|
content = fp.read()
|
||||||
if not content:
|
if not content:
|
||||||
continue
|
continue
|
||||||
@ -114,7 +113,7 @@ class LibBuilderBase(object):
|
|||||||
def __init__(self, env, path, manifest=None, verbose=False):
|
def __init__(self, env, path, manifest=None, verbose=False):
|
||||||
self.env = env.Clone()
|
self.env = env.Clone()
|
||||||
self.envorigin = env.Clone()
|
self.envorigin = env.Clone()
|
||||||
self.path = realpath(env.subst(path))
|
self.path = os.path.realpath(env.subst(path))
|
||||||
self.verbose = verbose
|
self.verbose = verbose
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -148,11 +147,11 @@ class LibBuilderBase(object):
|
|||||||
p2 = p2.lower()
|
p2 = p2.lower()
|
||||||
if p1 == p2:
|
if p1 == p2:
|
||||||
return True
|
return True
|
||||||
return commonprefix((p1 + sep, p2)) == p1 + sep
|
return os.path.commonprefix((p1 + os.path.sep, p2)) == p1 + os.path.sep
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
return self._manifest.get("name", basename(self.path))
|
return self._manifest.get("name", os.path.basename(self.path))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def version(self):
|
def version(self):
|
||||||
@ -173,13 +172,19 @@ class LibBuilderBase(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def include_dir(self):
|
def include_dir(self):
|
||||||
if not all(isdir(join(self.path, d)) for d in ("include", "src")):
|
if not all(
|
||||||
|
os.path.isdir(os.path.join(self.path, d)) for d in ("include", "src")
|
||||||
|
):
|
||||||
return None
|
return None
|
||||||
return join(self.path, "include")
|
return os.path.join(self.path, "include")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def src_dir(self):
|
def src_dir(self):
|
||||||
return join(self.path, "src") if isdir(join(self.path, "src")) else self.path
|
return (
|
||||||
|
os.path.join(self.path, "src")
|
||||||
|
if os.path.isdir(os.path.join(self.path, "src"))
|
||||||
|
else self.path
|
||||||
|
)
|
||||||
|
|
||||||
def get_include_dirs(self):
|
def get_include_dirs(self):
|
||||||
items = []
|
items = []
|
||||||
@ -192,7 +197,9 @@ class LibBuilderBase(object):
|
|||||||
@property
|
@property
|
||||||
def build_dir(self):
|
def build_dir(self):
|
||||||
lib_hash = hashlib.sha1(hashlib_encode_data(self.path)).hexdigest()[:3]
|
lib_hash = hashlib.sha1(hashlib_encode_data(self.path)).hexdigest()[:3]
|
||||||
return join("$BUILD_DIR", "lib%s" % lib_hash, basename(self.path))
|
return os.path.join(
|
||||||
|
"$BUILD_DIR", "lib%s" % lib_hash, os.path.basename(self.path)
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def build_flags(self):
|
def build_flags(self):
|
||||||
@ -271,7 +278,7 @@ class LibBuilderBase(object):
|
|||||||
if self.extra_script:
|
if self.extra_script:
|
||||||
self.env.SConscriptChdir(1)
|
self.env.SConscriptChdir(1)
|
||||||
self.env.SConscript(
|
self.env.SConscript(
|
||||||
realpath(self.extra_script),
|
os.path.realpath(self.extra_script),
|
||||||
exports={"env": self.env, "pio_lib_builder": self},
|
exports={"env": self.env, "pio_lib_builder": self},
|
||||||
)
|
)
|
||||||
self.env.ProcessUnFlags(self.build_unflags)
|
self.env.ProcessUnFlags(self.build_unflags)
|
||||||
@ -297,14 +304,14 @@ class LibBuilderBase(object):
|
|||||||
|
|
||||||
def get_search_files(self):
|
def get_search_files(self):
|
||||||
items = [
|
items = [
|
||||||
join(self.src_dir, item)
|
os.path.join(self.src_dir, item)
|
||||||
for item in self.env.MatchSourceFiles(self.src_dir, self.src_filter)
|
for item in self.env.MatchSourceFiles(self.src_dir, self.src_filter)
|
||||||
]
|
]
|
||||||
include_dir = self.include_dir
|
include_dir = self.include_dir
|
||||||
if include_dir:
|
if include_dir:
|
||||||
items.extend(
|
items.extend(
|
||||||
[
|
[
|
||||||
join(include_dir, item)
|
os.path.join(include_dir, item)
|
||||||
for item in self.env.MatchSourceFiles(include_dir)
|
for item in self.env.MatchSourceFiles(include_dir)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@ -373,7 +380,7 @@ class LibBuilderBase(object):
|
|||||||
continue
|
continue
|
||||||
_f_part = _h_path[: _h_path.rindex(".")]
|
_f_part = _h_path[: _h_path.rindex(".")]
|
||||||
for ext in piotool.SRC_C_EXT + piotool.SRC_CXX_EXT:
|
for ext in piotool.SRC_C_EXT + piotool.SRC_CXX_EXT:
|
||||||
if not isfile("%s.%s" % (_f_part, ext)):
|
if not os.path.isfile("%s.%s" % (_f_part, ext)):
|
||||||
continue
|
continue
|
||||||
_c_path = self.env.File("%s.%s" % (_f_part, ext))
|
_c_path = self.env.File("%s.%s" % (_f_part, ext))
|
||||||
if _c_path not in result:
|
if _c_path not in result:
|
||||||
@ -467,23 +474,23 @@ class UnknownLibBuilder(LibBuilderBase):
|
|||||||
|
|
||||||
class ArduinoLibBuilder(LibBuilderBase):
|
class ArduinoLibBuilder(LibBuilderBase):
|
||||||
def load_manifest(self):
|
def load_manifest(self):
|
||||||
manifest_path = join(self.path, "library.properties")
|
manifest_path = os.path.join(self.path, "library.properties")
|
||||||
if not isfile(manifest_path):
|
if not os.path.isfile(manifest_path):
|
||||||
return {}
|
return {}
|
||||||
return ManifestParserFactory.new_from_file(manifest_path).as_dict()
|
return ManifestParserFactory.new_from_file(manifest_path).as_dict()
|
||||||
|
|
||||||
def get_include_dirs(self):
|
def get_include_dirs(self):
|
||||||
include_dirs = LibBuilderBase.get_include_dirs(self)
|
include_dirs = LibBuilderBase.get_include_dirs(self)
|
||||||
if isdir(join(self.path, "src")):
|
if os.path.isdir(os.path.join(self.path, "src")):
|
||||||
return include_dirs
|
return include_dirs
|
||||||
if isdir(join(self.path, "utility")):
|
if os.path.isdir(os.path.join(self.path, "utility")):
|
||||||
include_dirs.append(join(self.path, "utility"))
|
include_dirs.append(os.path.join(self.path, "utility"))
|
||||||
return include_dirs
|
return include_dirs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def src_filter(self):
|
def src_filter(self):
|
||||||
src_dir = join(self.path, "src")
|
src_dir = os.path.join(self.path, "src")
|
||||||
if isdir(src_dir):
|
if os.path.isdir(src_dir):
|
||||||
# pylint: disable=no-member
|
# pylint: disable=no-member
|
||||||
src_filter = LibBuilderBase.src_filter.fget(self)
|
src_filter = LibBuilderBase.src_filter.fget(self)
|
||||||
for root, _, files in os.walk(src_dir, followlinks=True):
|
for root, _, files in os.walk(src_dir, followlinks=True):
|
||||||
@ -495,20 +502,20 @@ class ArduinoLibBuilder(LibBuilderBase):
|
|||||||
if not found:
|
if not found:
|
||||||
continue
|
continue
|
||||||
rel_path = root.replace(src_dir, "")
|
rel_path = root.replace(src_dir, "")
|
||||||
if rel_path.startswith(sep):
|
if rel_path.startswith(os.path.sep):
|
||||||
rel_path = rel_path[1:] + sep
|
rel_path = rel_path[1:] + os.path.sep
|
||||||
src_filter.append("-<%s*.[aA][sS][mM]>" % rel_path)
|
src_filter.append("-<%s*.[aA][sS][mM]>" % rel_path)
|
||||||
return src_filter
|
return src_filter
|
||||||
|
|
||||||
src_filter = []
|
src_filter = []
|
||||||
is_utility = isdir(join(self.path, "utility"))
|
is_utility = os.path.isdir(os.path.join(self.path, "utility"))
|
||||||
for ext in piotool.SRC_BUILD_EXT + piotool.SRC_HEADER_EXT:
|
for ext in piotool.SRC_BUILD_EXT + piotool.SRC_HEADER_EXT:
|
||||||
# arduino ide ignores files with .asm or .ASM extensions
|
# arduino ide ignores files with .asm or .ASM extensions
|
||||||
if ext.lower() == "asm":
|
if ext.lower() == "asm":
|
||||||
continue
|
continue
|
||||||
src_filter.append("+<*.%s>" % ext)
|
src_filter.append("+<*.%s>" % ext)
|
||||||
if is_utility:
|
if is_utility:
|
||||||
src_filter.append("+<utility%s*.%s>" % (sep, ext))
|
src_filter.append("+<utility%s*.%s>" % (os.path.sep, ext))
|
||||||
return src_filter
|
return src_filter
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -541,21 +548,21 @@ class ArduinoLibBuilder(LibBuilderBase):
|
|||||||
|
|
||||||
class MbedLibBuilder(LibBuilderBase):
|
class MbedLibBuilder(LibBuilderBase):
|
||||||
def load_manifest(self):
|
def load_manifest(self):
|
||||||
manifest_path = join(self.path, "module.json")
|
manifest_path = os.path.join(self.path, "module.json")
|
||||||
if not isfile(manifest_path):
|
if not os.path.isfile(manifest_path):
|
||||||
return {}
|
return {}
|
||||||
return ManifestParserFactory.new_from_file(manifest_path).as_dict()
|
return ManifestParserFactory.new_from_file(manifest_path).as_dict()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def include_dir(self):
|
def include_dir(self):
|
||||||
if isdir(join(self.path, "include")):
|
if os.path.isdir(os.path.join(self.path, "include")):
|
||||||
return join(self.path, "include")
|
return os.path.join(self.path, "include")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def src_dir(self):
|
def src_dir(self):
|
||||||
if isdir(join(self.path, "source")):
|
if os.path.isdir(os.path.join(self.path, "source")):
|
||||||
return join(self.path, "source")
|
return os.path.join(self.path, "source")
|
||||||
return LibBuilderBase.src_dir.fget(self) # pylint: disable=no-member
|
return LibBuilderBase.src_dir.fget(self) # pylint: disable=no-member
|
||||||
|
|
||||||
def get_include_dirs(self):
|
def get_include_dirs(self):
|
||||||
@ -565,13 +572,13 @@ class MbedLibBuilder(LibBuilderBase):
|
|||||||
|
|
||||||
# library with module.json
|
# library with module.json
|
||||||
for p in self._manifest.get("extraIncludes", []):
|
for p in self._manifest.get("extraIncludes", []):
|
||||||
include_dirs.append(join(self.path, p))
|
include_dirs.append(os.path.join(self.path, p))
|
||||||
|
|
||||||
# old mbed library without manifest, add to CPPPATH all folders
|
# old mbed library without manifest, add to CPPPATH all folders
|
||||||
if not self._manifest:
|
if not self._manifest:
|
||||||
for root, _, __ in os.walk(self.path):
|
for root, _, __ in os.walk(self.path):
|
||||||
part = root.replace(self.path, "").lower()
|
part = root.replace(self.path, "").lower()
|
||||||
if any(s in part for s in ("%s." % sep, "test", "example")):
|
if any(s in part for s in ("%s." % os.path.sep, "test", "example")):
|
||||||
continue
|
continue
|
||||||
if root not in include_dirs:
|
if root not in include_dirs:
|
||||||
include_dirs.append(root)
|
include_dirs.append(root)
|
||||||
@ -587,7 +594,7 @@ class MbedLibBuilder(LibBuilderBase):
|
|||||||
|
|
||||||
def _process_mbed_lib_confs(self):
|
def _process_mbed_lib_confs(self):
|
||||||
mbed_lib_paths = [
|
mbed_lib_paths = [
|
||||||
join(root, "mbed_lib.json")
|
os.path.join(root, "mbed_lib.json")
|
||||||
for root, _, files in os.walk(self.path)
|
for root, _, files in os.walk(self.path)
|
||||||
if "mbed_lib.json" in files
|
if "mbed_lib.json" in files
|
||||||
]
|
]
|
||||||
@ -596,8 +603,8 @@ class MbedLibBuilder(LibBuilderBase):
|
|||||||
|
|
||||||
mbed_config_path = None
|
mbed_config_path = None
|
||||||
for p in self.env.get("CPPPATH"):
|
for p in self.env.get("CPPPATH"):
|
||||||
mbed_config_path = join(self.env.subst(p), "mbed_config.h")
|
mbed_config_path = os.path.join(self.env.subst(p), "mbed_config.h")
|
||||||
if isfile(mbed_config_path):
|
if os.path.isfile(mbed_config_path):
|
||||||
break
|
break
|
||||||
mbed_config_path = None
|
mbed_config_path = None
|
||||||
if not mbed_config_path:
|
if not mbed_config_path:
|
||||||
@ -689,26 +696,26 @@ class MbedLibBuilder(LibBuilderBase):
|
|||||||
|
|
||||||
class PlatformIOLibBuilder(LibBuilderBase):
|
class PlatformIOLibBuilder(LibBuilderBase):
|
||||||
def load_manifest(self):
|
def load_manifest(self):
|
||||||
manifest_path = join(self.path, "library.json")
|
manifest_path = os.path.join(self.path, "library.json")
|
||||||
if not isfile(manifest_path):
|
if not os.path.isfile(manifest_path):
|
||||||
return {}
|
return {}
|
||||||
return ManifestParserFactory.new_from_file(manifest_path).as_dict()
|
return ManifestParserFactory.new_from_file(manifest_path).as_dict()
|
||||||
|
|
||||||
def _has_arduino_manifest(self):
|
def _has_arduino_manifest(self):
|
||||||
return isfile(join(self.path, "library.properties"))
|
return os.path.isfile(os.path.join(self.path, "library.properties"))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def include_dir(self):
|
def include_dir(self):
|
||||||
if "includeDir" in self._manifest.get("build", {}):
|
if "includeDir" in self._manifest.get("build", {}):
|
||||||
with fs.cd(self.path):
|
with fs.cd(self.path):
|
||||||
return realpath(self._manifest.get("build").get("includeDir"))
|
return os.path.realpath(self._manifest.get("build").get("includeDir"))
|
||||||
return LibBuilderBase.include_dir.fget(self) # pylint: disable=no-member
|
return LibBuilderBase.include_dir.fget(self) # pylint: disable=no-member
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def src_dir(self):
|
def src_dir(self):
|
||||||
if "srcDir" in self._manifest.get("build", {}):
|
if "srcDir" in self._manifest.get("build", {}):
|
||||||
with fs.cd(self.path):
|
with fs.cd(self.path):
|
||||||
return realpath(self._manifest.get("build").get("srcDir"))
|
return os.path.realpath(self._manifest.get("build").get("srcDir"))
|
||||||
return LibBuilderBase.src_dir.fget(self) # pylint: disable=no-member
|
return LibBuilderBase.src_dir.fget(self) # pylint: disable=no-member
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -786,10 +793,10 @@ class PlatformIOLibBuilder(LibBuilderBase):
|
|||||||
if (
|
if (
|
||||||
"build" not in self._manifest
|
"build" not in self._manifest
|
||||||
and self._has_arduino_manifest()
|
and self._has_arduino_manifest()
|
||||||
and not isdir(join(self.path, "src"))
|
and not os.path.isdir(os.path.join(self.path, "src"))
|
||||||
and isdir(join(self.path, "utility"))
|
and os.path.isdir(os.path.join(self.path, "utility"))
|
||||||
):
|
):
|
||||||
include_dirs.append(join(self.path, "utility"))
|
include_dirs.append(os.path.join(self.path, "utility"))
|
||||||
|
|
||||||
for path in self.env.get("CPPPATH", []):
|
for path in self.env.get("CPPPATH", []):
|
||||||
if path not in self.envorigin.get("CPPPATH", []):
|
if path not in self.envorigin.get("CPPPATH", []):
|
||||||
@ -808,7 +815,7 @@ class ProjectAsLibBuilder(LibBuilderBase):
|
|||||||
@property
|
@property
|
||||||
def include_dir(self):
|
def include_dir(self):
|
||||||
include_dir = self.env.subst("$PROJECT_INCLUDE_DIR")
|
include_dir = self.env.subst("$PROJECT_INCLUDE_DIR")
|
||||||
return include_dir if isdir(include_dir) else None
|
return include_dir if os.path.isdir(include_dir) else None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def src_dir(self):
|
def src_dir(self):
|
||||||
@ -817,7 +824,7 @@ class ProjectAsLibBuilder(LibBuilderBase):
|
|||||||
def get_include_dirs(self):
|
def get_include_dirs(self):
|
||||||
include_dirs = []
|
include_dirs = []
|
||||||
project_include_dir = self.env.subst("$PROJECT_INCLUDE_DIR")
|
project_include_dir = self.env.subst("$PROJECT_INCLUDE_DIR")
|
||||||
if isdir(project_include_dir):
|
if os.path.isdir(project_include_dir):
|
||||||
include_dirs.append(project_include_dir)
|
include_dirs.append(project_include_dir)
|
||||||
for include_dir in LibBuilderBase.get_include_dirs(self):
|
for include_dir in LibBuilderBase.get_include_dirs(self):
|
||||||
if include_dir not in include_dirs:
|
if include_dir not in include_dirs:
|
||||||
@ -831,7 +838,7 @@ class ProjectAsLibBuilder(LibBuilderBase):
|
|||||||
if "__test" in COMMAND_LINE_TARGETS:
|
if "__test" in COMMAND_LINE_TARGETS:
|
||||||
items.extend(
|
items.extend(
|
||||||
[
|
[
|
||||||
join("$PROJECT_TEST_DIR", item)
|
os.path.join("$PROJECT_TEST_DIR", item)
|
||||||
for item in self.env.MatchSourceFiles(
|
for item in self.env.MatchSourceFiles(
|
||||||
"$PROJECT_TEST_DIR", "$PIOTEST_SRC_FILTER"
|
"$PROJECT_TEST_DIR", "$PIOTEST_SRC_FILTER"
|
||||||
)
|
)
|
||||||
@ -884,7 +891,7 @@ class ProjectAsLibBuilder(LibBuilderBase):
|
|||||||
|
|
||||||
did_install = False
|
did_install = False
|
||||||
lm = LibraryPackageManager(
|
lm = LibraryPackageManager(
|
||||||
self.env.subst(join("$PROJECT_LIBDEPS_DIR", "$PIOENV"))
|
self.env.subst(os.path.join("$PROJECT_LIBDEPS_DIR", "$PIOENV"))
|
||||||
)
|
)
|
||||||
for spec in not_found_specs:
|
for spec in not_found_specs:
|
||||||
try:
|
try:
|
||||||
@ -975,12 +982,12 @@ def GetLibBuilders(env): # pylint: disable=too-many-branches
|
|||||||
found_incompat = False
|
found_incompat = False
|
||||||
|
|
||||||
for storage_dir in env.GetLibSourceDirs():
|
for storage_dir in env.GetLibSourceDirs():
|
||||||
storage_dir = realpath(storage_dir)
|
storage_dir = os.path.realpath(storage_dir)
|
||||||
if not isdir(storage_dir):
|
if not os.path.isdir(storage_dir):
|
||||||
continue
|
continue
|
||||||
for item in sorted(os.listdir(storage_dir)):
|
for item in sorted(os.listdir(storage_dir)):
|
||||||
lib_dir = join(storage_dir, item)
|
lib_dir = os.path.join(storage_dir, item)
|
||||||
if item == "__cores__" or not isdir(lib_dir):
|
if item == "__cores__" or not os.path.isdir(lib_dir):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
lb = LibBuilderFactory.new(env, lib_dir)
|
lb = LibBuilderFactory.new(env, lib_dir)
|
||||||
|
Reference in New Issue
Block a user