mbligh | ce955fc | 2009-08-24 21:59:02 +0000 | [diff] [blame] | 1 | # Copyright 2009 Google Inc. Released under the GPL v2 |
| 2 | |
| 3 | """ |
| 4 | This file contains the implementation of a host object for the local machine. |
| 5 | """ |
| 6 | |
mbligh | 3c0ea96 | 2009-11-06 03:02:38 +0000 | [diff] [blame] | 7 | import glob, os, platform |
mbligh | ce955fc | 2009-08-24 21:59:02 +0000 | [diff] [blame] | 8 | from autotest_lib.client.common_lib import hosts, error |
| 9 | from autotest_lib.client.bin import utils |
| 10 | |
| 11 | class LocalHost(hosts.Host): |
mbligh | 3c0ea96 | 2009-11-06 03:02:38 +0000 | [diff] [blame] | 12 | def _initialize(self, hostname=None, bootloader=None, *args, **dargs): |
mbligh | ce955fc | 2009-08-24 21:59:02 +0000 | [diff] [blame] | 13 | super(LocalHost, self)._initialize(*args, **dargs) |
| 14 | |
| 15 | # hostname will be an actual hostname when this client was created |
| 16 | # by an autoserv process |
| 17 | if not hostname: |
| 18 | hostname = platform.node() |
| 19 | self.hostname = hostname |
mbligh | 3c0ea96 | 2009-11-06 03:02:38 +0000 | [diff] [blame] | 20 | self.bootloader = bootloader |
mbligh | ce955fc | 2009-08-24 21:59:02 +0000 | [diff] [blame] | 21 | |
| 22 | |
| 23 | def wait_up(self, timeout=None): |
| 24 | # a local host is always up |
| 25 | return True |
| 26 | |
| 27 | |
| 28 | def run(self, command, timeout=3600, ignore_status=False, |
| 29 | stdout_tee=utils.TEE_TO_LOGS, stderr_tee=utils.TEE_TO_LOGS, |
jadmanski | f7368c5 | 2009-10-13 14:56:23 +0000 | [diff] [blame] | 30 | stdin=None, args=()): |
mbligh | ce955fc | 2009-08-24 21:59:02 +0000 | [diff] [blame] | 31 | """ |
| 32 | @see common_lib.hosts.Host.run() |
| 33 | """ |
showard | 8e62be1 | 2009-10-14 16:07:38 +0000 | [diff] [blame] | 34 | try: |
| 35 | result = utils.run( |
jadmanski | f7368c5 | 2009-10-13 14:56:23 +0000 | [diff] [blame] | 36 | command, timeout=timeout, ignore_status=True, |
| 37 | stdout_tee=stdout_tee, stderr_tee=stderr_tee, stdin=stdin, |
| 38 | args=args) |
showard | 8e62be1 | 2009-10-14 16:07:38 +0000 | [diff] [blame] | 39 | except error.CmdError, e: |
| 40 | # this indicates a timeout exception |
| 41 | raise error.AutotestHostRunError('command timed out', e.result_obj) |
mbligh | ce955fc | 2009-08-24 21:59:02 +0000 | [diff] [blame] | 42 | |
| 43 | if not ignore_status and result.exit_status > 0: |
| 44 | raise error.AutotestHostRunError('command execution error', result) |
| 45 | |
| 46 | return result |
mbligh | 3c0ea96 | 2009-11-06 03:02:38 +0000 | [diff] [blame] | 47 | |
| 48 | |
| 49 | def list_files_glob(self, path_glob): |
| 50 | """ |
| 51 | Get a list of files on a remote host given a glob pattern path. |
| 52 | """ |
| 53 | return glob.glob(path_glob) |
| 54 | |
| 55 | |
| 56 | def symlink_closure(self, paths): |
| 57 | """ |
| 58 | Given a sequence of path strings, return the set of all paths that |
| 59 | can be reached from the initial set by following symlinks. |
| 60 | |
| 61 | @param paths: sequence of path strings. |
| 62 | @return: a sequence of path strings that are all the unique paths that |
| 63 | can be reached from the given ones after following symlinks. |
| 64 | """ |
| 65 | paths = set(paths) |
| 66 | closure = set() |
| 67 | |
| 68 | while paths: |
| 69 | path = paths.pop() |
| 70 | if not os.path.exists(path): |
| 71 | continue |
| 72 | closure.add(path) |
| 73 | if os.path.islink(path): |
| 74 | link_to = os.path.join(os.path.dirname(path), |
| 75 | os.readlink(path)) |
| 76 | if link_to not in closure: |
| 77 | paths.add(link_to) |
| 78 | |
| 79 | return closure |