run a command in the unix shell
(command, stdin=False, outfile=None)
| 4 | import sys |
| 5 | |
| 6 | def run(command, stdin=False, outfile=None): |
| 7 | """ run a command in the unix shell """ |
| 8 | |
| 9 | sin = None |
| 10 | if stdin: sin = subprocess.PIPE |
| 11 | p0 = subprocess.Popen(command, stdin=sin, stdout=subprocess.PIPE, |
| 12 | stderr=subprocess.STDOUT, shell=True) |
| 13 | |
| 14 | stdout0 = p0.communicate() |
| 15 | if stdin: p0.stdin.close() |
| 16 | rc = p0.returncode |
| 17 | p0.stdout.close() |
| 18 | |
| 19 | if outfile is not None: |
| 20 | try: cf = io.open(outfile, "w", encoding="latin-1") |
| 21 | except IOError: |
| 22 | sys.exit("ERROR: unable to open file for writing: {}".format(outfile)) |
| 23 | else: |
| 24 | for line in stdout0: |
| 25 | if line is not None: |
| 26 | cf.write(line.decode('latin-1')) |
| 27 | cf.close() |
| 28 | |
| 29 | return stdout0, rc |
| 30 | |
| 31 | |
| 32 | class Preprocessor(object): |
no test coverage detected