blob: 377ea145abf954ea18a873c3392fa7b39138d433 [file] [log] [blame]
mblighce955fc2009-08-24 21:59:02 +00001# Copyright 2009 Google Inc. Released under the GPL v2
2
3"""
4This file contains the implementation of a host object for the local machine.
5"""
6
mbligh3c0ea962009-11-06 03:02:38 +00007import glob, os, platform
mblighce955fc2009-08-24 21:59:02 +00008from autotest_lib.client.common_lib import hosts, error
9from autotest_lib.client.bin import utils
10
11class LocalHost(hosts.Host):
mbligh3c0ea962009-11-06 03:02:38 +000012 def _initialize(self, hostname=None, bootloader=None, *args, **dargs):
mblighce955fc2009-08-24 21:59:02 +000013 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
mbligh3c0ea962009-11-06 03:02:38 +000020 self.bootloader = bootloader
mblighce955fc2009-08-24 21:59:02 +000021
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,
jadmanskif7368c52009-10-13 14:56:23 +000030 stdin=None, args=()):
mblighce955fc2009-08-24 21:59:02 +000031 """
32 @see common_lib.hosts.Host.run()
33 """
showard8e62be12009-10-14 16:07:38 +000034 try:
35 result = utils.run(
jadmanskif7368c52009-10-13 14:56:23 +000036 command, timeout=timeout, ignore_status=True,
37 stdout_tee=stdout_tee, stderr_tee=stderr_tee, stdin=stdin,
38 args=args)
showard8e62be12009-10-14 16:07:38 +000039 except error.CmdError, e:
40 # this indicates a timeout exception
41 raise error.AutotestHostRunError('command timed out', e.result_obj)
mblighce955fc2009-08-24 21:59:02 +000042
43 if not ignore_status and result.exit_status > 0:
44 raise error.AutotestHostRunError('command execution error', result)
45
46 return result
mbligh3c0ea962009-11-06 03:02:38 +000047
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