2019-05-07 22:13:21 +03:00
|
|
|
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import os
|
|
|
|
from hashlib import sha1
|
|
|
|
from os import walk
|
2019-05-24 20:49:05 +03:00
|
|
|
from os.path import (abspath, dirname, expanduser, isdir, isfile, join,
|
|
|
|
splitdrive)
|
2019-05-07 22:13:21 +03:00
|
|
|
|
|
|
|
from platformio import __version__
|
2019-05-10 17:26:10 +03:00
|
|
|
from platformio.compat import PY2, WINDOWS
|
2019-05-07 22:13:21 +03:00
|
|
|
from platformio.project.config import ProjectConfig
|
|
|
|
|
|
|
|
|
|
|
|
def get_project_dir():
|
|
|
|
return os.getcwd()
|
|
|
|
|
|
|
|
|
|
|
|
def is_platformio_project(project_dir=None):
|
|
|
|
if not project_dir:
|
|
|
|
project_dir = get_project_dir()
|
|
|
|
return isfile(join(project_dir, "platformio.ini"))
|
|
|
|
|
|
|
|
|
|
|
|
def find_project_dir_above(path):
|
|
|
|
if isfile(path):
|
|
|
|
path = dirname(path)
|
|
|
|
if is_platformio_project(path):
|
|
|
|
return path
|
|
|
|
if isdir(dirname(path)):
|
|
|
|
return find_project_dir_above(dirname(path))
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_project_optional_dir(name, default=None):
|
|
|
|
paths = None
|
2019-05-20 17:07:59 +03:00
|
|
|
|
|
|
|
# check for system environment variable
|
2019-05-07 22:13:21 +03:00
|
|
|
var_name = "PLATFORMIO_%s" % name.upper()
|
|
|
|
if var_name in os.environ:
|
|
|
|
paths = os.getenv(var_name)
|
2019-05-20 17:07:59 +03:00
|
|
|
|
|
|
|
config = ProjectConfig.get_instance(
|
|
|
|
join(get_project_dir(), "platformio.ini"))
|
|
|
|
if (config.has_section("platformio")
|
|
|
|
and config.has_option("platformio", name)):
|
|
|
|
paths = config.get("platformio", name)
|
|
|
|
|
2019-05-07 22:13:21 +03:00
|
|
|
if not paths:
|
|
|
|
return default
|
|
|
|
|
|
|
|
items = []
|
|
|
|
for item in paths.split(", "):
|
|
|
|
if item.startswith("~"):
|
|
|
|
item = expanduser(item)
|
|
|
|
items.append(abspath(item))
|
|
|
|
paths = ", ".join(items)
|
|
|
|
|
|
|
|
while "$PROJECT_HASH" in paths:
|
|
|
|
project_dir = get_project_dir()
|
|
|
|
paths = paths.replace(
|
|
|
|
"$PROJECT_HASH",
|
|
|
|
sha1(project_dir if PY2 else project_dir.encode()).hexdigest()
|
|
|
|
[:10])
|
|
|
|
|
|
|
|
return paths
|
|
|
|
|
|
|
|
|
2019-05-24 20:49:05 +03:00
|
|
|
def get_project_core_dir():
|
|
|
|
core_dir = get_project_optional_dir(
|
|
|
|
"core_dir",
|
|
|
|
get_project_optional_dir("home_dir",
|
|
|
|
join(expanduser("~"), ".platformio")))
|
|
|
|
win_core_dir = None
|
|
|
|
if WINDOWS:
|
|
|
|
win_core_dir = splitdrive(core_dir)[0] + "\\.platformio"
|
|
|
|
if isdir(win_core_dir):
|
|
|
|
core_dir = win_core_dir
|
|
|
|
|
|
|
|
if not isdir(core_dir):
|
|
|
|
try:
|
|
|
|
os.makedirs(core_dir)
|
|
|
|
except: # pylint: disable=bare-except
|
|
|
|
if win_core_dir:
|
|
|
|
os.makedirs(win_core_dir)
|
|
|
|
core_dir = win_core_dir
|
|
|
|
|
|
|
|
assert isdir(core_dir)
|
|
|
|
return core_dir
|
|
|
|
|
|
|
|
|
|
|
|
def get_project_global_lib_dir():
|
|
|
|
return get_project_optional_dir("globallib_dir",
|
|
|
|
join(get_project_core_dir(), "lib"))
|
|
|
|
|
|
|
|
|
|
|
|
def get_project_platforms_dir():
|
|
|
|
return get_project_optional_dir("platforms_dir",
|
|
|
|
join(get_project_core_dir(), "platforms"))
|
|
|
|
|
|
|
|
|
|
|
|
def get_project_packages_dir():
|
|
|
|
return get_project_optional_dir("packages_dir",
|
|
|
|
join(get_project_core_dir(), "packages"))
|
|
|
|
|
|
|
|
|
|
|
|
def get_project_cache_dir():
|
|
|
|
return get_project_optional_dir("cache_dir",
|
|
|
|
join(get_project_core_dir(), ".cache"))
|
|
|
|
|
|
|
|
|
|
|
|
def get_project_workspace_dir():
|
2019-05-20 17:01:54 +03:00
|
|
|
return get_project_optional_dir("workspace_dir",
|
|
|
|
join(get_project_dir(), ".pio"))
|
|
|
|
|
|
|
|
|
2019-05-24 20:49:05 +03:00
|
|
|
def get_project_build_dir(force=False):
|
2019-05-20 17:01:54 +03:00
|
|
|
path = get_project_optional_dir("build_dir",
|
2019-05-24 20:49:05 +03:00
|
|
|
join(get_project_workspace_dir(), "build"))
|
2019-05-20 17:01:54 +03:00
|
|
|
try:
|
|
|
|
if not isdir(path):
|
|
|
|
os.makedirs(path)
|
|
|
|
except Exception as e: # pylint: disable=broad-except
|
|
|
|
if not force:
|
|
|
|
raise Exception(e)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2019-05-24 20:49:05 +03:00
|
|
|
def get_project_libdeps_dir():
|
2019-05-23 00:23:24 +03:00
|
|
|
return get_project_optional_dir(
|
2019-05-24 20:49:05 +03:00
|
|
|
"libdeps_dir", join(get_project_workspace_dir(), "libdeps"))
|
2019-05-07 22:13:21 +03:00
|
|
|
|
|
|
|
|
2019-05-24 20:49:05 +03:00
|
|
|
def get_project_lib_dir():
|
2019-05-23 00:23:24 +03:00
|
|
|
return get_project_optional_dir("lib_dir", join(get_project_dir(), "lib"))
|
2019-05-07 22:13:21 +03:00
|
|
|
|
|
|
|
|
2019-05-24 20:49:05 +03:00
|
|
|
def get_project_include_dir():
|
2019-05-07 22:13:21 +03:00
|
|
|
return get_project_optional_dir("include_dir",
|
|
|
|
join(get_project_dir(), "include"))
|
|
|
|
|
|
|
|
|
2019-05-24 20:49:05 +03:00
|
|
|
def get_project_src_dir():
|
|
|
|
return get_project_optional_dir("src_dir", join(get_project_dir(), "src"))
|
|
|
|
|
|
|
|
|
|
|
|
def get_project_test_dir():
|
2019-05-07 22:13:21 +03:00
|
|
|
return get_project_optional_dir("test_dir", join(get_project_dir(),
|
|
|
|
"test"))
|
|
|
|
|
|
|
|
|
2019-05-24 20:49:05 +03:00
|
|
|
def get_project_boards_dir():
|
2019-05-07 22:13:21 +03:00
|
|
|
return get_project_optional_dir("boards_dir",
|
|
|
|
join(get_project_dir(), "boards"))
|
|
|
|
|
|
|
|
|
2019-05-24 20:49:05 +03:00
|
|
|
def get_project_data_dir():
|
2019-05-07 22:13:21 +03:00
|
|
|
return get_project_optional_dir("data_dir", join(get_project_dir(),
|
|
|
|
"data"))
|
|
|
|
|
|
|
|
|
|
|
|
def calculate_project_hash():
|
|
|
|
check_suffixes = (".c", ".cc", ".cpp", ".h", ".hpp", ".s", ".S")
|
|
|
|
chunks = [__version__]
|
2019-05-24 20:49:05 +03:00
|
|
|
for d in (get_project_src_dir(), get_project_lib_dir()):
|
2019-05-07 22:13:21 +03:00
|
|
|
if not isdir(d):
|
|
|
|
continue
|
|
|
|
for root, _, files in walk(d):
|
|
|
|
for f in files:
|
|
|
|
path = join(root, f)
|
|
|
|
if path.endswith(check_suffixes):
|
|
|
|
chunks.append(path)
|
|
|
|
chunks_to_str = ",".join(sorted(chunks))
|
2019-05-10 17:26:10 +03:00
|
|
|
if WINDOWS:
|
2019-05-07 22:13:21 +03:00
|
|
|
# Fix issue with useless project rebuilding for case insensitive FS.
|
|
|
|
# A case of disk drive can differ...
|
|
|
|
chunks_to_str = chunks_to_str.lower()
|
|
|
|
return sha1(chunks_to_str if PY2 else chunks_to_str.encode()).hexdigest()
|