2019-05-16 21:03:15 +03:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-08-22 17:48:49 +03:00
|
|
|
from contextlib import contextmanager
|
2019-05-16 21:03:15 +03:00
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
from platformio import exception
|
2019-10-30 20:43:37 +02:00
|
|
|
from platformio.compat import (
|
2020-12-02 15:15:17 +02:00
|
|
|
PY2,
|
2019-10-30 20:43:37 +02:00
|
|
|
WINDOWS,
|
|
|
|
get_filesystem_encoding,
|
|
|
|
get_locale_encoding,
|
|
|
|
string_types,
|
|
|
|
)
|
2019-05-16 21:03:15 +03:00
|
|
|
|
|
|
|
|
2019-05-17 12:53:51 +03:00
|
|
|
class AsyncPipeBase(object):
|
|
|
|
def __init__(self):
|
2019-05-16 21:03:15 +03:00
|
|
|
self._fd_read, self._fd_write = os.pipe()
|
2020-04-28 18:05:08 +03:00
|
|
|
self._pipe_reader = os.fdopen(self._fd_read)
|
2019-05-17 12:53:51 +03:00
|
|
|
self._buffer = ""
|
|
|
|
self._thread = Thread(target=self.run)
|
|
|
|
self._thread.start()
|
2019-05-16 21:03:15 +03:00
|
|
|
|
|
|
|
def get_buffer(self):
|
|
|
|
return self._buffer
|
|
|
|
|
|
|
|
def fileno(self):
|
|
|
|
return self._fd_write
|
|
|
|
|
|
|
|
def run(self):
|
2019-05-17 12:53:51 +03:00
|
|
|
try:
|
|
|
|
self.do_reading()
|
|
|
|
except (KeyboardInterrupt, SystemExit, IOError):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def do_reading(self):
|
|
|
|
raise NotImplementedError()
|
2019-05-16 21:03:15 +03:00
|
|
|
|
|
|
|
def close(self):
|
2019-05-17 12:53:51 +03:00
|
|
|
self._buffer = ""
|
2019-05-16 21:03:15 +03:00
|
|
|
os.close(self._fd_write)
|
2019-05-17 12:53:51 +03:00
|
|
|
self._thread.join()
|
|
|
|
|
|
|
|
|
|
|
|
class BuildAsyncPipe(AsyncPipeBase):
|
|
|
|
def __init__(self, line_callback, data_callback):
|
|
|
|
self.line_callback = line_callback
|
|
|
|
self.data_callback = data_callback
|
|
|
|
super(BuildAsyncPipe, self).__init__()
|
|
|
|
|
|
|
|
def do_reading(self):
|
|
|
|
line = ""
|
|
|
|
print_immediately = False
|
|
|
|
|
|
|
|
for byte in iter(lambda: self._pipe_reader.read(1), ""):
|
|
|
|
self._buffer += byte
|
|
|
|
|
2019-06-28 19:07:59 +03:00
|
|
|
if line and byte.strip() and line[-3:] == (byte * 3):
|
2019-05-17 12:53:51 +03:00
|
|
|
print_immediately = True
|
|
|
|
|
|
|
|
if print_immediately:
|
|
|
|
# leftover bytes
|
|
|
|
if line:
|
|
|
|
self.data_callback(line)
|
|
|
|
line = ""
|
|
|
|
self.data_callback(byte)
|
|
|
|
if byte == "\n":
|
|
|
|
print_immediately = False
|
|
|
|
else:
|
|
|
|
line += byte
|
|
|
|
if byte != "\n":
|
|
|
|
continue
|
|
|
|
self.line_callback(line)
|
|
|
|
line = ""
|
|
|
|
|
|
|
|
self._pipe_reader.close()
|
|
|
|
|
|
|
|
|
|
|
|
class LineBufferedAsyncPipe(AsyncPipeBase):
|
|
|
|
def __init__(self, line_callback):
|
|
|
|
self.line_callback = line_callback
|
|
|
|
super(LineBufferedAsyncPipe, self).__init__()
|
|
|
|
|
|
|
|
def do_reading(self):
|
|
|
|
for line in iter(self._pipe_reader.readline, ""):
|
|
|
|
self._buffer += line
|
2019-05-27 22:25:22 +03:00
|
|
|
self.line_callback(line)
|
2019-05-17 12:53:51 +03:00
|
|
|
self._pipe_reader.close()
|
2019-05-16 21:03:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
def exec_command(*args, **kwargs):
|
|
|
|
result = {"out": None, "err": None, "returncode": None}
|
|
|
|
|
|
|
|
default = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
default.update(kwargs)
|
|
|
|
kwargs = default
|
|
|
|
|
|
|
|
p = subprocess.Popen(*args, **kwargs)
|
|
|
|
try:
|
2019-09-23 23:13:48 +03:00
|
|
|
result["out"], result["err"] = p.communicate()
|
|
|
|
result["returncode"] = p.returncode
|
2019-05-16 21:03:15 +03:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise exception.AbortedByUser()
|
|
|
|
finally:
|
|
|
|
for s in ("stdout", "stderr"):
|
2019-05-17 12:53:51 +03:00
|
|
|
if isinstance(kwargs[s], AsyncPipeBase):
|
2019-05-16 21:03:15 +03:00
|
|
|
kwargs[s].close()
|
|
|
|
|
|
|
|
for s in ("stdout", "stderr"):
|
2019-05-17 12:53:51 +03:00
|
|
|
if isinstance(kwargs[s], AsyncPipeBase):
|
|
|
|
result[s[3:]] = kwargs[s].get_buffer()
|
2019-05-16 21:03:15 +03:00
|
|
|
|
|
|
|
for k, v in result.items():
|
2020-12-02 15:15:17 +02:00
|
|
|
if PY2 and isinstance(v, unicode): # pylint: disable=undefined-variable
|
|
|
|
result[k] = v.encode()
|
|
|
|
elif not PY2 and isinstance(result[k], bytes):
|
2019-06-03 17:44:41 +03:00
|
|
|
try:
|
2019-10-30 20:43:37 +02:00
|
|
|
result[k] = result[k].decode(
|
|
|
|
get_locale_encoding() or get_filesystem_encoding()
|
|
|
|
)
|
2019-06-03 17:44:41 +03:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
result[k] = result[k].decode("latin-1")
|
2019-05-16 21:03:15 +03:00
|
|
|
if v and isinstance(v, string_types):
|
|
|
|
result[k] = result[k].strip()
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2020-08-22 17:48:49 +03:00
|
|
|
@contextmanager
|
|
|
|
def capture_std_streams(stdout, stderr=None):
|
|
|
|
_stdout = sys.stdout
|
|
|
|
_stderr = sys.stderr
|
|
|
|
sys.stdout = stdout
|
|
|
|
sys.stderr = stderr or stdout
|
|
|
|
yield
|
|
|
|
sys.stdout = _stdout
|
|
|
|
sys.stderr = _stderr
|
|
|
|
|
|
|
|
|
2019-05-16 21:03:15 +03:00
|
|
|
def is_ci():
|
|
|
|
return os.getenv("CI", "").lower() == "true"
|
|
|
|
|
|
|
|
|
|
|
|
def is_container():
|
2020-06-05 14:17:19 +03:00
|
|
|
if os.path.exists("/.dockerenv"):
|
|
|
|
return True
|
|
|
|
if not os.path.isfile("/proc/1/cgroup"):
|
2019-05-16 21:03:15 +03:00
|
|
|
return False
|
|
|
|
with open("/proc/1/cgroup") as fp:
|
2020-06-05 14:17:19 +03:00
|
|
|
return ":/docker/" in fp.read()
|
2019-05-16 21:03:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_pythonexe_path():
|
2020-06-05 14:17:19 +03:00
|
|
|
return os.environ.get("PYTHONEXEPATH", os.path.normpath(sys.executable))
|
2019-05-16 21:03:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
def copy_pythonpath_to_osenv():
|
|
|
|
_PYTHONPATH = []
|
|
|
|
if "PYTHONPATH" in os.environ:
|
|
|
|
_PYTHONPATH = os.environ.get("PYTHONPATH").split(os.pathsep)
|
|
|
|
for p in os.sys.path:
|
|
|
|
conditions = [p not in _PYTHONPATH]
|
|
|
|
if not WINDOWS:
|
2020-06-05 14:17:19 +03:00
|
|
|
conditions.append(
|
|
|
|
os.path.isdir(os.path.join(p, "click"))
|
|
|
|
or os.path.isdir(os.path.join(p, "platformio"))
|
|
|
|
)
|
2019-05-16 21:03:15 +03:00
|
|
|
if all(conditions):
|
|
|
|
_PYTHONPATH.append(p)
|
2019-09-23 23:13:48 +03:00
|
|
|
os.environ["PYTHONPATH"] = os.pathsep.join(_PYTHONPATH)
|
2019-05-16 21:03:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
def where_is_program(program, envpath=None):
|
|
|
|
env = os.environ
|
|
|
|
if envpath:
|
2019-09-23 23:13:48 +03:00
|
|
|
env["PATH"] = envpath
|
2019-05-16 21:03:15 +03:00
|
|
|
|
|
|
|
# try OS's built-in commands
|
|
|
|
try:
|
2019-09-23 23:13:48 +03:00
|
|
|
result = exec_command(["where" if WINDOWS else "which", program], env=env)
|
2020-06-05 14:17:19 +03:00
|
|
|
if result["returncode"] == 0 and os.path.isfile(result["out"].strip()):
|
2019-09-23 23:13:48 +03:00
|
|
|
return result["out"].strip()
|
2019-05-16 21:03:15 +03:00
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# look up in $PATH
|
|
|
|
for bin_dir in env.get("PATH", "").split(os.pathsep):
|
2020-06-05 14:17:19 +03:00
|
|
|
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)
|
2019-05-16 21:03:15 +03:00
|
|
|
|
|
|
|
return program
|
2020-11-30 20:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def append_env_path(name, value):
|
|
|
|
cur_value = os.environ.get(name) or ""
|
|
|
|
if cur_value and value in cur_value.split(os.pathsep):
|
|
|
|
return cur_value
|
|
|
|
os.environ[name] = os.pathsep.join([cur_value, value])
|
|
|
|
return os.environ[name]
|