-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleaks_check.py
More file actions
60 lines (49 loc) · 1.46 KB
/
leaks_check.py
File metadata and controls
60 lines (49 loc) · 1.46 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
import logging
import re
import subprocess
import threading
from time import sleep
from utils.ExecutionContext import console
logger = logging.getLogger("leak-check")
leaks_regex = re.compile(".* (\d+) leak(?:s)? for (\d+) total.*")
class LeakException(Exception):
pass
class LeakChecker(threading.Thread):
def __init__(self, command, timeout=1, input=None):
self.stdout = None
self.stderr = None
self.pid = None
self.command = command
self.return_code = None
self.input = input
self.timeout = timeout
threading.Thread.__init__(self)
def run(self):
proc = subprocess.Popen(("leaks -q -atExit -- " + self.command).split(),
errors="backslashreplace",
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT)
self.pid = str(proc.pid)
self.stdout = ""
if self.input:
lines = self.input.splitlines(True)
for line in lines:
proc.stdin.write(line)
proc.stdin.close()
while True:
line = proc.stdout.readline()
self.stdout += line
def has_leaks(command, timeout=1.5, input=None):
checker = LeakChecker(command, timeout, input=input)
checker.daemon = True
checker.start()
sleep(1)
if not checker.stdout:
console.print("\nLeak check was not executed, do it manually\n", style="b yellow")
raise LeakException()
leaks = next(line for line in checker.stdout.splitlines() if leaks_regex.match(line))
match = leaks_regex.match(leaks)
if match.group(1) != "0":
return checker.stdout
return False