2017-06-05 16:02:39 +03:00
|
|
|
# Copyright (c) 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
|
|
|
|
2020-08-14 16:48:12 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2020-04-19 19:26:56 +03:00
|
|
|
import getpass
|
2016-08-29 20:20:12 +03:00
|
|
|
import hashlib
|
2020-08-16 20:21:30 +03:00
|
|
|
import json
|
2016-09-14 19:06:22 +03:00
|
|
|
import os
|
2020-04-10 17:59:58 +03:00
|
|
|
import platform
|
2020-04-19 19:26:56 +03:00
|
|
|
import socket
|
2016-08-27 23:15:32 +03:00
|
|
|
import uuid
|
2019-11-15 16:02:15 +02:00
|
|
|
from os.path import dirname, isdir, isfile, join, realpath
|
2014-11-29 22:39:44 +02:00
|
|
|
|
2020-08-22 17:48:49 +03:00
|
|
|
from platformio import __version__, exception, fs, proc
|
2019-09-23 23:13:48 +03:00
|
|
|
from platformio.compat import WINDOWS, dump_json_to_unicode, hashlib_encode_data
|
2020-07-31 15:42:26 +03:00
|
|
|
from platformio.package.lockfile import LockFile
|
2020-08-22 20:05:14 +03:00
|
|
|
from platformio.project.helpers import get_default_projects_dir, get_project_core_dir
|
2019-07-08 17:21:28 +03:00
|
|
|
|
|
|
|
|
2017-12-14 16:52:13 +02:00
|
|
|
def projects_dir_validate(projects_dir):
|
|
|
|
assert isdir(projects_dir)
|
2019-11-15 16:02:15 +02:00
|
|
|
return realpath(projects_dir)
|
2017-12-14 16:52:13 +02:00
|
|
|
|
|
|
|
|
2014-11-29 22:39:44 +02:00
|
|
|
DEFAULT_SETTINGS = {
|
2017-12-14 16:52:13 +02:00
|
|
|
"auto_update_libraries": {
|
|
|
|
"description": "Automatically update libraries (Yes/No)",
|
2019-09-23 23:13:48 +03:00
|
|
|
"value": False,
|
2014-11-29 22:39:44 +02:00
|
|
|
},
|
2017-12-14 16:52:13 +02:00
|
|
|
"auto_update_platforms": {
|
|
|
|
"description": "Automatically update platforms (Yes/No)",
|
2019-09-23 23:13:48 +03:00
|
|
|
"value": False,
|
2014-11-29 22:39:44 +02:00
|
|
|
},
|
|
|
|
"check_libraries_interval": {
|
|
|
|
"description": "Check for the library updates interval (days)",
|
2019-09-23 23:13:48 +03:00
|
|
|
"value": 7,
|
2014-11-29 22:39:44 +02:00
|
|
|
},
|
2017-12-14 16:52:13 +02:00
|
|
|
"check_platformio_interval": {
|
|
|
|
"description": "Check for the new PlatformIO interval (days)",
|
2019-09-23 23:13:48 +03:00
|
|
|
"value": 3,
|
2014-11-29 22:39:44 +02:00
|
|
|
},
|
2017-12-14 16:52:13 +02:00
|
|
|
"check_platforms_interval": {
|
|
|
|
"description": "Check for the platform updates interval (days)",
|
2019-09-23 23:13:48 +03:00
|
|
|
"value": 7,
|
2016-07-17 00:48:59 +03:00
|
|
|
},
|
2021-01-24 17:21:22 +02:00
|
|
|
"check_prune_system_threshold": {
|
|
|
|
"description": "Check for pruning unnecessary data threshold (megabytes)",
|
|
|
|
"value": 1024,
|
|
|
|
},
|
2017-12-14 16:52:13 +02:00
|
|
|
"enable_cache": {
|
2020-08-22 20:05:14 +03:00
|
|
|
"description": "Enable caching for HTTP API requests",
|
2019-09-23 23:13:48 +03:00
|
|
|
"value": True,
|
2016-08-25 22:57:52 +03:00
|
|
|
},
|
2014-11-29 22:39:44 +02:00
|
|
|
"enable_telemetry": {
|
2019-09-23 23:13:48 +03:00
|
|
|
"description": ("Telemetry service <http://bit.ly/pio-telemetry> (Yes/No)"),
|
|
|
|
"value": True,
|
2017-12-14 16:52:13 +02:00
|
|
|
},
|
|
|
|
"force_verbose": {
|
|
|
|
"description": "Force verbose output when processing environments",
|
2019-09-23 23:13:48 +03:00
|
|
|
"value": False,
|
2017-12-14 16:52:13 +02:00
|
|
|
},
|
|
|
|
"projects_dir": {
|
2020-08-28 14:08:26 +03:00
|
|
|
"description": "Default location for PlatformIO projects (PlatformIO Home)",
|
2019-07-08 17:21:28 +03:00
|
|
|
"value": get_default_projects_dir(),
|
2019-09-23 23:13:48 +03:00
|
|
|
"validator": projects_dir_validate,
|
2017-12-14 16:52:13 +02:00
|
|
|
},
|
2014-11-29 22:39:44 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 14:13:53 +03:00
|
|
|
SESSION_VARS = {
|
|
|
|
"command_ctx": None,
|
|
|
|
"force_option": False,
|
|
|
|
"caller_id": None,
|
|
|
|
"custom_project_conf": 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:
|
2019-05-24 20:49:05 +03:00
|
|
|
self.path = join(get_project_core_dir(), "appstate.json")
|
2019-07-02 00:41:47 +03:00
|
|
|
self._storage = {}
|
2015-11-25 19:54:06 +02:00
|
|
|
self._lockfile = None
|
2019-07-02 15:52:12 +03:00
|
|
|
self.modified = False
|
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):
|
2019-08-12 19:44:37 +03:00
|
|
|
self._storage = fs.load_json(self.path)
|
2019-07-02 00:41:47 +03:00
|
|
|
assert isinstance(self._storage, dict)
|
2019-09-23 23:13:48 +03:00
|
|
|
except (
|
|
|
|
AssertionError,
|
|
|
|
ValueError,
|
|
|
|
UnicodeDecodeError,
|
|
|
|
exception.InvalidJSONFile,
|
|
|
|
):
|
2019-07-02 00:41:47 +03:00
|
|
|
self._storage = {}
|
|
|
|
return self
|
2014-11-29 22:39:44 +02:00
|
|
|
|
|
|
|
def __exit__(self, type_, value, traceback):
|
2019-07-02 15:52:12 +03:00
|
|
|
if self.modified:
|
2017-12-19 15:05:41 +02:00
|
|
|
try:
|
2019-06-05 17:57:22 +03:00
|
|
|
with open(self.path, "w") as fp:
|
2019-07-02 00:41:47 +03:00
|
|
|
fp.write(dump_json_to_unicode(self._storage))
|
2017-12-19 15:05:41 +02:00
|
|
|
except IOError:
|
2019-05-24 20:49:05 +03:00
|
|
|
raise exception.HomeDirPermissionsError(get_project_core_dir())
|
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
|
2020-07-31 15:42:26 +03:00
|
|
|
self._lockfile = LockFile(self.path)
|
2016-10-10 23:06:09 +03:00
|
|
|
try:
|
|
|
|
self._lockfile.acquire()
|
2018-07-14 22:10:56 +03:00
|
|
|
except IOError:
|
2017-12-23 19:48:16 +02:00
|
|
|
raise exception.HomeDirPermissionsError(dirname(self.path))
|
2015-09-23 16:35:31 +03:00
|
|
|
|
|
|
|
def _unlock_state_file(self):
|
2018-12-03 18:31:12 -08:00
|
|
|
if hasattr(self, "_lockfile") and self._lockfile:
|
2015-11-25 19:54:06 +02:00
|
|
|
self._lockfile.release()
|
2014-11-29 22:39:44 +02:00
|
|
|
|
2019-07-02 00:45:35 +03:00
|
|
|
def __del__(self):
|
|
|
|
self._unlock_state_file()
|
|
|
|
|
2019-07-02 00:41:47 +03:00
|
|
|
# Dictionary Proxy
|
|
|
|
|
|
|
|
def as_dict(self):
|
|
|
|
return self._storage
|
|
|
|
|
2020-07-31 15:42:26 +03:00
|
|
|
def keys(self):
|
|
|
|
return self._storage.keys()
|
|
|
|
|
2019-07-02 00:41:47 +03:00
|
|
|
def get(self, key, default=True):
|
|
|
|
return self._storage.get(key, default)
|
|
|
|
|
|
|
|
def update(self, *args, **kwargs):
|
2019-07-02 15:52:12 +03:00
|
|
|
self.modified = True
|
2019-07-02 00:41:47 +03:00
|
|
|
return self._storage.update(*args, **kwargs)
|
|
|
|
|
2019-07-16 14:15:48 +03:00
|
|
|
def clear(self):
|
|
|
|
return self._storage.clear()
|
|
|
|
|
2019-07-02 00:41:47 +03:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return self._storage[key]
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2019-07-02 15:52:12 +03:00
|
|
|
self.modified = True
|
2019-07-02 00:41:47 +03:00
|
|
|
self._storage[key] = value
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
2019-07-02 15:52:12 +03:00
|
|
|
self.modified = True
|
2019-07-02 00:41:47 +03:00
|
|
|
del self._storage[key]
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self._storage
|
2018-07-14 22:10:56 +03:00
|
|
|
|
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:
|
2017-12-19 15:05:41 +02:00
|
|
|
raise exception.InvalidSettingName(name)
|
2015-03-05 01:36:31 +02:00
|
|
|
|
|
|
|
defdata = DEFAULT_SETTINGS[name]
|
|
|
|
try:
|
|
|
|
if "validator" in defdata:
|
2019-09-23 23:13:48 +03:00
|
|
|
value = defdata["validator"](value)
|
|
|
|
elif isinstance(defdata["value"], bool):
|
2015-03-05 01:36:31 +02:00
|
|
|
if not isinstance(value, bool):
|
|
|
|
value = str(value).lower() in ("true", "yes", "y", "1")
|
2019-09-23 23:13:48 +03:00
|
|
|
elif isinstance(defdata["value"], int):
|
2015-03-05 01:36:31 +02:00
|
|
|
value = int(value)
|
|
|
|
except Exception:
|
2017-12-19 15:05:41 +02:00
|
|
|
raise exception.InvalidSettingValue(value, name)
|
2015-03-05 01:36:31 +02:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
2014-11-29 22:39:44 +02:00
|
|
|
def get_state_item(name, default=None):
|
2019-07-02 15:52:12 +03:00
|
|
|
with State() as state:
|
|
|
|
return state.get(name, default)
|
2014-11-29 22:39:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def set_state_item(name, value):
|
2019-07-02 15:52:12 +03:00
|
|
|
with State(lock=True) as state:
|
|
|
|
state[name] = value
|
|
|
|
state.modified = True
|
2014-11-29 22:39:44 +02:00
|
|
|
|
|
|
|
|
2017-05-26 00:45:56 +03:00
|
|
|
def delete_state_item(name):
|
2019-07-02 15:52:12 +03:00
|
|
|
with State(lock=True) as state:
|
|
|
|
if name in state:
|
|
|
|
del state[name]
|
2017-05-26 00:45:56 +03:00
|
|
|
|
|
|
|
|
2014-11-29 22:39:44 +02:00
|
|
|
def get_setting(name):
|
2015-03-05 01:36:31 +02:00
|
|
|
_env_name = "PLATFORMIO_SETTING_%s" % name.upper()
|
2020-08-22 20:05:14 +03:00
|
|
|
if _env_name in os.environ:
|
|
|
|
return sanitize_setting(name, os.getenv(_env_name))
|
2014-11-29 22:39:44 +02:00
|
|
|
|
2019-07-02 15:52:12 +03:00
|
|
|
with State() as state:
|
2019-09-23 23:13:48 +03:00
|
|
|
if "settings" in state and name in state["settings"]:
|
|
|
|
return state["settings"][name]
|
2014-11-29 22:39:44 +02:00
|
|
|
|
2019-09-23 23:13:48 +03:00
|
|
|
return DEFAULT_SETTINGS[name]["value"]
|
2014-11-29 22:39:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def set_setting(name, value):
|
2019-07-02 15:52:12 +03:00
|
|
|
with State(lock=True) as state:
|
|
|
|
if "settings" not in state:
|
2019-09-23 23:13:48 +03:00
|
|
|
state["settings"] = {}
|
|
|
|
state["settings"][name] = sanitize_setting(name, value)
|
2019-07-02 15:52:12 +03:00
|
|
|
state.modified = True
|
2014-11-29 22:39:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def reset_settings():
|
2019-07-02 15:52:12 +03:00
|
|
|
with State(lock=True) as state:
|
|
|
|
if "settings" in state:
|
2019-09-23 23:13:48 +03:00
|
|
|
del state["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():
|
2019-09-23 23:13:48 +03:00
|
|
|
return any(
|
|
|
|
[
|
|
|
|
get_session_var("force_option"),
|
2020-06-05 14:17:19 +03:00
|
|
|
proc.is_ci(),
|
2020-08-22 20:05:14 +03:00
|
|
|
os.getenv("PLATFORMIO_DISABLE_PROGRESSBAR") == "true",
|
2019-09-23 23:13:48 +03:00
|
|
|
]
|
|
|
|
)
|
2016-08-27 23:15:32 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_cid():
|
2020-08-22 17:48:49 +03:00
|
|
|
# pylint: disable=import-outside-toplevel
|
|
|
|
from platformio.clients.http import fetch_remote_content
|
|
|
|
|
2016-08-27 23:15:32 +03:00
|
|
|
cid = get_state_item("cid")
|
2018-12-26 20:54:29 +02:00
|
|
|
if cid:
|
|
|
|
return cid
|
|
|
|
uid = None
|
2020-08-22 20:05:14 +03:00
|
|
|
if os.getenv("C9_UID"):
|
|
|
|
uid = os.getenv("C9_UID")
|
2020-11-01 21:05:03 +02:00
|
|
|
elif os.getenv("GITPOD_GIT_USER_NAME"):
|
|
|
|
uid = os.getenv("GITPOD_GIT_USER_NAME")
|
2020-08-22 20:05:14 +03:00
|
|
|
elif os.getenv("CHE_API", os.getenv("CHE_API_ENDPOINT")):
|
2018-12-26 20:54:29 +02:00
|
|
|
try:
|
2020-08-16 20:21:30 +03:00
|
|
|
uid = json.loads(
|
2020-08-22 17:48:49 +03:00
|
|
|
fetch_remote_content(
|
2019-09-23 23:13:48 +03:00
|
|
|
"{api}/user?token={token}".format(
|
2020-08-22 20:05:14 +03:00
|
|
|
api=os.getenv("CHE_API", os.getenv("CHE_API_ENDPOINT")),
|
|
|
|
token=os.getenv("USER_TOKEN"),
|
2019-09-23 23:13:48 +03:00
|
|
|
)
|
|
|
|
)
|
2020-08-16 20:21:30 +03:00
|
|
|
).get("id")
|
2018-12-26 20:54:29 +02:00
|
|
|
except: # pylint: disable=bare-except
|
|
|
|
pass
|
|
|
|
if not uid:
|
|
|
|
uid = uuid.getnode()
|
2019-06-03 13:30:35 +03:00
|
|
|
cid = uuid.UUID(bytes=hashlib.md5(hashlib_encode_data(uid)).digest())
|
2018-12-26 20:54:29 +02:00
|
|
|
cid = str(cid)
|
2019-09-27 14:13:53 +03:00
|
|
|
if WINDOWS or os.getuid() > 0: # pylint: disable=no-member
|
2018-12-26 20:54:29 +02:00
|
|
|
set_state_item("cid", cid)
|
2016-08-27 23:15:32 +03:00
|
|
|
return cid
|
2020-04-10 17:59:58 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_user_agent():
|
2020-06-05 14:17:19 +03:00
|
|
|
data = [
|
|
|
|
"PlatformIO/%s" % __version__,
|
|
|
|
"CI/%d" % int(proc.is_ci()),
|
|
|
|
"Container/%d" % int(proc.is_container()),
|
|
|
|
]
|
2020-04-10 17:59:58 +03:00
|
|
|
if get_session_var("caller_id"):
|
|
|
|
data.append("Caller/%s" % get_session_var("caller_id"))
|
|
|
|
if os.getenv("PLATFORMIO_IDE"):
|
|
|
|
data.append("IDE/%s" % os.getenv("PLATFORMIO_IDE"))
|
|
|
|
data.append("Python/%s" % platform.python_version())
|
|
|
|
data.append("Platform/%s" % platform.platform())
|
|
|
|
return " ".join(data)
|
2020-04-19 19:26:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_host_id():
|
|
|
|
h = hashlib.sha1(hashlib_encode_data(get_cid()))
|
|
|
|
try:
|
|
|
|
username = getpass.getuser()
|
|
|
|
h.update(hashlib_encode_data(username))
|
|
|
|
except: # pylint: disable=bare-except
|
|
|
|
pass
|
|
|
|
return h.hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
def get_host_name():
|
|
|
|
return str(socket.gethostname())[:255]
|