Files
platformio-core/platformio/app.py

284 lines
7.8 KiB
Python
Raw Normal View History

# 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.
2014-11-29 22:39:44 +02: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
import json
import os
import platform
2020-04-19 19:26:56 +03:00
import socket
2016-08-27 23:15:32 +03:00
import uuid
2014-11-29 22:39:44 +02:00
2020-08-22 17:48:49 +03:00
from platformio import __version__, exception, fs, proc
2021-03-19 00:21:44 +02:00
from platformio.compat import IS_WINDOWS, hashlib_encode_data
from platformio.package.lockfile import LockFile
from platformio.project.config import ProjectConfig
from platformio.project.helpers import get_default_projects_dir
def projects_dir_validate(projects_dir):
assert os.path.isdir(projects_dir)
return os.path.abspath(projects_dir)
2014-11-29 22:39:44 +02:00
DEFAULT_SETTINGS = {
"check_platformio_interval": {
"description": "Check for the new PlatformIO Core interval (days)",
"value": 7,
},
"check_prune_system_threshold": {
"description": "Check for pruning unnecessary data threshold (megabytes)",
"value": 1024,
},
"enable_cache": {
"description": "Enable caching for HTTP API requests",
"value": True,
2016-08-25 22:57:52 +03:00
},
2014-11-29 22:39:44 +02:00
"enable_telemetry": {
2021-10-25 20:25:23 +03:00
"description": ("Telemetry service <https://bit.ly/pio-telemetry> (Yes/No)"),
"value": True,
},
"force_verbose": {
"description": "Force verbose output when processing environments",
"value": False,
},
"projects_dir": {
"description": "Default location for PlatformIO projects (PlatformIO Home)",
"value": get_default_projects_dir(),
"validator": projects_dir_validate,
},
2014-11-29 22:39:44 +02:00
}
SESSION_VARS = {
"command_ctx": None,
"force_option": False,
"caller_id": None,
"custom_project_conf": 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:
core_dir = ProjectConfig.get_instance().get("platformio", "core_dir")
if not os.path.isdir(core_dir):
os.makedirs(core_dir)
self.path = os.path.join(core_dir, "appstate.json")
2019-07-02 00:41:47 +03:00
self._storage = {}
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:
self._lock_state_file()
if os.path.isfile(self.path):
self._storage = fs.load_json(self.path)
2019-07-02 00:41:47 +03:00
assert isinstance(self._storage, dict)
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:
try:
with open(self.path, mode="w", encoding="utf8") as fp:
2021-03-17 21:08:06 +02:00
fp.write(json.dumps(self._storage))
except IOError:
raise exception.HomeDirPermissionsError(os.path.dirname(self.path))
self._unlock_state_file()
def _lock_state_file(self):
if not self.lock:
return
self._lockfile = LockFile(self.path)
try:
self._lockfile.acquire()
except IOError:
raise exception.HomeDirPermissionsError(os.path.dirname(self.path))
def _unlock_state_file(self):
2018-12-03 18:31:12 -08:00
if hasattr(self, "_lockfile") and self._lockfile:
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
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)
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
2014-11-29 22:39:44 +02:00
def sanitize_setting(name, value):
if name not in DEFAULT_SETTINGS:
raise exception.InvalidSettingName(name)
defdata = DEFAULT_SETTINGS[name]
try:
if "validator" in defdata:
value = defdata["validator"](value)
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 exception.InvalidSettingValue(value, name)
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
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]
2014-11-29 22:39:44 +02:00
def get_setting(name):
_env_name = "PLATFORMIO_SETTING_%s" % name.upper()
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:
if "settings" in state and name in state["settings"]:
return state["settings"][name]
2014-11-29 22:39:44 +02: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:
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:
del state["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 any(
[
get_session_var("force_option"),
proc.is_ci(),
os.getenv("PLATFORMIO_DISABLE_PROGRESSBAR") == "true",
]
)
2016-08-27 23:15:32 +03:00
def get_cid():
cid = get_state_item("cid")
if cid:
return cid
uid = None
2022-01-19 17:16:23 +02:00
if os.getenv("GITHUB_USER"):
uid = os.getenv("GITHUB_USER")
2020-11-01 21:05:03 +02:00
elif os.getenv("GITPOD_GIT_USER_NAME"):
uid = os.getenv("GITPOD_GIT_USER_NAME")
if not uid:
uid = uuid.getnode()
cid = uuid.UUID(bytes=hashlib.md5(hashlib_encode_data(uid)).digest())
cid = str(cid)
2021-03-19 00:21:44 +02:00
if IS_WINDOWS or os.getuid() > 0: # pylint: disable=no-member
set_state_item("cid", cid)
2016-08-27 23:15:32 +03:00
return cid
def get_user_agent():
data = [
"PlatformIO/%s" % __version__,
"CI/%d" % int(proc.is_ci()),
"Container/%d" % int(proc.is_container()),
]
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]