Files
platformio-core/platformio/app.py

190 lines
5.5 KiB
Python
Raw Normal View History

2016-01-01 20:51:48 +02:00
# Copyright 2014-2016 Ivan Kravets <me@ikravets.com>
#
# 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.
2014-11-29 22:39:44 +02:00
import json
from copy import deepcopy
from os import environ, getenv
from os.path import getmtime, isfile, join
from time import time
2014-11-29 22:39:44 +02:00
from lockfile import LockFile
2014-11-29 22:39:44 +02:00
from platformio import __version__
from platformio.exception import InvalidSettingName, InvalidSettingValue
2015-07-30 17:33:45 +03:00
from platformio.util import get_home_dir, is_ci
2014-11-29 22:39:44 +02:00
DEFAULT_SETTINGS = {
"check_platformio_interval": {
"description": "Check for the new PlatformIO interval (days)",
"value": 3
},
"check_platforms_interval": {
"description": "Check for the platform updates interval (days)",
"value": 7
},
"check_libraries_interval": {
"description": "Check for the library updates interval (days)",
"value": 7
},
"auto_update_platforms": {
"description": "Automatically update platforms (Yes/No)",
"value": False
2014-11-29 22:39:44 +02:00
},
"auto_update_libraries": {
"description": "Automatically update libraries (Yes/No)",
"value": False
2014-11-29 22:39:44 +02:00
},
"enable_telemetry": {
"description": (
2015-12-14 11:30:39 +02:00
"Telemetry service <http://docs.platformio.org/en/latest/"
"userguide/cmd_settings.html?#enable-telemetry> (Yes/No)"),
2014-11-29 22:39:44 +02:00
"value": True
},
"enable_prompts": {
"description": (
"Can PlatformIO communicate with you via prompts: "
"propose to install platforms which aren't installed yet, "
"paginate over library search results and etc.)? ATTENTION!!! "
"If you call PlatformIO like subprocess, "
"please disable prompts to avoid blocking (Yes/No)"),
"value": True
2014-11-29 22:39:44 +02:00
}
}
SESSION_VARS = {
"command_ctx": None,
"force_option": False,
"caller_id": None
}
2014-11-29 22:39:44 +02:00
class State(object):
def __init__(self, path=None, lock=False):
2014-11-29 22:39:44 +02:00
self.path = path
self.lock = lock
2014-11-29 22:39:44 +02:00
if not self.path:
self.path = join(get_home_dir(), "appstate.json")
self._state = {}
self._prev_state = {}
self._lockfile = None
2014-11-29 22:39:44 +02:00
def __enter__(self):
try:
self._lock_state_file()
2014-11-29 22:39:44 +02:00
if isfile(self.path):
with open(self.path, "r") as fp:
self._state = json.load(fp)
except ValueError:
self._state = {}
self._prev_state = deepcopy(self._state)
2014-11-29 22:39:44 +02:00
return self._state
def __exit__(self, type_, value, traceback):
if self._prev_state != self._state:
with open(self.path, "w") as fp:
if "dev" in __version__:
json.dump(self._state, fp, indent=4)
else:
json.dump(self._state, fp)
self._unlock_state_file()
def _lock_state_file(self):
if not self.lock:
return
self._lockfile = LockFile(self.path)
if (self._lockfile.is_locked() and
(time() - getmtime(self._lockfile.lock_file)) > 10):
self._lockfile.break_lock()
self._lockfile.acquire()
def _unlock_state_file(self):
if self._lockfile:
self._lockfile.release()
2014-11-29 22:39:44 +02:00
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
2014-11-29 22:39:44 +02:00
def get_state_item(name, default=None):
with State() as data:
return data.get(name, default)
def set_state_item(name, value):
with State(lock=True) as data:
2014-11-29 22:39:44 +02:00
data[name] = value
def get_setting(name):
if name == "enable_prompts":
# disable prompts for Continuous Integration systems
# and when global "--force" option is set
2015-07-30 17:33:45 +03:00
if any([is_ci(), get_session_var("force_option")]):
return False
_env_name = "PLATFORMIO_SETTING_%s" % name.upper()
if _env_name in environ:
return sanitize_setting(name, getenv(_env_name))
2014-11-29 22:39:44 +02:00
with State() as data:
if "settings" in data and name in data['settings']:
return data['settings'][name]
return DEFAULT_SETTINGS[name]['value']
def set_setting(name, value):
with State(lock=True) as data:
2014-11-29 22:39:44 +02:00
if "settings" not in data:
data['settings'] = {}
data['settings'][name] = sanitize_setting(name, value)
2014-11-29 22:39:44 +02:00
def reset_settings():
with State(lock=True) as data:
2014-11-29 22:39:44 +02:00
if "settings" in data:
del data['settings']
def get_session_var(name, default=None):
return SESSION_VARS.get(name, default)
def set_session_var(name, value):
assert name in SESSION_VARS
SESSION_VARS[name] = value
def is_disabled_progressbar():
return is_ci() or getenv("PLATFORMIO_DISABLE_PROGRESSBAR") == "true"