| 28 | import subprocess |
| 29 | |
| 30 | class CMD_helper(object): |
| 31 | |
| 32 | def execute_cmd(self, command, sudo=False): |
| 33 | if sudo: |
| 34 | command = "sudo {0}".format(command) |
| 35 | #Initialize temp file for stdout. Will be removed when closed. |
| 36 | outfile = tempfile.SpooledTemporaryFile() |
| 37 | try: |
| 38 | #Invoke process |
| 39 | p = subprocess.Popen(command, stdout=outfile, stderr=subprocess.STDOUT, shell=True) |
| 40 | p.communicate() |
| 41 | #Read stdout from file |
| 42 | outfile.seek(0) |
| 43 | stdout = outfile.read() |
| 44 | except: |
| 45 | raise |
| 46 | finally: |
| 47 | #Make sure the file is closed |
| 48 | outfile.close() |
| 49 | retcode = p.returncode |
| 50 | return stdout, retcode |
| 51 | |
| 52 | def get_command_path(self, binary): |
| 53 | """Get the path to the binary.""" |
| 54 | path = os.path.dirname(os.path.abspath(__file__)) |
| 55 | return os.path.join(path, binary) |
no outgoing calls
no test coverage detected