blob: 121e5b4305c098f9b3be20e09486c82add72ba80 [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 Jackmanbe2775a2016-12-14 14:41:04 +000032 def __init__(self, keep_password=True, unrooted=False, password=None,
33 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
52 def execute(self, command, timeout=None, check_exit_code=True, as_root=False):
53 self.logger.debug(command)
54 if as_root:
55 if self.unrooted:
56 raise TargetError('unrooted')
57 password = self._get_password()
58 command = 'echo \'{}\' | sudo -S '.format(password) + command
59 ignore = None if check_exit_code else 'all'
60 try:
61 return check_output(command, shell=True, timeout=timeout, ignore=ignore)[0]
62 except subprocess.CalledProcessError as e:
Brendan Jackman179e45f2017-01-31 19:21:11 +000063 message = 'Got exit code {}\nfrom: {}\nOUTPUT: {}'.format(
64 e.returncode, command, e.output)
65 raise TargetError(message)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010066
67 def background(self, command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, as_root=False):
68 if as_root:
69 if self.unrooted:
70 raise TargetError('unrooted')
71 password = self._get_password()
72 command = 'echo \'{}\' | sudo -S '.format(password) + command
73 return subprocess.Popen(command, stdout=stdout, stderr=stderr, shell=True)
74
75 def close(self):
76 pass
77
78 def cancel_running_command(self):
79 pass
80
81 def _get_password(self):
82 if self.password:
83 return self.password
84 password = getpass('sudo password:')
85 if self.keep_password:
86 self.password = password
87 return password