File size: 5,801 Bytes
a103028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

import subprocess
import threading
import logging as log

from .Common import *
from .Device import *

def PrintLog(executable_path, log_output):
        
    for _line in log_output.split('\n'):

        LogTokenCharCount = 4
        logLevel = log.getLevelName(_line[0:LogTokenCharCount])
        logText = _line[LogTokenCharCount+1:]

        if len(logText.strip()) > 0:

            if logLevel == log.FATAL:
                log.fatal("[{executable_path}] {logText}".format(executable_path = executable_path, logText = logText))
            elif logLevel == log.ERROR:
                log.error("[{executable_path}] {logText}".format(executable_path = executable_path, logText = logText))                
            elif logLevel == log.WARNING:
                log.warning("[{executable_path}] {logText}".format(executable_path = executable_path, logText = logText))
            elif logLevel == log.INFO:
                if log.getLogger().getEffectiveLevel() < logLevel:
                    log.info("[{executable_path}] {logText}".format(executable_path = executable_path, logText = logText))
            elif logLevel == log.DEBUG:
                if log.getLogger().getEffectiveLevel() < logLevel:
                    log.debug("[{executable_path}] {logText}".format(executable_path = executable_path, logText = logText))

def ExecuteCommand( command, checkForError = True, success_code = 0 ):

    __process = None

    log.debug("Running " + command)

    if platform.system() == 'Windows':
        # Cannot redirect stdout, stderr AND set close_fds=True on Windows
        __process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
    else:
        __process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, executable="/bin/bash")

    __stdout, __stderr = __process.communicate()
    __stdout = __stdout.decode('UTF-8')
    __stderr = __stderr.decode('UTF-8')

    if checkForError:
        if __process.returncode != success_code:
            print(__stdout)
            print(__stderr)
            log.fatal(
                f"Command '{command}' failed with return code {__process.returncode}, expected {success_code}\n"
                f"STDOUT:\n{__stdout}\nSTDERR:\n{__stderr}"
            )

    executable_path = "none"
    tokens = command.split(" ")
    if len(tokens) > 0:
        executable_path = tokens[0]
    PrintLog(executable_path, __stderr)

    return __stdout, __stderr, __process.returncode

def ExecuteFunction(functionptr, functionargs):

    log.info( 'FUNC: ' + str(functionptr) + "(" + ",".join( str(w) for w in functionargs ) + ")" )
    
    return functionptr( *functionargs )    

def JoinThreads(threads):
    
    join_count = 0

    for t in threads:

        if t.is_alive() == False:

            retcode = t.join()
            join_count = join_count + 1

            threads.remove(t)

    return join_count   

def ExecuteCommandList(commandlist, parallelism="auto"):
    threads = []
    running = 0

    if parallelism == "auto":
        parallelism = str(GetNumberOfVirtualCores())
    else:
        parallelism = str(parallelism)

    if len(commandlist) > 0:
        log.info("Running "+ str(len(commandlist)) + " commands" + " (" + parallelism + " in parallel)" )

    for cmd in commandlist:

        # TODO(ADEEL) - Will Fix Later
        # stdout = None
        # if 'stdout' in cmd.keys():
        #     stdout = cmd['stdout']

        # stderr = None
        # if 'stderr' in cmd.keys():
        #     stderr = cmd['stderr']

        success_code = 0
        if 'success_code' in cmd.keys():
            success_code = cmd['success_code']

        if "command" in cmd.keys():            
            command = cmd['command']

            if parallelism == "1" or len(commandlist) == 1:
                ExecuteCommand(command, True, success_code)
            else:
                thread = threading.Thread(target = ExecuteCommand, args=[command, True, success_code])
                thread.start()
                thread.setName(command)

                threads.append( thread )

                running = running + 1

                while str(running) == parallelism:
                    running = running - JoinThreads(threads)

    while len(threads):
        JoinThreads(threads)

def ExecuteFunctionList(functionlist, parallelism="auto"):
    threads = []
    running = 0

    if parallelism == "auto":
        parallelism = str(GetNumberOfVirtualCores())
    else:
        parallelism = str(parallelism)

    log.info("Running "+ str(len(functionlist)) + " functions" + " (" + parallelism + " in parallel)" )

    for function in functionlist:

        functionptr     = function['function']
        functionargs    = function['arguments']
        functionname    = str(functionptr) + "(" + str(functionargs[0]) + "...)"

        if parallelism == "1" or len(functionlist) == 1:
            ExecuteFunction(functionptr, functionargs)
        else:
            log.info( 'LAUNCH: ' + functionname )
            thread = threading.Thread( target = ExecuteFunction, args =[functionptr, functionargs] )
            thread.start()
            thread.setName(functionname)
            
            threads.append( thread )

            running = running + 1

            while str(running) == parallelism:            
                running = running - JoinThreads(threads)

    while len(threads):
        JoinThreads(threads)

def ExecuteCommandInstantOutput( command, checkForError = True ):

    log.info(command)

    returncode = os.system(command)

    if checkForError:
        if returncode != 0:
            log.fatal('Failed command ' + command + ' | returned: ' +  str(returncode))

    return returncode;