Best Python code snippet using lisa_python
kernel_source_installer.py
Source:kernel_source_installer.py  
...73    def install(self) -> str:74        node = self._node75        runbook: SourceInstallerSchema = self.runbook76        assert runbook.location, "the repo must be defined."77        self._install_build_tools(node)78        factory = subclasses.Factory[BaseLocation](BaseLocation)79        source = factory.create_by_runbook(80            runbook=runbook.location, node=node, parent_log=self._log81        )82        code_path = source.get_source_code()83        assert node.shell.exists(code_path), f"cannot find code path: {code_path}"84        self._log.info(f"kernel code path: {code_path}")85        # modify code86        self._modify_code(node=node, code_path=code_path)87        self._build_code(node=node, code_path=code_path)88        self._install_build(node=node, code_path=code_path)89        result = node.execute(90            "make kernelrelease 2>/dev/null", cwd=code_path, shell=True91        )92        kernel_version = result.stdout93        result.assert_exit_code(0, f"failed on get kernel version: {kernel_version}")94        # copy current config back to system folder.95        result = node.execute(96            f"cp .config /boot/config-{kernel_version}", cwd=code_path, sudo=True97        )98        result.assert_exit_code()99        return kernel_version100    def _install_build(self, node: Node, code_path: PurePath) -> None:101        make = node.tools[Make]102        make.make(arguments="modules", cwd=code_path, sudo=True)103        make.make(arguments="modules_install", cwd=code_path, sudo=True)104        make.make(arguments="install", cwd=code_path, sudo=True)105        # The build for Redhat needs extra steps than RPM package. So put it106        # here, not in OS.107        if isinstance(node.os, Redhat):108            result = node.execute("grub2-set-default 0", sudo=True)109            result.assert_exit_code()110            result = node.execute("grub2-mkconfig -o /boot/grub2/grub.cfg", sudo=True)111            result.assert_exit_code()112    def _modify_code(self, node: Node, code_path: PurePath) -> None:113        runbook: SourceInstallerSchema = self.runbook114        if not runbook.modifier:115            return116        modifier_runbooks: List[BaseModifierSchema] = runbook.modifier117        assert isinstance(118            modifier_runbooks, list119        ), f"modifier must be a list, but it's {type(modifier_runbooks)}"120        factory = subclasses.Factory[BaseModifier](BaseModifier)121        for modifier_runbook in modifier_runbooks:122            modifier = factory.create_by_runbook(123                runbook=modifier_runbook,124                node=node,125                code_path=code_path,126                parent_log=self._log,127            )128            self._log.debug(f"modifying code by {modifier.type_name()}")129            modifier.modify()130    def _build_code(self, node: Node, code_path: PurePath) -> None:131        self._log.info("building code...")132        uname = node.tools[Uname]133        kernel_information = uname.get_linux_information()134        result = node.execute(135            f"cp /boot/config-{kernel_information.kernel_version_raw} .config",136            cwd=code_path,137        )138        result.assert_exit_code()139        config_path = code_path.joinpath(".config")140        sed = self._node.tools[Sed]141        sed.substitute(142            regexp="CONFIG_DEBUG_INFO_BTF=.*",143            replacement="CONFIG_DEBUG_INFO_BTF=no",144            file=str(config_path),145            sudo=True,146        )147        # workaround failures.148        #149        # make[1]: *** No rule to make target 'debian/canonical-certs.pem',150        # needed by 'certs/x509_certificate_list'.  Stop.151        #152        # make[1]: *** No rule to make target 'certs/rhel.pem', needed by153        # 'certs/x509_certificate_list'.  Stop.154        result = node.execute(155            "scripts/config --disable SYSTEM_TRUSTED_KEYS",156            cwd=code_path,157            shell=True,158        )159        result.assert_exit_code()160        # workaround failures.161        #162        # make[1]: *** No rule to make target 'debian/canonical-revoked-certs.pem',163        # needed by 'certs/x509_revocation_list'.  Stop.164        result = node.execute(165            "scripts/config --disable SYSTEM_REVOCATION_KEYS",166            cwd=code_path,167            shell=True,168        )169        result.assert_exit_code()170        # the gcc version of Redhat 7.x is too old. Upgrade it.171        if isinstance(node.os, Redhat) and node.os.information.version < "8.0.0":172            node.os.install_packages(["devtoolset-8"])173            result = node.execute("mv /bin/gcc /bin/gcc_back", sudo=True)174            result.assert_exit_code()175            result = node.execute(176                "ln -s /opt/rh/devtoolset-8/root/usr/bin/gcc /bin/gcc", sudo=True177            )178            result.assert_exit_code()179        make = node.tools[Make]180        make.make(arguments="olddefconfig", cwd=code_path)181        # set timeout to 2 hours182        make.make(arguments="", cwd=code_path, timeout=60 * 60 * 2)183    def _install_build_tools(self, node: Node) -> None:184        os = node.os185        self._log.info("installing build tools")186        if isinstance(os, Redhat):187            for package in list(188                ["elfutils-libelf-devel", "openssl-devel", "dwarves", "bc"]189            ):190                if os.is_package_in_repo(package):191                    os.install_packages(package)192            os.group_install_packages("Development Tools")193            if os.information.version < "8.0.0":194                # git from default CentOS/RedHat 7.x does not support git tag format195                # syntax temporarily use a community repo, then remove it196                node.execute("yum remove -y git", sudo=True)197                node.execute(...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
