]> begriffs open source - cmsis/blob - CMSIS/CoreValidation/Tests/buildutils/buildcmd.py
Aligned develop branch with master after release.
[cmsis] / CMSIS / CoreValidation / Tests / buildutils / buildcmd.py
1 #! python
2
3 import os
4 import shutil
5 from subprocess import Popen
6 from tempfile import TemporaryFile
7
8 class BuildCmd:
9   def __init__(self, env=os.environ):
10     self._result = -1
11     self._output = TemporaryFile(mode="r+")
12     self._env = env
13     
14   def getCommand(self):
15     raise NotImplementedError
16     
17   def getArguments(self):
18     return []
19     
20   def needsShell(self):
21     return False
22     
23   def getOutput(self):
24     return self._output
25
26   def getLog(self):
27     return None
28     
29   def isSuccess(self):
30     return self._output == 0
31
32   def run(self):  
33     cmd = shutil.which(self.getCommand(), path=self._env['PATH'])
34     if not cmd:
35       raise FileNotFoundError(self.getCommand() + " in PATH='" + self._env['PATH']+"'")
36     cmd = [ os.path.normpath(cmd) ] + self.getArguments()
37     print("Running: " + ' '.join(cmd))
38     try:
39       with Popen(cmd, stdout = self._output, stderr = self._output, shell=self.needsShell(), env=self._env) as proc:
40         self._result = proc.wait()
41     except:
42       print("Fatal error!")
43     self._output.seek(0)
44     print(self._output.read())
45     
46     logfile = self.getLog()
47     if logfile != None:
48       print(logfile.read())
49       
50     print("Command returned: {0}".format(self._result))
51       
52     return self._result
53     
54   def skip(self):
55     self._result = 0
56