Best Python code snippet using lisa_python
service.py
Source:service.py
...31 self._internal_tool.enable_service(name) # type: ignore32 def check_service_status(self, name: str) -> bool:33 return self._internal_tool._check_service_running(name) # type: ignore34 def check_service_exists(self, name: str) -> bool:35 return self._internal_tool._check_service_exists(name) # type: ignore36class ServiceInternal(Tool):37 @property38 def command(self) -> str:39 return "service"40 @property41 def can_install(self) -> bool:42 return False43 def _check_exists(self) -> bool:44 return True45 def _check_service_exists(self, name: str) -> bool:46 cmd_result = self.run(f"{name} status", shell=True, sudo=True, force_run=True)47 if "unrecognized service" in cmd_result.stdout:48 return False49 return True50 def _check_service_running(self, name: str) -> bool:51 cmd_result = self.run(f"{name} status", shell=True, sudo=True, force_run=True)52 return (53 "unrecognized service" not in cmd_result.stdout54 and 0 == cmd_result.exit_code55 )56 def stop_service(self, name: str) -> None:57 if self._check_service_running(name):58 cmd_result = self.run(f"{name} stop", shell=True, sudo=True, force_run=True)59 cmd_result.assert_exit_code()60 def restart_service(self, name: str, ignore_exit_code: int = 0) -> None:61 cmd_result = self.run(f"{name} restart", shell=True, sudo=True, force_run=True)62 # optionally ignore exit code if it matches our expected non-zero value63 _check_error_codes(cmd_result, ignore_exit_code)64class Systemctl(Tool):65 @property66 def command(self) -> str:67 return "systemctl"68 @property69 def can_install(self) -> bool:70 return False71 def stop_service(self, name: str) -> None:72 if self._check_service_running(name):73 cmd_result = self.run(f"stop {name}", shell=True, sudo=True, force_run=True)74 cmd_result.assert_exit_code()75 def restart_service(self, name: str, ignore_exit_code: int = 0) -> None:76 cmd_result = self.run(f"restart {name}", shell=True, sudo=True, force_run=True)77 _check_error_codes(cmd_result, ignore_exit_code)78 def enable_service(self, name: str) -> None:79 cmd_result = self.run(f"enable {name}", shell=True, sudo=True, force_run=True)80 cmd_result.assert_exit_code()81 def hibernate(self) -> None:82 self.run_async("hibernate", sudo=True, force_run=True)83 def _check_exists(self) -> bool:84 return True85 def _check_service_exists(self, name: str) -> bool:86 cmd_result = self.run(87 f"--full --no-pager status {name}", shell=True, sudo=True, force_run=True88 )89 if (90 "could not be found" in cmd_result.stdout91 or "not-found" in cmd_result.stdout92 ):93 return False94 return True95 def _check_service_running(self, name: str) -> bool:96 cmd_result = self.run(97 f"--full --no-pager status {name}", shell=True, sudo=True, force_run=True98 )99 return (...
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!!