Repository navigation
Expand file tree
/
Copy pathprocess.py
More file actions
135 lines (123 loc) · 4 KB
/
Copy pathprocess.py
File metadata and controls
135 lines (123 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
Process utils.
"""
import time
import psutil
from core.osutils.os_type import OSType
from core.settings.settings import CURRENT_OS
class Process(object):
@staticmethod
def is_running(proc_name):
"""Check if process is running"""
result = False
for proc in psutil.process_iter():
if proc_name in str(proc):
result = True
break
return result
@staticmethod
def is_running_by_commandline(commandline):
"""Check if process with specified commandline is running"""
proc = Process.get_proc_by_commandline(commandline=commandline)
if proc is not None:
return True
else:
return False
@staticmethod
def get_proc_by_commandline(commandline):
"""
Get process by commandline.
:param commandline: Sub string of process commandline.
:return: Process.
"""
result = None
for proc in psutil.process_iter():
cmdline = ""
try:
cmdline = str(proc.cmdline())
except:
continue
if commandline in cmdline:
result = proc
break
return result
@staticmethod
def wait_until_running(proc_name, timeout=60):
"""Wait until process is running
:param proc_name: Process name.
:param timeout: Timeout in seconds.
:return: True if running, false if not running.
"""
running = False
end_time = time.time() + timeout
while not running:
time.sleep(5)
running = Process.is_running(proc_name)
if running:
running = True
break
if (running is False) and (time.time() > end_time):
raise NameError("{0} not running in {1} seconds.", proc_name, timeout)
return running
@staticmethod
def kill(proc_name, proc_cmdline=None):
if CURRENT_OS is OSType.WINDOWS:
proc_name += ".exe"
result = False
for proc in psutil.process_iter():
name = ""
cmdline = ""
try:
name = str(proc.name())
cmdline = str(proc.cmdline())
except:
continue
if proc_name == name:
if (proc_cmdline is None) or (proc_cmdline is not None and proc_cmdline in cmdline):
try:
proc.kill()
print "Process {0} has been killed.".format(proc_name)
result = True
except psutil.NoSuchProcess:
continue
return result
@staticmethod
def kill_by_commandline(cmdline):
result = False
for proc in psutil.process_iter():
cmd = ""
try:
name = str(proc.name())
cmd = str(proc.cmdline())
except:
continue
if (cmdline in cmd):
try:
proc.kill()
print "Process {0} has been killed.".format(cmdline)
result = True
except psutil.NoSuchProcess:
continue
return result
@staticmethod
def kill_by_handle(file_path):
for proc in psutil.process_iter():
try:
for item in proc.open_files():
if file_path in item.path:
print "{0} is locked by {1}".format(file_path, proc.name())
print "Proc cmd: {0}".format(proc.cmdline())
proc.kill()
except:
continue
@staticmethod
def list():
for proc in psutil.process_iter():
name = ""
cmdline = ""
try:
name = str(proc.name())
cmdline = str(proc.cmdline())
except:
continue
print "{0} {1}".format(name, cmdline)