Best Python code snippet using autotest_python
base_sysinfo.py
Source:base_sysinfo.py  
...268            except Exception, e:269                logging.error("error running journalctl --show-cursor: %s", e)270        # log some sysinfo data into the test keyval file in case system crash.271        test_sysinfodir = self._get_sysinfodir(test.outputdir)272        keyval = self.log_test_keyvals(test_sysinfodir)273        test.write_test_keyval(keyval)274    @log.log_and_ignore_errors("post-test sysinfo error:")275    def log_after_each_test(self, test):276        """Logging hook called after a test finishs.277        @param test: A test object.278        """279        test_sysinfodir = self._get_sysinfodir(test.outputdir)280        # create a symlink in the test sysinfo dir to the current boot281        reboot_dir = self._get_boot_subdir()282        assert os.path.exists(reboot_dir)283        symlink_dest = os.path.join(test_sysinfodir, "reboot_current")284        symlink_src = utils.get_relative_path(reboot_dir,285                                              os.path.dirname(symlink_dest))286        try:287            os.symlink(symlink_src, symlink_dest)288        except Exception, e:289            raise Exception, '%s: whilst linking %s to %s' % (e, symlink_src,290                                                              symlink_dest)291        # run all the standard logging commands292        _run_loggables_ignoring_errors(self.test_loggables, test_sysinfodir)293        # grab any new data from /var/log/messages294        self._log_messages(test_sysinfodir)295        # grab any new data from systemd journal296        self._log_journal(test_sysinfodir)297        # log some sysinfo data into the test keyval file298        keyval = self.log_test_keyvals(test_sysinfodir)299        test.write_test_keyval(keyval)300        # log any changes to installed packages301        old_packages = set(self._installed_packages)302        new_packages = set(package.list_all())303        added_path = os.path.join(test_sysinfodir, "added_packages")304        added_packages = "\n".join(new_packages - old_packages) + "\n"305        utils.open_write_close(added_path, added_packages)306        removed_path = os.path.join(test_sysinfodir, "removed_packages")307        removed_packages = "\n".join(old_packages - new_packages) + "\n"308        utils.open_write_close(removed_path, removed_packages)309    @log.log_and_ignore_errors("pre-test siteration sysinfo error:")310    def log_before_each_iteration(self, test, iteration=None):311        """Logging hook called before a test iteration.312        @param test: A test object.313        @param iteration: A test iteration.314        """315        if not iteration:316            iteration = test.iteration317        logdir = self._get_iteration_subdir(test, iteration)318        _run_loggables_ignoring_errors(self.before_iteration_loggables, logdir)319        # Start each log with the board name for orientation.320        board = utils.get_board_with_frequency_and_memory()321        logging.info('ChromeOS BOARD = %s', board)322        # Leave some autotest bread crumbs in the system logs.323        utils.system('logger "autotest starting iteration %s on %s"' % (logdir,324                                                                        board),325                     ignore_status=True)326    @log.log_and_ignore_errors("post-test siteration sysinfo error:")327    def log_after_each_iteration(self, test, iteration=None):328        """Logging hook called after a test iteration.329        @param test: A test object.330        @param iteration: A test iteration.331        """332        if not iteration:333            iteration = test.iteration334        logdir = self._get_iteration_subdir(test, iteration)335        _run_loggables_ignoring_errors(self.after_iteration_loggables, logdir)336        utils.system('logger "autotest finished iteration %s"' % logdir,337                     ignore_status=True)338    def _log_messages(self, logdir):339        """Log all of the new data in /var/log/messages."""340        try:341            # log all of the new data in /var/log/messages342            bytes_to_skip = 0343            if hasattr(self, "_messages_size"):344                current_inode = os.stat("/var/log/messages").st_ino345                if current_inode == self._messages_inode:346                    bytes_to_skip = self._messages_size347            in_messages = open("/var/log/messages")348            in_messages.seek(bytes_to_skip)349            out_messages = open(os.path.join(logdir, "messages"), "w")350            out_messages.write(in_messages.read())351            in_messages.close()352            out_messages.close()353        except Exception, e:354            logging.error("/var/log/messages collection failed with %s", e)355    def _log_journal(self, logdir):356        """Log all of the new data in systemd journal."""357        if not hasattr(self, "_journal_cursor"):358            return359        cmd = "/usr/bin/journalctl --after-cursor \"%s\"" % (360            self._journal_cursor)361        try:362            with open(os.path.join(logdir, "journal"), "w") as journal:363              journal.write(utils.system_output(cmd))364        except Exception, e:365            logging.error("journal collection failed with %s", e)366    @staticmethod367    def _read_sysinfo_keyvals(loggables, logdir):368        keyval = {}369        for log in loggables:370            if log.log_in_keyval:371                keyval["sysinfo-" + log.logf] = log.readline(logdir)372        return keyval373    def log_test_keyvals(self, test_sysinfodir):374        """Generate keyval for the sysinfo.375        Collects keyval entries to be written in the test keyval.376        @param test_sysinfodir: The test's system info directory.377        """378        keyval = {}379        # grab any loggables that should be in the keyval380        keyval.update(self._read_sysinfo_keyvals(self.test_loggables,381                                                 test_sysinfodir))382        keyval.update(self._read_sysinfo_keyvals(383            self.boot_loggables, os.path.join(test_sysinfodir,384                                              'reboot_current')))385        # remove hostname from uname info386        #   Linux lpt36 2.6.18-smp-230.1 #1 [4069269] SMP Fri Oct 24 11:30:...387        if "sysinfo-uname" in keyval:...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!!
