Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 1 | # 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 Jackman | b587049 | 2017-01-27 19:33:43 +0000 | [diff] [blame] | 15 | from glob import iglob |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 16 | import os |
| 17 | import shutil |
| 18 | import subprocess |
| 19 | import logging |
| 20 | from getpass import getpass |
| 21 | |
| 22 | from devlib.exception import TargetError |
| 23 | from devlib.utils.misc import check_output |
| 24 | |
| 25 | PACKAGE_BIN_DIRECTORY = os.path.join(os.path.dirname(__file__), 'bin') |
| 26 | |
| 27 | |
| 28 | class LocalConnection(object): |
| 29 | |
| 30 | name = 'local' |
| 31 | |
Brendan Jackman | 680406b | 2017-02-14 11:30:32 +0000 | [diff] [blame] | 32 | def __init__(self, platform=None, keep_password=True, unrooted=False, |
| 33 | password=None, timeout=None): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 34 | self.logger = logging.getLogger('local_connection') |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 35 | self.keep_password = keep_password |
| 36 | self.unrooted = unrooted |
Sebastian Goscik | a7f6ddb | 2016-02-23 17:07:20 +0000 | [diff] [blame] | 37 | self.password = password |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 38 | |
| 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 Jackman | b587049 | 2017-01-27 19:33:43 +0000 | [diff] [blame] | 45 | 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 Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 51 | |
Anouk Van Laer | 29a7940 | 2017-01-31 12:48:58 +0000 | [diff] [blame] | 52 | def execute(self, command, timeout=None, check_exit_code=True, |
| 53 | as_root=False, strip_colors=True): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 54 | 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 Jackman | 179e45f | 2017-01-31 19:21:11 +0000 | [diff] [blame] | 64 | message = 'Got exit code {}\nfrom: {}\nOUTPUT: {}'.format( |
| 65 | e.returncode, command, e.output) |
| 66 | raise TargetError(message) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 67 | |
| 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 |