1# Copyright (c) 2015 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
5import logging
6import os
7import StringIO
8
9SERVER_TEST_ROOT = os.path.dirname(__file__)
10CLIENT_TEST_ROOT = '/usr/local/autotest/tests/native_Benchmarks'
11
12def run_check(host, cmd, err_msg):
13    """Run command on a host object.
14    It checks and logs if error occurred.
15
16    @param host: the host object
17    @param cmd: the command to run
18    @param err_msg: what to print when error occurred.
19    @return: stdout of the cmd.
20    """
21    logging.info('(%s) Running: %s', host, cmd)
22    stdout = StringIO.StringIO()
23    stderr = StringIO.StringIO()
24    try:
25        result = host.run(cmd, stdout_tee=stdout, stderr_tee=stderr)
26    except:
27        logging.info('%s:\n%s\n%s\n', err_msg,
28                                      stdout.getvalue(),
29                                      stderr.getvalue())
30        raise
31    finally:
32        stdout_str = stdout.getvalue()
33        stdout.close()
34        stderr.close()
35    return stdout_str
36
37def rcp_check(client, src, dst, err_msg):
38    """Copy src on the running machine to dst on client.
39    It checks and logs if error occurred.
40
41    @param client: a host object representing client.
42    @param src: path on the running machine.
43    @param dst: path on client.
44    @param err_msg: what to print when error occurred.
45    """
46    logging.info('Copying: %s -> %s', src, dst)
47    try:
48        client.send_file(src, dst)
49    except:
50        logging.info('%s: %s %s', err_msg, src, dst)
51        raise
52
53def def_flag(d, k, v):
54    """Define a flag: k=v in d
55    Warn if k is already in d.
56
57    @param d: the flag dictionary
58    @param k: key
59    @param v: value
60    """
61    if k in d:
62        logging.info('WARNING: Overriding flag %s: from %s to %s', k, d[k], v)
63    d[k] = v
64