From 27fd3b0b14995dc87ab345c954d150d14ec0177c Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Fri, 5 Jun 2020 14:17:19 +0300 Subject: [PATCH] Improve detecting if PlatformIO Core is run in container --- platformio/app.py | 11 +++++++---- platformio/maintenance.py | 5 ++--- platformio/proc.py | 28 ++++++++++++++-------------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/platformio/app.py b/platformio/app.py index 6c7c7b1a..f4ae15b9 100644 --- a/platformio/app.py +++ b/platformio/app.py @@ -25,9 +25,8 @@ from time import time import requests -from platformio import __version__, exception, fs, lockfile +from platformio import __version__, exception, fs, lockfile, proc from platformio.compat import WINDOWS, dump_json_to_unicode, hashlib_encode_data -from platformio.proc import is_ci from platformio.project.helpers import ( get_default_projects_dir, get_project_cache_dir, @@ -383,7 +382,7 @@ def is_disabled_progressbar(): return any( [ get_session_var("force_option"), - is_ci(), + proc.is_ci(), getenv("PLATFORMIO_DISABLE_PROGRESSBAR") == "true", ] ) @@ -420,7 +419,11 @@ def get_cid(): def get_user_agent(): - data = ["PlatformIO/%s" % __version__, "CI/%d" % int(is_ci())] + 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"): diff --git a/platformio/maintenance.py b/platformio/maintenance.py index 16712872..0c8ee2df 100644 --- a/platformio/maintenance.py +++ b/platformio/maintenance.py @@ -66,10 +66,9 @@ def on_platformio_exception(e): def set_caller(caller=None): + caller = caller or getenv("PLATFORMIO_CALLER") if not caller: - if getenv("PLATFORMIO_CALLER"): - caller = getenv("PLATFORMIO_CALLER") - elif getenv("VSCODE_PID") or getenv("VSCODE_NLS_CONFIG"): + if getenv("VSCODE_PID") or getenv("VSCODE_NLS_CONFIG"): caller = "vscode" elif is_container(): if getenv("C9_UID"): diff --git a/platformio/proc.py b/platformio/proc.py index 80e50201..04f15a57 100644 --- a/platformio/proc.py +++ b/platformio/proc.py @@ -15,7 +15,6 @@ import os import subprocess import sys -from os.path import isdir, isfile, join, normpath from threading import Thread from platformio import exception @@ -143,18 +142,16 @@ def is_ci(): def is_container(): - if not isfile("/proc/1/cgroup"): + if os.path.exists("/.dockerenv"): + return True + if not os.path.isfile("/proc/1/cgroup"): return False with open("/proc/1/cgroup") as fp: - for line in fp: - line = line.strip() - if ":" in line and not line.endswith(":/"): - return True - return False + return ":/docker/" in fp.read() def get_pythonexe_path(): - return os.environ.get("PYTHONEXEPATH", normpath(sys.executable)) + return os.environ.get("PYTHONEXEPATH", os.path.normpath(sys.executable)) def copy_pythonpath_to_osenv(): @@ -164,7 +161,10 @@ def copy_pythonpath_to_osenv(): for p in os.sys.path: conditions = [p not in _PYTHONPATH] if not WINDOWS: - conditions.append(isdir(join(p, "click")) or isdir(join(p, "platformio"))) + conditions.append( + os.path.isdir(os.path.join(p, "click")) + or os.path.isdir(os.path.join(p, "platformio")) + ) if all(conditions): _PYTHONPATH.append(p) os.environ["PYTHONPATH"] = os.pathsep.join(_PYTHONPATH) @@ -178,16 +178,16 @@ def where_is_program(program, envpath=None): # try OS's built-in commands try: result = exec_command(["where" if WINDOWS else "which", program], env=env) - if result["returncode"] == 0 and isfile(result["out"].strip()): + if result["returncode"] == 0 and os.path.isfile(result["out"].strip()): return result["out"].strip() except OSError: pass # look up in $PATH for bin_dir in env.get("PATH", "").split(os.pathsep): - if isfile(join(bin_dir, program)): - return join(bin_dir, program) - if isfile(join(bin_dir, "%s.exe" % program)): - return join(bin_dir, "%s.exe" % program) + if os.path.isfile(os.path.join(bin_dir, program)): + return os.path.join(bin_dir, program) + if os.path.isfile(os.path.join(bin_dir, "%s.exe" % program)): + return os.path.join(bin_dir, "%s.exe" % program) return program