Home | History | Annotate | Download | only in cros
      1 # Copyright 2014 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import os
      6 
      7 from autotest_lib.client.bin import utils
      8 from autotest_lib.client.common_lib import error
      9 
     10 
     11 def get_install_path(filename, host=None):
     12     """
     13     Checks if a file exists on a remote machine in one of several paths.
     14 
     15     @param filename String name of the file to check for existence.
     16     @param host Host object representing the remote machine.
     17     @return String full path of installed file, or None if not found.
     18 
     19     """
     20     run = utils.run
     21     if host is not None:
     22         run = host.run
     23     PATHS = ['/bin',
     24              '/sbin',
     25              '/system/bin',
     26              '/usr/bin',
     27              '/usr/sbin',
     28              '/usr/local/bin',
     29              '/usr/local/sbin']
     30     glob_list = [os.path.join(path, filename) for path in PATHS]
     31     # Some hosts have poor support for which.  Sometimes none.
     32     # Others have shells that can't perform advanced globbing.
     33     result = host.run('ls %s 2> /dev/null' % ' '.join(glob_list),
     34                       ignore_status=True)
     35     found_path = result.stdout.split('\n')[0].strip()
     36     return found_path or None
     37 
     38 
     39 def must_be_installed(cmd, host=None):
     40     """
     41     Asserts that cmd is installed on a remote machine at some path and raises
     42     an exception if this is not the case.
     43 
     44     @param cmd String name of the command to check for existence.
     45     @param host Host object representing the remote machine.
     46     @return String full path of cmd on success.  Error raised on failure.
     47 
     48     """
     49     run = utils.run
     50     if host is not None:
     51         run = host.run
     52     if run('ls %s >/dev/null 2>&1' % cmd,
     53            ignore_status=True).exit_status == 0:
     54         return cmd
     55 
     56     # Hunt for the equivalent file in a bunch of places.
     57     cmd_base = os.path.basename(cmd)
     58     alternate_path = get_install_path(cmd_base, host=host)
     59     if alternate_path:
     60         return alternate_path
     61 
     62     error_msg = 'Unable to find %s' % cmd
     63     if host is not None:
     64         error_msg += ' on %s' % host.hostname
     65     raise error.TestError(error_msg)
     66