1# Copyright 2010 Google Inc. Released under the GPL v2
2#
3# Eric Li <ericli@google.com>
4
5import logging, os, pickle, re, sys
6import common
7from autotest_lib.client.bin import setup_job as client_setup_job
8
9
10def touch_init(parent_dir, child_dir):
11    """
12    Touch __init__.py file all alone through from dir_patent to child_dir.
13
14    So client tests could be loaded as Python modules. Assume child_dir is a
15    subdirectory of parent_dir.
16    """
17
18    if not child_dir.startswith(parent_dir):
19        logging.error('%s is not a subdirectory of %s' % (child_dir,
20                                                          parent_dir))
21        return
22    sub_parent_dirs = parent_dir.split(os.path.sep)
23    sub_child_dirs = child_dir.split(os.path.sep)
24    for sub_dir in sub_child_dirs[len(sub_parent_dirs):]:
25        sub_parent_dirs.append(sub_dir)
26        path = os.path.sep.join(sub_parent_dirs)
27        init_py = os.path.join(path, '__init__.py')
28        open(init_py, 'a').close()
29
30
31def init_test(testdir):
32    """
33    Instantiate a client test object from a given test directory.
34
35    @param testdir The test directory.
36    @returns A test object or None if failed to instantiate.
37    """
38
39    class options:
40        tag = ''
41        verbose = None
42        cont = False
43        harness = 'autoserv'
44        hostname = None
45        user = None
46        log = True
47    return client_setup_job.init_test(options, testdir)
48
49
50def setup(autotest_client_dir, client_test_dir):
51    """
52    Setup prebuild of a client test.
53
54    @param autotest_client_dir: The autotest/client base directory.
55    @param client_test_dir: The actual test directory under client.
56    """
57
58    os.environ['AUTODIR'] = autotest_client_dir
59    touch_init(autotest_client_dir, client_test_dir)
60
61    # instantiate a client_test instance.
62    client_test = init_test(client_test_dir)
63    client_setup_job.setup_test(client_test)
64