Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import re
- from sys import platform
- class proc_man:
- os_commands = {
- 'get_proc_info': {
- 'win32': {
- 'command': 'wmic process where description="{}.exe" get processid, Commandline /format:list',
- 'splitter': '\n\n' * 2,
- },
- 'linux': {
- 'command': 'ps -fC {}',
- 'splitter': '\n'
- },
- 'darwin': None
- }
- }
- @classmethod
- def get_process_by(cls, name='python'):
- """
- get processes by name for Windows
- :param name:
- :return:
- """
- # cmd = "wmic.exe path Win32_Process where description='python.exe' get Commandline /format:list"
- # cmd = "wmic.exe path Win32_Process where handle='8828' get Commandline /format:htable"
- # cmd = 'wmic process where description="python.exe" get processid, Commandline /format:list'
- # ps = subprocess.check_output(cmd).split(b'\r\r\n\r\r\n')
- cmd, sep = '', ''
- cmd, sep = cls.os_commands.get('get_proc_info', {}).get(platform, 'linux').values()
- ps = subprocess.getoutput(cmd.format(name)).split(sep)
- ps_list = cls.get_proc_args(ps)
- # ps = [p.lstrip().split() for p in ps if p]
- # args = [''.join(ps[1][1:-1]) for p in ps]
- return [ps[1:-1] for ps in ps_list]
- @classmethod
- def get_proc_args(cls, ps):
- if platform.startswith('win'):
- ps = [str(p.lstrip()) for p in ps if p.strip()]
- ps_list = []
- pos = 0
- for p in ps:
- args_list = []
- files = re.finditer('"[\s\S]*"', p)
- for file in files:
- args_list.extend(p[pos:file.start()].split())
- args_list.append(file.group()) # .replace('\\\\', '\\')
- pos = file.end()
- args_list.extend(p[pos:].split())
- ps_list.append(args_list)
- return ps_list
- elif platform.startswith('linux'):
- headers = len(ps[0].split())
- args = [p.split()[headers - 1:] + ['ProcessId=' + p.split()[1]] for p in ps[1:]]
- return args
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement