2024-01-17 11:46:02 +00:00
|
|
|
# Copyright 2015-2024 ARM Limited
|
2015-10-09 09:30:04 +01: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.
|
|
|
|
#
|
|
|
|
import os
|
2017-09-12 14:52:39 +01:00
|
|
|
import signal
|
2015-10-09 09:30:04 +01:00
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import logging
|
2023-06-26 10:43:10 +01:00
|
|
|
import sys
|
2015-10-09 09:30:04 +01:00
|
|
|
from getpass import getpass
|
2022-11-23 11:41:05 +00:00
|
|
|
from shlex import quote
|
2015-10-09 09:30:04 +01:00
|
|
|
|
2021-08-20 15:54:46 +01:00
|
|
|
from devlib.exception import (
|
2024-01-17 11:46:02 +00:00
|
|
|
TargetStableError, TargetTransientCalledProcessError, TargetStableCalledProcessError
|
2021-08-20 15:54:46 +01:00
|
|
|
)
|
2015-10-09 09:30:04 +01:00
|
|
|
from devlib.utils.misc import check_output
|
2020-01-15 17:19:24 +00:00
|
|
|
from devlib.connection import ConnectionBase, PopenBackgroundCommand
|
2015-10-09 09:30:04 +01:00
|
|
|
|
2020-01-09 16:45:44 +00:00
|
|
|
|
2023-06-26 10:43:10 +01:00
|
|
|
if sys.version_info >= (3, 8):
|
|
|
|
def copy_tree(src, dst):
|
|
|
|
from shutils import copy, copytree
|
|
|
|
copytree(
|
|
|
|
src,
|
|
|
|
dst,
|
|
|
|
# dirs_exist_ok=True only exists in Python >= 3.8
|
|
|
|
dirs_exist_ok=True,
|
|
|
|
# Do not copy creation and modification time to behave like other
|
|
|
|
# targets.
|
|
|
|
copy_function=copy
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
def copy_tree(src, dst):
|
|
|
|
from distutils.dir_util import copy_tree
|
|
|
|
# Mirror the behavior of all other targets which only copy the
|
|
|
|
# content without metadata
|
|
|
|
copy_tree(src, dst, preserve_mode=False, preserve_times=False)
|
|
|
|
|
|
|
|
|
2015-10-09 09:30:04 +01:00
|
|
|
PACKAGE_BIN_DIRECTORY = os.path.join(os.path.dirname(__file__), 'bin')
|
|
|
|
|
2020-01-09 16:45:44 +00:00
|
|
|
|
2018-07-11 17:30:45 +01:00
|
|
|
# pylint: disable=redefined-outer-name
|
2017-09-12 14:52:39 +01:00
|
|
|
def kill_children(pid, signal=signal.SIGKILL):
|
|
|
|
with open('/proc/{0}/task/{0}/children'.format(pid), 'r') as fd:
|
|
|
|
for cpid in map(int, fd.read().strip().split()):
|
|
|
|
kill_children(cpid, signal)
|
|
|
|
os.kill(cpid, signal)
|
2015-10-09 09:30:04 +01:00
|
|
|
|
2020-01-09 16:45:44 +00:00
|
|
|
|
2020-01-15 17:19:24 +00:00
|
|
|
class LocalConnection(ConnectionBase):
|
2015-10-09 09:30:04 +01:00
|
|
|
|
|
|
|
name = 'local'
|
2020-01-09 16:45:44 +00:00
|
|
|
host = 'localhost'
|
2015-10-09 09:30:04 +01:00
|
|
|
|
2019-09-11 16:47:07 +01:00
|
|
|
@property
|
|
|
|
def connected_as_root(self):
|
|
|
|
if self._connected_as_root is None:
|
|
|
|
result = self.execute('id', as_root=False)
|
|
|
|
self._connected_as_root = 'uid=0(' in result
|
|
|
|
return self._connected_as_root
|
|
|
|
|
|
|
|
@connected_as_root.setter
|
|
|
|
def connected_as_root(self, state):
|
|
|
|
self._connected_as_root = state
|
|
|
|
|
2018-07-11 17:30:45 +01:00
|
|
|
# pylint: disable=unused-argument
|
2017-02-14 11:30:32 +00:00
|
|
|
def __init__(self, platform=None, keep_password=True, unrooted=False,
|
|
|
|
password=None, timeout=None):
|
2020-01-15 17:19:24 +00:00
|
|
|
super().__init__()
|
2019-09-11 16:47:07 +01:00
|
|
|
self._connected_as_root = None
|
2015-10-09 09:30:04 +01:00
|
|
|
self.logger = logging.getLogger('local_connection')
|
|
|
|
self.keep_password = keep_password
|
|
|
|
self.unrooted = unrooted
|
2016-02-23 17:07:20 +00:00
|
|
|
self.password = password
|
2015-10-09 09:30:04 +01:00
|
|
|
|
2020-06-18 12:25:17 +01:00
|
|
|
|
|
|
|
def _copy_path(self, source, dest):
|
|
|
|
self.logger.debug('copying {} to {}'.format(source, dest))
|
|
|
|
if os.path.isdir(source):
|
2023-06-26 10:43:10 +01:00
|
|
|
copy_tree(source, dest)
|
2020-06-18 12:25:17 +01:00
|
|
|
else:
|
2020-06-16 11:56:25 +01:00
|
|
|
shutil.copy(source, dest)
|
2015-10-09 09:30:04 +01:00
|
|
|
|
2020-06-18 12:25:17 +01:00
|
|
|
def _copy_paths(self, sources, dest):
|
2020-06-16 11:56:25 +01:00
|
|
|
for source in sources:
|
2020-06-18 12:25:17 +01:00
|
|
|
self._copy_path(source, dest)
|
|
|
|
|
|
|
|
def push(self, sources, dest, timeout=None, as_root=False): # pylint: disable=unused-argument
|
|
|
|
self._copy_paths(sources, dest)
|
|
|
|
|
|
|
|
def pull(self, sources, dest, timeout=None, as_root=False): # pylint: disable=unused-argument
|
|
|
|
self._copy_paths(sources, dest)
|
2015-10-09 09:30:04 +01:00
|
|
|
|
2018-07-11 17:30:45 +01:00
|
|
|
# pylint: disable=unused-argument
|
2017-01-31 12:48:58 +00:00
|
|
|
def execute(self, command, timeout=None, check_exit_code=True,
|
exceptions: Classify transient exceptions
Exceptions such as TargetError can sometimes be raised because of a
network issue, which is useful to distinguish from errors caused by a
missing feature for automated testing environments.
The following exceptions are introduced:
* DevlibStableError: raised when a non-transient error is encountered
* TargetStableError
* DevlibTransientError: raised when a transient error is encountered,
including timeouts.
* TargetTransientError
When there is an ambiguity on the type of exception to use, it can be
assumed that the configuration is correct, and therefore it is a
transient error, unless the function is specifically designed to probe a
property of the system. In that case, ambiguity is allowed to be lifted
by assuming a non-transient error, since we expect it to raise an
exception when that property is not met. Such ambiguous case can appear
when checking Android has booted, since we cannot know if this is a
timeout/connection issue, or an actual issue with the Android build or
configuration. Another case are the execute() methods, which can be
expected to fail on purpose. A new parameter will_succeed=False is
added, to automatically turn non transient errors into transient ones if
the caller is 100% sure that the command cannot fail unless there is an
environment issue that is outside of the scope controlled by the user.
devlib now never raises TargetError directly, but one of
TargetStableError or TargetTransientError. External code can therefore
rely on all (indirect) instances TargetError to be in either category.
Most existing uses of TargetError are replaced by TargetStableError.
2018-06-20 15:04:12 +01:00
|
|
|
as_root=False, strip_colors=True, will_succeed=False):
|
2015-10-09 09:30:04 +01:00
|
|
|
self.logger.debug(command)
|
2020-11-18 17:13:17 +00:00
|
|
|
use_sudo = as_root and not self.connected_as_root
|
|
|
|
if use_sudo:
|
2015-10-09 09:30:04 +01:00
|
|
|
if self.unrooted:
|
exceptions: Classify transient exceptions
Exceptions such as TargetError can sometimes be raised because of a
network issue, which is useful to distinguish from errors caused by a
missing feature for automated testing environments.
The following exceptions are introduced:
* DevlibStableError: raised when a non-transient error is encountered
* TargetStableError
* DevlibTransientError: raised when a transient error is encountered,
including timeouts.
* TargetTransientError
When there is an ambiguity on the type of exception to use, it can be
assumed that the configuration is correct, and therefore it is a
transient error, unless the function is specifically designed to probe a
property of the system. In that case, ambiguity is allowed to be lifted
by assuming a non-transient error, since we expect it to raise an
exception when that property is not met. Such ambiguous case can appear
when checking Android has booted, since we cannot know if this is a
timeout/connection issue, or an actual issue with the Android build or
configuration. Another case are the execute() methods, which can be
expected to fail on purpose. A new parameter will_succeed=False is
added, to automatically turn non transient errors into transient ones if
the caller is 100% sure that the command cannot fail unless there is an
environment issue that is outside of the scope controlled by the user.
devlib now never raises TargetError directly, but one of
TargetStableError or TargetTransientError. External code can therefore
rely on all (indirect) instances TargetError to be in either category.
Most existing uses of TargetError are replaced by TargetStableError.
2018-06-20 15:04:12 +01:00
|
|
|
raise TargetStableError('unrooted')
|
2015-10-09 09:30:04 +01:00
|
|
|
password = self._get_password()
|
2021-03-24 10:09:45 +00:00
|
|
|
command = "echo {} | sudo -k -p ' ' -S -- sh -c {}".format(quote(password), quote(command))
|
2015-10-09 09:30:04 +01:00
|
|
|
ignore = None if check_exit_code else 'all'
|
|
|
|
try:
|
2020-11-06 16:22:01 +00:00
|
|
|
stdout, stderr = check_output(command, shell=True, timeout=timeout, ignore=ignore)
|
2015-10-09 09:30:04 +01:00
|
|
|
except subprocess.CalledProcessError as e:
|
2021-08-20 15:54:46 +01:00
|
|
|
cls = TargetTransientCalledProcessError if will_succeed else TargetStableCalledProcessError
|
|
|
|
raise cls(
|
|
|
|
e.returncode,
|
|
|
|
command,
|
|
|
|
e.output,
|
|
|
|
e.stderr,
|
|
|
|
)
|
2020-11-18 17:13:17 +00:00
|
|
|
|
|
|
|
# Remove the one-character prompt of sudo -S -p
|
|
|
|
if use_sudo and stderr:
|
|
|
|
stderr = stderr[1:]
|
|
|
|
|
2020-11-06 16:22:01 +00:00
|
|
|
return stdout + stderr
|
2015-10-09 09:30:04 +01:00
|
|
|
|
|
|
|
def background(self, command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, as_root=False):
|
2019-09-11 16:47:07 +01:00
|
|
|
if as_root and not self.connected_as_root:
|
2015-10-09 09:30:04 +01:00
|
|
|
if self.unrooted:
|
exceptions: Classify transient exceptions
Exceptions such as TargetError can sometimes be raised because of a
network issue, which is useful to distinguish from errors caused by a
missing feature for automated testing environments.
The following exceptions are introduced:
* DevlibStableError: raised when a non-transient error is encountered
* TargetStableError
* DevlibTransientError: raised when a transient error is encountered,
including timeouts.
* TargetTransientError
When there is an ambiguity on the type of exception to use, it can be
assumed that the configuration is correct, and therefore it is a
transient error, unless the function is specifically designed to probe a
property of the system. In that case, ambiguity is allowed to be lifted
by assuming a non-transient error, since we expect it to raise an
exception when that property is not met. Such ambiguous case can appear
when checking Android has booted, since we cannot know if this is a
timeout/connection issue, or an actual issue with the Android build or
configuration. Another case are the execute() methods, which can be
expected to fail on purpose. A new parameter will_succeed=False is
added, to automatically turn non transient errors into transient ones if
the caller is 100% sure that the command cannot fail unless there is an
environment issue that is outside of the scope controlled by the user.
devlib now never raises TargetError directly, but one of
TargetStableError or TargetTransientError. External code can therefore
rely on all (indirect) instances TargetError to be in either category.
Most existing uses of TargetError are replaced by TargetStableError.
2018-06-20 15:04:12 +01:00
|
|
|
raise TargetStableError('unrooted')
|
2015-10-09 09:30:04 +01:00
|
|
|
password = self._get_password()
|
2020-11-18 17:13:17 +00:00
|
|
|
# The sudo prompt will add a space on stderr, but we cannot filter
|
|
|
|
# it out here
|
2021-03-24 10:09:45 +00:00
|
|
|
command = "echo {} | sudo -k -p ' ' -S -- sh -c {}".format(quote(password), quote(command))
|
2015-10-09 09:30:04 +01:00
|
|
|
|
2020-01-15 17:19:24 +00:00
|
|
|
# Make sure to get a new PGID so PopenBackgroundCommand() can kill
|
|
|
|
# all sub processes that could be started without troubles.
|
|
|
|
def preexec_fn():
|
|
|
|
os.setpgrp()
|
|
|
|
|
|
|
|
popen = subprocess.Popen(
|
|
|
|
command,
|
|
|
|
stdout=stdout,
|
|
|
|
stderr=stderr,
|
2021-08-18 16:18:11 +01:00
|
|
|
stdin=subprocess.PIPE,
|
2020-01-15 17:19:24 +00:00
|
|
|
shell=True,
|
|
|
|
preexec_fn=preexec_fn,
|
|
|
|
)
|
2023-04-06 21:02:17 +01:00
|
|
|
bg_cmd = PopenBackgroundCommand(self, popen)
|
2020-01-15 17:19:24 +00:00
|
|
|
return bg_cmd
|
|
|
|
|
|
|
|
def _close(self):
|
2015-10-09 09:30:04 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
def cancel_running_command(self):
|
|
|
|
pass
|
|
|
|
|
2019-11-07 09:59:48 +00:00
|
|
|
def wait_for_device(self, timeout=30):
|
|
|
|
return
|
|
|
|
|
|
|
|
def reboot_bootloader(self, timeout=30):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2015-10-09 09:30:04 +01:00
|
|
|
def _get_password(self):
|
|
|
|
if self.password:
|
|
|
|
return self.password
|
|
|
|
password = getpass('sudo password:')
|
|
|
|
if self.keep_password:
|
|
|
|
self.password = password
|
|
|
|
return password
|