2016-08-03 22:18:51 +03:00
|
|
|
# Copyright 2014-present PlatformIO <contact@platformio.org>
|
2015-11-18 17:16:17 +02:00
|
|
|
#
|
|
|
|
# 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
|
2015-11-25 19:54:06 +02:00
|
|
|
from copy import deepcopy
|
2015-03-05 01:36:31 +02:00
|
|
|
from os import environ, getenv
|
2015-09-23 16:35:31 +03:00
|
|
|
from os.path import getmtime, isfile, join
|
|
|
|
from time import time
|
2014-11-29 22:39:44 +02:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
from lockfile import LockFile
|
|
|
|
|
2016-04-09 14:15:59 +03:00
|
|
|
from platformio import __version__, util
|
2014-11-29 22:39:44 +02:00
|
|
|
from platformio.exception import InvalidSettingName, InvalidSettingValue
|
|
|
|
|
|
|
|
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)",
|
2015-04-17 12:28:10 +01:00
|
|
|
"value": False
|
2014-11-29 22:39:44 +02:00
|
|
|
},
|
|
|
|
"auto_update_libraries": {
|
|
|
|
"description": "Automatically update libraries (Yes/No)",
|
2015-04-17 12:28:10 +01:00
|
|
|
"value": False
|
2016-07-17 00:48:59 +03:00
|
|
|
},
|
|
|
|
"force_verbose": {
|
|
|
|
"description": "Force verbose output when processing environments",
|
|
|
|
"value": False
|
2014-11-29 22:39:44 +02:00
|
|
|
},
|
|
|
|
"enable_telemetry": {
|
2016-08-03 23:38:20 +03:00
|
|
|
"description":
|
|
|
|
("Telemetry service <http://docs.platformio.org/en/stable/"
|
|
|
|
"userguide/cmd_settings.html?#enable-telemetry> (Yes/No)"),
|
2014-11-29 22:39:44 +02:00
|
|
|
"value": True
|
2014-12-10 22:32:22 +02:00
|
|
|
},
|
|
|
|
"enable_prompts": {
|
2016-08-03 23:38:20 +03:00
|
|
|
"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)"),
|
2014-12-10 22:32:22 +02:00
|
|
|
"value": True
|
2014-11-29 22:39:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 23:38:20 +03:00
|
|
|
SESSION_VARS = {"command_ctx": None, "force_option": False, "caller_id": None}
|
2015-04-16 17:04:45 +01:00
|
|
|
|
|
|
|
|
2014-11-29 22:39:44 +02:00
|
|
|
class State(object):
|
|
|
|
|
2015-11-25 19:54:06 +02:00
|
|
|
def __init__(self, path=None, lock=False):
|
2014-11-29 22:39:44 +02:00
|
|
|
self.path = path
|
2015-11-25 19:54:06 +02:00
|
|
|
self.lock = lock
|
2014-11-29 22:39:44 +02:00
|
|
|
if not self.path:
|
2016-04-09 14:15:59 +03:00
|
|
|
self.path = join(util.get_home_dir(), "appstate.json")
|
2014-11-29 22:39:44 +02:00
|
|
|
self._state = {}
|
2015-09-03 19:04:09 +03:00
|
|
|
self._prev_state = {}
|
2015-11-25 19:54:06 +02:00
|
|
|
self._lockfile = None
|
2014-11-29 22:39:44 +02:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
try:
|
2015-09-23 16:35:31 +03:00
|
|
|
self._lock_state_file()
|
2014-11-29 22:39:44 +02:00
|
|
|
if isfile(self.path):
|
2016-04-09 14:15:59 +03:00
|
|
|
self._state = util.load_json(self.path)
|
2014-11-29 22:39:44 +02:00
|
|
|
except ValueError:
|
|
|
|
self._state = {}
|
2015-11-25 19:54:06 +02:00
|
|
|
self._prev_state = deepcopy(self._state)
|
2014-11-29 22:39:44 +02:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def __exit__(self, type_, value, traceback):
|
2015-09-03 19:04:09 +03:00
|
|
|
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)
|
2015-09-23 16:35:31 +03:00
|
|
|
self._unlock_state_file()
|
|
|
|
|
|
|
|
def _lock_state_file(self):
|
2015-11-25 19:54:06 +02:00
|
|
|
if not self.lock:
|
|
|
|
return
|
|
|
|
self._lockfile = LockFile(self.path)
|
2015-09-23 16:35:31 +03:00
|
|
|
|
2016-08-03 23:38:20 +03:00
|
|
|
if self._lockfile.is_locked() and \
|
|
|
|
(time() - getmtime(self._lockfile.lock_file)) > 10:
|
2015-11-25 19:54:06 +02:00
|
|
|
self._lockfile.break_lock()
|
2015-09-23 16:35:31 +03:00
|
|
|
|
2015-11-25 19:54:06 +02:00
|
|
|
self._lockfile.acquire()
|
2015-09-23 16:35:31 +03:00
|
|
|
|
|
|
|
def _unlock_state_file(self):
|
2015-11-25 19:54:06 +02:00
|
|
|
if self._lockfile:
|
|
|
|
self._lockfile.release()
|
2014-11-29 22:39:44 +02:00
|
|
|
|
|
|
|
|
2015-03-05 01:36:31 +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):
|
2015-11-25 19:54:06 +02:00
|
|
|
with State(lock=True) as data:
|
2014-11-29 22:39:44 +02:00
|
|
|
data[name] = value
|
|
|
|
|
|
|
|
|
|
|
|
def get_setting(name):
|
2015-04-16 17:04:45 +01:00
|
|
|
if name == "enable_prompts":
|
|
|
|
# disable prompts for Continuous Integration systems
|
|
|
|
# and when global "--force" option is set
|
2016-04-09 14:15:59 +03:00
|
|
|
if any([util.is_ci(), get_session_var("force_option")]):
|
2015-04-16 17:04:45 +01:00
|
|
|
return False
|
2015-03-05 14:48:56 +02:00
|
|
|
|
2015-03-05 01:36:31 +02:00
|
|
|
_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):
|
2015-11-25 19:54:06 +02:00
|
|
|
with State(lock=True) as data:
|
2014-11-29 22:39:44 +02:00
|
|
|
if "settings" not in data:
|
|
|
|
data['settings'] = {}
|
2015-03-05 01:36:31 +02:00
|
|
|
data['settings'][name] = sanitize_setting(name, value)
|
2014-11-29 22:39:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def reset_settings():
|
2015-11-25 19:54:06 +02:00
|
|
|
with State(lock=True) as data:
|
2014-11-29 22:39:44 +02:00
|
|
|
if "settings" in data:
|
|
|
|
del data['settings']
|
2015-04-16 17:04:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2016-01-24 16:45:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
def is_disabled_progressbar():
|
2016-03-21 14:17:04 +02:00
|
|
|
return (not get_setting("enable_prompts") or
|
|
|
|
getenv("PLATFORMIO_DISABLE_PROGRESSBAR") == "true")
|