Best Python code snippet using lisa_python
shell.py
Source:shell.py  
...280            mode: directory creation mode (Posix targets only)281            parents: make parent directories as needed282            exist_ok: return with no error if target already present283        """284        path_str = self._purepath_to_str(path)285        self.initialize()286        assert self._inner_shell287        try:288            self._inner_shell.mkdir(289                path_str, mode=mode, parents=parents, exist_ok=exist_ok290            )291        except PermissionError:292            self._inner_shell.run(command=["sudo", "mkdir", "-p", path_str])293        except SSHException as identifier:294            # no sftp, try commands295            if "Channel closed." in str(identifier):296                assert isinstance(path_str, str)297                self.spawn(command=["mkdir", "-p", path_str])298    def exists(self, path: PurePath) -> bool:299        """Check if a target directory/file exist300        Inputs:301            path: target path. (Absolute. Use a PurePosixPath, if the302                                target node is a Posix one, because LISA303                                might be ran from Windows)304        Outputs:305            bool: True if present, False otherwise306        """307        self.initialize()308        assert self._inner_shell309        path_str = self._purepath_to_str(path)310        return cast(bool, self._inner_shell.exists(path_str))311    def remove(self, path: PurePath, recursive: bool = False) -> None:312        """Remove a target directory/file313        Inputs:314            path: target path. (Absolute. Use a PurePosixPath, if the315                                target node is a Posix one, because LISA316                                might be ran from Windows)317            recursive: whether to remove recursively, if target is a directory318                       (will fail if that's the case and this flag is off)319        """320        self.initialize()321        assert self._inner_shell322        path_str = self._purepath_to_str(path)323        try:324            self._inner_shell.remove(path_str, recursive)325        except PermissionError:326            self._inner_shell.run(command=["sudo", "rm", path_str])327        except SSHException as identifier:328            # no sftp, try commands329            if "Channel closed." in str(identifier):330                assert isinstance(path_str, str)331                self.spawn(command=["rm", path_str])332    def chmod(self, path: PurePath, mode: int) -> None:333        """Change the file mode bits of each given file according to mode (Posix targets only)334        Inputs:335            path: target path. (Absolute. Use a PurePosixPath, if the336                                target node is a Posix one, because LISA337                                might be ran from Windows)338            mode: numerical chmod mode entry339        """340        self.initialize()341        assert self._inner_shell342        path_str = self._purepath_to_str(path)343        self._inner_shell.chmod(path_str, mode)344    def stat(self, path: PurePath) -> os.stat_result:345        """Display file/directory status.346        Inputs:347            path: target path. (Absolute. Use a PurePosixPath, if the348                                target node is a Posix one, because LISA349                                might be ran from Windows)350        Outputs:351            os.stat_result: The status structure/class352        """353        self.initialize()354        assert self._inner_shell355        path_str = self._purepath_to_str(path)356        sftp_attributes: paramiko.SFTPAttributes = self._inner_shell.stat(path_str)357        result = os.stat_result(358            (359                # st_mode360                sftp_attributes.st_mode if sftp_attributes.st_mode is not None else 0,361                # st_ino362                0,363                # st_dev364                0,365                # st_nlink366                0,367                # st_uid368                sftp_attributes.st_uid if sftp_attributes.st_uid is not None else 0,369                # st_gid370                sftp_attributes.st_gid if sftp_attributes.st_gid is not None else 0,371                # st_size372                sftp_attributes.st_size if sftp_attributes.st_size is not None else 0,373                # st_atime374                sftp_attributes.st_atime if sftp_attributes.st_atime is not None else 0,375                # st_mtime376                sftp_attributes.st_mtime if sftp_attributes.st_mtime is not None else 0,377                # st_ctime378                0,379            )380        )381        return result382    def is_dir(self, path: PurePath) -> bool:383        """Check if given path is a directory384        Inputs:385            path: target path. (Absolute. Use a PurePosixPath, if the386                                target node is a Posix one, because LISA387                                might be ran from Windows)388        Outputs:389            bool: True if it is a directory, False otherwise390        """391        self.initialize()392        assert self._inner_shell393        path_str = self._purepath_to_str(path)394        return cast(bool, self._inner_shell.is_dir(path_str))395    def is_symlink(self, path: PurePath) -> bool:396        """Check if given path is a symlink397        Inputs:398            path: target path. (Absolute. Use a PurePosixPath, if the399                                target node is a Posix one, because LISA400                                might be ran from Windows)401        Outputs:402            bool: True if it is a symlink, False otherwise403        """404        self.initialize()405        assert self._inner_shell406        path_str = self._purepath_to_str(path)407        return cast(bool, self._inner_shell.is_symlink(path_str))408    def symlink(self, source: PurePath, destination: PurePath) -> None:409        """Create a symbolic link from source to destination, in the target node410        Inputs:411            source: source path. (Absolute. Use a PurePosixPath, if the412                                 target node is a Posix one, because LISA413                                 might be ran from Windows)414            destination: destination path. (Absolute. Use a PurePosixPath, if the415                                            target node is a Posix one, because LISA416                                            might be ran from Windows)417        """418        self.initialize()419        assert self._inner_shell420        source_str = self._purepath_to_str(source)421        destination_str = self._purepath_to_str(destination)422        self._inner_shell.symlink(source_str, destination_str)423    def copy(self, local_path: PurePath, node_path: PurePath) -> None:424        """Upload local file to target node425        Inputs:426            local_path: local path. (Absolute. Use a PurePosixPath, if the427                                     target node is a Posix one, because LISA428                                     might be ran from Windows)429            node_path: target path. (Absolute. Use a PurePosixPath, if the430                                     target node is a Posix one, because LISA431                                     might be ran from Windows)432        """433        self.mkdir(node_path.parent, parents=True, exist_ok=True)434        self.initialize()435        assert self._inner_shell436        local_path_str = self._purepath_to_str(local_path)437        node_path_str = self._purepath_to_str(node_path)438        self._inner_shell.put(439            local_path_str,440            node_path_str,441            create_directories=True,442            consistent=self.is_posix,443        )444    def copy_back(self, node_path: PurePath, local_path: PurePath) -> None:445        """Download target node's file to local node446        Inputs:447            local_path: local path. (Absolute. Use a PurePosixPath, if the448                                     target node is a Posix one, because LISA449                                     might be ran from Windows)450            node_path: target path. (Absolute. Use a PurePosixPath, if the451                                     target node is a Posix one, because LISA452                                     might be ran from Windows)453        """454        self.initialize()455        assert self._inner_shell456        node_path_str = self._purepath_to_str(node_path)457        local_path_str = self._purepath_to_str(local_path)458        self._inner_shell.get(459            node_path_str,460            local_path_str,461            consistent=self.is_posix,462        )463    def _purepath_to_str(464        self, path: Union[Path, PurePath, str]465    ) -> Union[Path, PurePath, str]:466        """467        spurplus doesn't support pure path, so it needs to convert.468        """469        if isinstance(path, PurePath):470            path = str(path)471        return path472    def _establish_jump_boxes(self, address: str, port: int) -> Any:473        jump_boxes_runbook = development.get_jump_boxes()474        sock: Any = None475        is_trace_enabled = development.is_trace_enabled()476        if is_trace_enabled:477            jb_logger = _get_jump_box_logger()...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!!
