(cmd, verbose=True)
| 27 | |
| 28 | |
| 29 | def execute(cmd, verbose=True): |
| 30 | if verbose: |
| 31 | print("CMD> %s" % cmd) |
| 32 | p = subprocess.Popen([cmd], |
| 33 | shell=True, |
| 34 | stdout=subprocess.PIPE, |
| 35 | stderr=subprocess.STDOUT, |
| 36 | stdin=subprocess.PIPE, |
| 37 | universal_newlines=True) |
| 38 | |
| 39 | if not verbose: |
| 40 | # use p.communicate instead of p.wait to avoid such situation: |
| 41 | # pipe is filled and the child process is blocked. |
| 42 | out, err = p.communicate() |
| 43 | if p.returncode != 0: |
| 44 | raise Exception("errorcode: {}".format(p.returncode)) |
| 45 | return out |
| 46 | |
| 47 | buf = [] |
| 48 | |
| 49 | while p.poll() is None: |
| 50 | line = p.stdout.readline().strip() |
| 51 | if verbose: |
| 52 | print(line) |
| 53 | buf.append(line) |
| 54 | |
| 55 | for li in p.stdout: |
| 56 | line = li.strip() |
| 57 | if verbose: |
| 58 | print(line) |
| 59 | buf.append(line) |
| 60 | |
| 61 | if p.returncode != 0: |
| 62 | if verbose: |
| 63 | print(line) |
| 64 | raise Exception("errorcode: %s" % p.returncode) |
| 65 | |
| 66 | return "\n".join(buf) |
| 67 | |
| 68 | |
| 69 | class Device(object): |
no outgoing calls
no test coverage detected