blob: 8c8a069e4603ac6f788401fbd23418276778d1f0 [file] [log] [blame]
Sergei Trofimov4e6afe92015-10-09 09:30:04 +01001# Copyright 2015 ARM Limited
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
Brendan Jackmanb5870492017-01-27 19:33:43 +000015from glob import iglob
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010016import os
17import shutil
18import subprocess
19import logging
20from getpass import getpass
21
22from devlib.exception import TargetError
23from devlib.utils.misc import check_output
24
25PACKAGE_BIN_DIRECTORY = os.path.join(os.path.dirname(__file__), 'bin')
26
27
28class LocalConnection(object):
29
30 name = 'local'
31
Brendan Jackman680406b2017-02-14 11:30:32 +000032 def __init__(self, platform=None, keep_password=True, unrooted=False,
33 password=None, timeout=None):
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010034 self.logger = logging.getLogger('local_connection')
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010035 self.keep_password = keep_password
36 self.unrooted = unrooted
Sebastian Goscika7f6ddb2016-02-23 17:07:20 +000037 self.password = password
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010038
39 def push(self, source, dest, timeout=None, as_root=False): # pylint: disable=unused-argument
40 self.logger.debug('cp {} {}'.format(source, dest))
41 shutil.copy(source, dest)
42
43 def pull(self, source, dest, timeout=None, as_root=False): # pylint: disable=unused-argument
44 self.logger.debug('cp {} {}'.format(source, dest))
Brendan Jackmanb5870492017-01-27 19:33:43 +000045 if ('*' in source or '?' in source) and os.path.isdir(dest):
46 # Pull all files matching a wildcard expression
47 for each_source in iglob(source):
48 shutil.copy(each_source, dest)
49 else:
50 shutil.copy(source, dest)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010051
Anouk Van Laer29a79402017-01-31 12:48:58 +000052 def execute(self, command, timeout=None, check_exit_code=True,
53 as_root=False, strip_colors=True):
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010054 self.logger.debug(command)
55 if as_root:
56 if self.unrooted:
57 raise TargetError('unrooted')
58 password = self._get_password()
59 command = 'echo \'{}\' | sudo -S '.format(password) + command
60 ignore = None if check_exit_code else 'all'
61 try:
62 return check_output(command, shell=True, timeout=timeout, ignore=ignore)[0]
63 except subprocess.CalledProcessError as e:
Brendan Jackman179e45f2017-01-31 19:21:11 +000064 message = 'Got exit code {}\nfrom: {}\nOUTPUT: {}'.format(
65 e.returncode, command, e.output)
66 raise TargetError(message)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010067
68 def background(self, command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, as_root=False):
69 if as_root:
70 if self.unrooted:
71 raise TargetError('unrooted')
72 password = self._get_password()
73 command = 'echo \'{}\' | sudo -S '.format(password) + command
74 return subprocess.Popen(command, stdout=stdout, stderr=stderr, shell=True)
75
76 def close(self):
77 pass
78
79 def cancel_running_command(self):
80 pass
81
82 def _get_password(self):
83 if self.password:
84 return self.password
85 password = getpass('sudo password:')
86 if self.keep_password:
87 self.password = password
88 return password