Last active
March 10, 2017 10:06
-
-
Save ntk148v/d2ead32db77bf90940b22aac004b27c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import logging | |
| import platform | |
| import subprocess | |
| import psutil | |
| LOG = logging.getLogger(__name__) | |
| class ProcessHandler(object): | |
| def __init__(self, process_name): | |
| self.process_name = process_name.strip() | |
| if self._check_platform() == 'Windows': | |
| self.process_name = self.process_name + '.exe' | |
| def _get_process_by_name(self): | |
| """Return list of process by name""" | |
| processes = [proc for proc in psutil.process_iter( | |
| ) if proc.name() == self.process_name] | |
| if len(processes) == 0: | |
| msg = ('Unable to find %s process', self.process_name) | |
| LOG.error(msg) | |
| raise Exception(msg) | |
| return processes | |
| def _check_platform(self): | |
| """Check current platform""" | |
| return platform.system() | |
| def do_action(self, action): | |
| """Run process's method""" | |
| processes = self._get_process_by_name() | |
| result = [] | |
| for proc in processes: | |
| try: | |
| result.append(getattr(proc, action)()) | |
| except Exception as e: | |
| LOG.error('Process doesnt have method %s', action) | |
| raise e | |
| return result | |
| if __name__ == '__main__': | |
| _handler = ProcessHandler('googledrivesync') | |
| _process_exe_path = _handler.do_action('exe') | |
| print(_handler.do_action('terminate')) | |
| # Re-Start Application | |
| subprocess.Popen(_path[0], stdout=subprocess.PIPE, shell=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment