forked from platformio/platformio-core
Implemented PLATFORMIO_* environment variables
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
||||
# See LICENSE for details.
|
||||
|
||||
VERSION = (1, 0, "2.dev0")
|
||||
VERSION = (1, 1, "0.dev0")
|
||||
__version__ = ".".join([str(s) for s in VERSION])
|
||||
|
||||
__title__ = "platformio"
|
||||
|
||||
+23
-18
@@ -2,6 +2,7 @@
|
||||
# See LICENSE for details.
|
||||
|
||||
import json
|
||||
from os import environ, getenv
|
||||
from os.path import isfile, join
|
||||
|
||||
from platformio import __version__
|
||||
@@ -71,6 +72,24 @@ class State(object):
|
||||
json.dump(self._state, fp)
|
||||
|
||||
|
||||
def sanitize_setting(name, value):
|
||||
if name not in DEFAULT_SETTINGS:
|
||||
raise InvalidSettingName(name)
|
||||
|
||||
defdata = DEFAULT_SETTINGS[name]
|
||||
try:
|
||||
if "validator" in defdata:
|
||||
value = defdata['validator']()
|
||||
elif isinstance(defdata['value'], bool):
|
||||
if not isinstance(value, bool):
|
||||
value = str(value).lower() in ("true", "yes", "y", "1")
|
||||
elif isinstance(defdata['value'], int):
|
||||
value = int(value)
|
||||
except Exception:
|
||||
raise InvalidSettingValue(value, name)
|
||||
return value
|
||||
|
||||
|
||||
def get_state_item(name, default=None):
|
||||
with State() as data:
|
||||
return data.get(name, default)
|
||||
@@ -82,8 +101,9 @@ def set_state_item(name, value):
|
||||
|
||||
|
||||
def get_setting(name):
|
||||
if name not in DEFAULT_SETTINGS:
|
||||
raise InvalidSettingName(name)
|
||||
_env_name = "PLATFORMIO_SETTING_%s" % name.upper()
|
||||
if _env_name in environ:
|
||||
return sanitize_setting(name, getenv(_env_name))
|
||||
|
||||
with State() as data:
|
||||
if "settings" in data and name in data['settings']:
|
||||
@@ -93,25 +113,10 @@ def get_setting(name):
|
||||
|
||||
|
||||
def set_setting(name, value):
|
||||
if name not in DEFAULT_SETTINGS:
|
||||
raise InvalidSettingName(name)
|
||||
|
||||
defdata = DEFAULT_SETTINGS[name]
|
||||
try:
|
||||
if "validator" in defdata:
|
||||
value = defdata['validator']()
|
||||
elif isinstance(defdata['value'], bool):
|
||||
if not isinstance(value, bool):
|
||||
value = str(value).lower() in ("yes", "y", "1")
|
||||
elif isinstance(defdata['value'], int):
|
||||
value = int(value)
|
||||
except Exception:
|
||||
raise InvalidSettingValue(value, name)
|
||||
|
||||
with State() as data:
|
||||
if "settings" not in data:
|
||||
data['settings'] = {}
|
||||
data['settings'][name] = value
|
||||
data['settings'][name] = sanitize_setting(name, value)
|
||||
|
||||
|
||||
def reset_settings():
|
||||
|
||||
@@ -59,7 +59,7 @@ def BuildFirmware(env, corelibs):
|
||||
_LIBFLAGS=" -Wl,--end-group"
|
||||
)
|
||||
|
||||
firmenv.MergeFlags(getenv("PIOSRCBUILD_FLAGS", "$SRCBUILD_FLAGS"))
|
||||
firmenv.MergeFlags(getenv("PLATFORMIO_SRCBUILD_FLAGS", "$SRCBUILD_FLAGS"))
|
||||
|
||||
return firmenv.Program(
|
||||
join("$BUILD_DIR", "firmware"),
|
||||
|
||||
+24
-21
@@ -56,25 +56,29 @@ def get_systype():
|
||||
return ("%s_%s" % (data[0], data[4])).lower()
|
||||
|
||||
|
||||
def _get_projconf_option_dir(option_name):
|
||||
def _get_projconf_option_dir(name, default=None):
|
||||
_env_name = "PLATFORMIO_%s" % name.upper()
|
||||
if _env_name in os.environ:
|
||||
return os.getenv(_env_name)
|
||||
|
||||
try:
|
||||
config = get_project_config()
|
||||
if (config.has_section("platformio") and
|
||||
config.has_option("platformio", option_name)):
|
||||
option_dir = config.get("platformio", option_name)
|
||||
config.has_option("platformio", name)):
|
||||
option_dir = config.get("platformio", name)
|
||||
if option_dir.startswith("~"):
|
||||
option_dir = expanduser(option_dir)
|
||||
return abspath(option_dir)
|
||||
except exception.NotPlatformProject:
|
||||
pass
|
||||
return None
|
||||
return default
|
||||
|
||||
|
||||
def get_home_dir():
|
||||
home_dir = _get_projconf_option_dir("home_dir")
|
||||
|
||||
if not home_dir:
|
||||
home_dir = join(expanduser("~"), ".platformio")
|
||||
home_dir = _get_projconf_option_dir(
|
||||
"home_dir",
|
||||
join(expanduser("~"), ".platformio")
|
||||
)
|
||||
|
||||
if not isdir(home_dir):
|
||||
os.makedirs(home_dir)
|
||||
@@ -84,12 +88,10 @@ def get_home_dir():
|
||||
|
||||
|
||||
def get_lib_dir():
|
||||
lib_dir = _get_projconf_option_dir("lib_dir")
|
||||
|
||||
if not lib_dir:
|
||||
lib_dir = join(get_home_dir(), "lib")
|
||||
|
||||
return lib_dir
|
||||
return _get_projconf_option_dir(
|
||||
"lib_dir",
|
||||
join(get_home_dir(), "lib")
|
||||
)
|
||||
|
||||
|
||||
def get_source_dir():
|
||||
@@ -101,16 +103,17 @@ def get_project_dir():
|
||||
|
||||
|
||||
def get_projectsrc_dir():
|
||||
src_dir = _get_projconf_option_dir("src_dir")
|
||||
|
||||
if not src_dir:
|
||||
src_dir = join(get_project_dir(), "src")
|
||||
|
||||
return src_dir
|
||||
return _get_projconf_option_dir(
|
||||
"src_dir",
|
||||
join(get_project_dir(), "src")
|
||||
)
|
||||
|
||||
|
||||
def get_pioenvs_dir():
|
||||
return os.getenv("PIOENVS_DIR", join(get_project_dir(), ".pioenvs"))
|
||||
return _get_projconf_option_dir(
|
||||
"envs_dir",
|
||||
join(get_project_dir(), ".pioenvs")
|
||||
)
|
||||
|
||||
|
||||
def get_project_config():
|
||||
|
||||
Reference in New Issue
Block a user