Best Python code snippet using autotest_python
profilers.py
Source:profilers.py  
...89            return self.installed_hosts.values()90        if host.hostname in self.installed_hosts:91            return [self.installed_hosts[host.hostname]]92        return []93    def _get_local_profilers_dir(self, test, hostname):94        in_machine_dir = (95                os.path.basename(test.job.resultdir) in test.job.machines)96        if len(test.job.machines) > 1 and not in_machine_dir:97            local_dir = os.path.join(test.profdir, hostname)98            if not os.path.exists(local_dir):99                os.makedirs(local_dir)100        else:101            local_dir = test.profdir102        return local_dir103    def _get_failure_logs(self, autodir, test, host):104        """105        Collect the client logs from a profiler run and put them in a106        file named failure-*.log.107        """108        try:109            fd, path = tempfile.mkstemp(suffix='.log', prefix='failure-',110                    dir=self._get_local_profilers_dir(test, host.hostname))111            os.close(fd)112            host.get_file(get_profiler_log_path(autodir), path)113            # try to collect any partial profiler logs114            self._get_profiler_logs(autodir, test, host)115        except (error.AutotestError, error.AutoservError):116            logging.exception('Profiler failure log collection failed')117            # swallow the exception so that we don't override an existing118            # exception being thrown119    def _get_all_failure_logs(self, test, hosts):120        for host, at, autodir in hosts:121            self._get_failure_logs(autodir, test, host)122    def _get_profiler_logs(self, autodir, test, host):123        results_dir = get_profiler_results_dir(autodir)124        local_dir = self._get_local_profilers_dir(test, host.hostname)125        self.job.remove_client_log(host.hostname, results_dir, local_dir)126        tempdir = tempfile.mkdtemp(dir=self.job.tmpdir)127        try:128            host.get_file(results_dir + '/', tempdir)129        except error.AutoservRunError:130            pass # no files to pull back, nothing we can do131        utils.merge_trees(tempdir, local_dir)132        shutil.rmtree(tempdir, ignore_errors=True)133    def _run_clients(self, test, hosts):134        """135        We initialize the profilers just before start because only then we136        know all the hosts involved.137        """138        hostnames = [host_info[0].hostname for host_info in hosts]139        profilers_args = [(p.name, p.args, p.dargs)140                          for p in self.list]141        for host, at, autodir in hosts:142            control_script = standalone_profiler.generate_test(hostnames,143                                                               host.hostname,144                                                               profilers_args,145                                                               180, None)146            try:147                at.run(control_script, background=True)148            except Exception:149                self._get_failure_logs(autodir, test, host)150                raise151            remote_results_dir = get_profiler_results_dir(autodir)152            local_results_dir = self._get_local_profilers_dir(test,153                                                              host.hostname)154            self.job.add_client_log(host.hostname, remote_results_dir,155                                    local_results_dir)156        try:157            # wait for the profilers to be added158            standalone_profiler.wait_for_profilers(hostnames)159        except Exception:160            self._get_all_failure_logs(test, hosts)161            raise162    def before_start(self, test, host=None):163        # create host objects and install the needed clients164        # so later in start() we don't spend too much time165        self._install_clients()166        self._run_clients(test, self._get_hosts(host))...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!!
