Passed
Push — master ( d549ae...f283e8 )
by Peter
01:49
created

ensurefileexists()   A

Complexity

Conditions 3

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 3
1
import os, sys, re, time, subprocess
2
from multiprocessing import Process, Value, Array, Pipe
3
4
#------------------------------------------------
5
# constants  ------------------------------------
6
#------------------------------------------------
7
8
executeable = './dinner'
9
parameters = ['3', '10']
10
11
outputFile = 'output.txt'
12
outputFormat = '^\d+;\d+;\d+$'
13
14
#------------------------------------------------
15
# global variables ------------------------------
16
#------------------------------------------------
17
18
maximalthreadcount=Value('i',0)
19
20
#------------------------------------------------
21
# functions -------------------------------------
22
#------------------------------------------------
23
24
# polling thread body
25
def pollingthreadbody(pipeRecv):
26
    #print("Waiting for PID")
27
    processid=pipeRecv.recv()[0]
28
    #print("Got PID "+str(processid))
29
    while True:
30
        #print("Poll")
31
        prog = os.popen('cat /proc/' + str(processid) + '/status | grep Thread')
32
        threadline = prog.read()
33
        if not threadline.startswith('Threads:'):
34
            return 0
35
        #print("#"+threadline+"#")
36
        currentthreadcount=int(threadline.replace("Threads:", "").strip())
37
        if currentthreadcount > maximalthreadcount.value:
38
            print("Found %u threads in application"%currentthreadcount)
39
            maximalthreadcount.value = currentthreadcount
40
        time.sleep(100)
41
42
# execute command
43
def execute(command, parameters, pipeSend):
44
    pollingthread.start()
45
    start = time.time()
46
    proc = subprocess.Popen([command] + parameters)
47
    pipeSend.send([proc.pid])
48
    proc.communicate()
49
    end = time.time()
50
    pollingthread.terminate()
51
    threadcount = maximalthreadcount.value
52
    print("Runtime:      %.4fs" % (end - start))
53
    print("Max thread count: " + str(threadcount))
54
55
# check if file exists
56
def ensurefileexists(file):
57
    if not os.path.exists(file) or not os.path.isfile(file):
58
        errorexit("File not found: " + file, 1)
59
    
60
# exit with message and error code
61
def errorexit(message, exitcode):
62
    print("[ERROR]-----------------------------")
63
    print(message)
64
    print("------------------------------------")
65
    sys.exit(exitcode)
66
67
# check if text is in the correct format
68
def matches(text, format):
69
    match = re.search(format, text)
70
    if match is None:
71
        return False
72
    return match.group(0) != ""
73
74
# check if file content matches pattern
75
def checkcontent(f, format):
76
    text = f.read()
77
    if not matches(text, format):
78
        errorexit("Result file does not match regular expression:\n" + format + "\n\n" + outputFile + "\n" + text, 3)
79
80
# check if file content matches pattern
81
def checkfile(file, contentformat):
82
    # try open file
83
    try:
84
        with open(file) as f:
85
            checkcontent(f, contentformat)
86
    except IOError as e:
87
        errorexit("Could not open file: " + file, 2)
88
89
#------------------------------------------------
90
# program starts HERE ---------------------------
91
#------------------------------------------------
92
93
if __name__ == '__main__':
94
    pipeRecv, pipeSend = Pipe(duplex=False)
95
    pollingthread = Process(target=pollingthreadbody, args=(pipeRecv,))
96
97
    ensurefileexists(executeable)
98
    execute(executeable, parameters, pipeSend)
99
100
    ensurefileexists(outputFile)    
101
    checkfile(outputFile, outputFormat)
102