1# Copyright 2007 Google Inc. Released under the GPL v2
2
3"""
4This module defines the SourceKernel class
5
6        SourceKernel: an linux kernel built from source
7"""
8
9
10from autotest_lib.server import kernel, autotest
11
12
13class SourceKernel(kernel.Kernel):
14    """
15    This class represents a linux kernel built from source.
16
17    It is used to obtain a built kernel or create one from source and
18    install it on a Host.
19
20    Implementation details:
21    This is a leaf class in an abstract class hierarchy, it must
22    implement the unimplemented methods in parent classes.
23    """
24    def __init__(self, k):
25        super(SourceKernel, self).__init__()
26        self.__kernel = k
27        self.__patch_list = []
28        self.__config_file = None
29        self.__autotest = autotest.Autotest()
30
31
32    def configure(self, configFile):
33        self.__config_file = configFile
34
35
36    def patch(self, patchFile):
37        self.__patch_list.append(patchFile)
38
39
40    def build(self, host):
41        ctlfile = self.__control_file(self.__kernel, self.__patch_list,
42                                    self.__config_file)
43        self.__autotest.run(ctlfile, host.get_tmp_dir(), host)
44
45
46    def install(self, host):
47        self.__autotest.install(host)
48        ctlfile = ("testkernel = job.kernel('%s')\n"
49                   "testkernel.install()\n"
50                   "testkernel.add_to_bootloader()\n" %(self.__kernel))
51        self.__autotest.run(ctlfile, host.get_tmp_dir(), host)
52
53
54    def __control_file(self, kernel, patch_list, config):
55        ctl = ("testkernel = job.kernel('%s')\n" % kernel)
56
57        if len(patch_list):
58            patches = ', '.join(["'%s'" % x for x in patch_list])
59            ctl += "testkernel.patch(%s)\n" % patches
60
61        if config:
62            ctl += "testkernel.config('%s')\n" % config
63        else:
64            ctl += "testkernel.config('', None, True)\n"
65
66        ctl += "testkernel.build()\n"
67
68        # copy back to server
69
70        return ctl
71