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 | # |
| 15 | import os |
| 16 | import shutil |
| 17 | import subprocess |
| 18 | import logging |
| 19 | from getpass import getpass |
| 20 | |
| 21 | from devlib.exception import TargetError |
| 22 | from devlib.utils.misc import check_output |
| 23 | |
| 24 | PACKAGE_BIN_DIRECTORY = os.path.join(os.path.dirname(__file__), 'bin') |
| 25 | |
| 26 | |
| 27 | class LocalConnection(object): |
| 28 | |
| 29 | name = 'local' |
| 30 | |
| 31 | def __init__(self, timeout=10, keep_password=True, unrooted=False): |
| 32 | self.logger = logging.getLogger('local_connection') |
| 33 | self.timeout = timeout |
| 34 | self.keep_password = keep_password |
| 35 | self.unrooted = unrooted |
| 36 | self.password = None |
| 37 | |
| 38 | def push(self, source, dest, timeout=None, as_root=False): # pylint: disable=unused-argument |
| 39 | self.logger.debug('cp {} {}'.format(source, dest)) |
| 40 | shutil.copy(source, dest) |
| 41 | |
| 42 | def pull(self, source, dest, timeout=None, as_root=False): # pylint: disable=unused-argument |
| 43 | self.logger.debug('cp {} {}'.format(source, dest)) |
| 44 | shutil.copy(source, dest) |
| 45 | |
| 46 | def execute(self, command, timeout=None, check_exit_code=True, as_root=False): |
| 47 | self.logger.debug(command) |
| 48 | if as_root: |
| 49 | if self.unrooted: |
| 50 | raise TargetError('unrooted') |
| 51 | password = self._get_password() |
| 52 | command = 'echo \'{}\' | sudo -S '.format(password) + command |
| 53 | ignore = None if check_exit_code else 'all' |
| 54 | try: |
| 55 | return check_output(command, shell=True, timeout=timeout, ignore=ignore)[0] |
| 56 | except subprocess.CalledProcessError as e: |
| 57 | raise TargetError(e) |
| 58 | |
| 59 | def background(self, command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, as_root=False): |
| 60 | if as_root: |
| 61 | if self.unrooted: |
| 62 | raise TargetError('unrooted') |
| 63 | password = self._get_password() |
| 64 | command = 'echo \'{}\' | sudo -S '.format(password) + command |
| 65 | return subprocess.Popen(command, stdout=stdout, stderr=stderr, shell=True) |
| 66 | |
| 67 | def close(self): |
| 68 | pass |
| 69 | |
| 70 | def cancel_running_command(self): |
| 71 | pass |
| 72 | |
| 73 | def _get_password(self): |
| 74 | if self.password: |
| 75 | return self.password |
| 76 | password = getpass('sudo password:') |
| 77 | if self.keep_password: |
| 78 | self.password = password |
| 79 | return password |
| 80 | |