Best Python code snippet using autotest_python
boottool.py
Source:boottool.py  
...1307            return self.boot_once_grub(entry_index)1308        elif bootloader == 'grub2':1309            return self.boot_once_grub2(entry_index)1310        elif bootloader == 'yaboot':1311            return self.boot_once_yaboot(title)1312        elif bootloader == 'elilo':1313            return self.boot_once_elilo(entry_index)1314        else:1315            self.log.error("Detected bootloader does not implement boot once")1316            return -11317    def boot_once_grub(self, entry_index):1318        '''1319        Implements the boot once feature for the grub bootloader1320        '''1321        # grubonce is a hack present in distros like OpenSUSE1322        grubonce_cmd = find_executable('grubonce')1323        if grubonce_cmd is None:1324            # XXX: check the type of default set (numeric or "saved")1325            grub_instructions = ['savedefault --default=%s --once' %1326                                 entry_index, 'quit']1327            grub_instructions_text = '\n'.join(grub_instructions)1328            grub_binary = find_executable('grub')1329            if grub_binary is None:1330                self.log.error("Could not find the 'grub' binary, aborting")1331                return -11332            p = subprocess.Popen([grub_binary, '--batch'],1333                                 stdin=subprocess.PIPE,1334                                 stdout=subprocess.PIPE,1335                                 stderr=subprocess.PIPE)1336            out, err = p.communicate(grub_instructions_text)1337            complete_out = ''1338            if out is not None:1339                complete_out = out1340            if err is not None:1341                complete_out += "\n%s" % err1342            grub_batch_err = []1343            if complete_out:1344                for l in complete_out.splitlines():1345                    if re.search('error', l, re.IGNORECASE):1346                        grub_batch_err.append(l)1347                if grub_batch_err:1348                    self.log.error("Error while running grub to set boot "1349                                   "once: %s", "\n".join(grub_batch_err))1350                    return -11351            self.log.debug('No error detected while running grub to set boot '1352                           'once')1353            return 01354        else:1355            rc = self._run_get_return([grubonce_cmd, str(entry_index)])1356            if rc:1357                self.log.error('Error running %s', grubonce_cmd)1358            else:1359                self.log.debug('No error detected while running %s',1360                               grubonce_cmd)1361            return rc1362    def boot_once_grub2(self, entry_index):1363        '''1364        Implements the boot once feature for the grub2 bootloader1365        Caveat: this assumes the default set is of type "saved", and not a1366        numeric value.1367        '''1368        default_index_re = re.compile('\s*set\s+default\s*=\s*\"+(\d+)\"+')1369        grub_reboot_names = ['grub-reboot', 'grub2-reboot']1370        grub_reboot_exec = None1371        for grub_reboot in grub_reboot_names:1372            grub_reboot_exec = find_executable(grub_reboot)1373            if grub_reboot_exec is not None:1374                break1375        if grub_reboot_exec is None:1376            self.log.error('Could not find executable among searched names: '1377                           '%s', ' ,'.join(grub_reboot_names))1378            return -11379        grub_set_default_names = ['grub-set-default', 'grub2-set-default']1380        grub_set_default_exec = None1381        for grub_set_default in grub_set_default_names:1382            grub_set_default_exec = find_executable(grub_set_default)1383            if grub_set_default_exec is not None:1384                break1385        if grub_set_default_exec is None:1386            self.log.error('Could not find executable among searched names: '1387                           '%s', ' ,'.join(grub_set_default_names))1388            return -11389        # Make sure the "set default" entry in the configuration file is set1390        # to "${saved_entry}. Assuming the config file is at /boot/grub/grub.cfg1391        deb_grub_cfg_path = '/boot/grub/grub.cfg'1392        if self._dist_uses_grub2():1393            deb_grub_cfg_path = '/boot/grub2/grub.cfg'1394        deb_grub_cfg_bkp_path = '%s.boottool.bak' % deb_grub_cfg_path1395        default_index = None1396        if os.path.exists(deb_grub_cfg_path):1397            shutil.move(deb_grub_cfg_path, deb_grub_cfg_bkp_path)1398            o = open(deb_grub_cfg_path, 'w')1399            for l in open(deb_grub_cfg_bkp_path).readlines():1400                m = default_index_re.match(l)1401                if m is not None:1402                    default_index = int(m.groups()[0])1403                    o.write('set default="${saved_entry}"\n')1404                else:1405                    o.write(l)1406            o.close()1407        # Make the current default entry the "previous saved entry"1408        if default_index is None:1409            default_index = self.get_default_index()1410        else:1411            # grubby adds entries to top. this assumes a new entry to boot once1412            # has already been added to the top, so fallback to the second1413            # entry (index 1) if the boot once entry fails to boot1414            if entry_index == 0:1415                default_index = 11416            else:1417                default_index = 01418        # A negative index is never acceptable1419        if default_index >= 0:1420            prev_saved_return = self._run_get_return([grub_set_default_exec,1421                                                      '%s' % default_index])1422            if prev_saved_return != 0:1423                self.log.error('Could not make entry %s the previous saved entry',1424                               default_index)1425                return prev_saved_return1426        # Finally set the boot once entry1427        return self._run_get_return([grub_reboot_exec,1428                                     '%s' % entry_index])1429    def boot_once_yaboot(self, entry_title):1430        '''1431        Implements the boot once feature for the yaboot bootloader1432        '''1433        nvsetenv_cmd = find_executable('nvsetenv')1434        if nvsetenv_cmd is None:1435            self.log.error("Could not find nvsetenv in PATH")1436            return -11437        return self._run_get_return([nvsetenv_cmd,1438                                     'boot-once',1439                                     entry_title])1440    def boot_once_elilo(self, entry_index):1441        '''1442        Implements boot once for machines with kernel >= 2.61443        This manipulates EFI variables via the interface available at...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!!
