Mercurial > repos > greg > assembly_post_processor
comparison utils.py @ 50:73b38edd22dd draft
Uploaded
| author | greg |
|---|---|
| date | Fri, 05 May 2017 09:30:44 -0400 |
| parents | 58382c8ed01c |
| children | c631e136d5e7 |
comparison
equal
deleted
inserted
replaced
| 49:9d58af3e459d | 50:73b38edd22dd |
|---|---|
| 1 import os | 1 import os |
| 2 import shutil | 2 import shutil |
| 3 import subprocess | |
| 3 import sys | 4 import sys |
| 4 | 5 |
| 6 FSTDERR = 'stderr.txt' | |
| 7 FSTDOUT = 'stdout.txt' | |
| 5 | 8 |
| 6 def check_execution_errors(rc, stderr, stdout=None): | 9 |
| 10 def check_execution_errors(rc, fstderr, fstdout): | |
| 7 if rc != 0: | 11 if rc != 0: |
| 8 if stdout is None: | 12 fh = open(fstdout, 'rb') |
| 9 stop_err(stderr.read()) | 13 out_msg = fh.read() |
| 10 msg = '%s\n%s' % (stdout.read(), stderr.read()) | 14 fh.close() |
| 15 fh = open(fstderr, 'rb') | |
| 16 err_msg = fh.read() | |
| 17 fh.close() | |
| 18 msg = '%s\n%s\n' % (str(out_msg), str(err_msg)) | |
| 19 stop_err(msg) | |
| 20 | |
| 21 | |
| 22 def get_response_buffers(): | |
| 23 fstderr = os.path.join(os.getcwd(), FSTDERR) | |
| 24 fherr = open(fstderr, 'wb') | |
| 25 fstdout = os.path.join(os.getcwd(), FSTDOUT) | |
| 26 fhout = open(fstdout, 'wb') | |
| 27 return fherr, fhout | |
| 11 | 28 |
| 12 | 29 |
| 13 def move_directory_files(source_dir, destination_dir): | 30 def move_directory_files(source_dir, destination_dir): |
| 14 source_directory = os.path.abspath(source_dir) | 31 source_directory = os.path.abspath(source_dir) |
| 15 destination_directory = os.path.abspath(destination_dir) | 32 destination_directory = os.path.abspath(destination_dir) |
| 18 for dir_entry in os.listdir(source_directory): | 35 for dir_entry in os.listdir(source_directory): |
| 19 source_entry = os.path.join(source_directory, dir_entry) | 36 source_entry = os.path.join(source_directory, dir_entry) |
| 20 shutil.move(source_entry, destination_directory) | 37 shutil.move(source_entry, destination_directory) |
| 21 | 38 |
| 22 | 39 |
| 40 def run_command(cmd): | |
| 41 fherr, fhout = get_response_buffers() | |
| 42 proc = subprocess.Popen(args=cmd, stderr=fherr, stdout=fhout, shell=True) | |
| 43 rc = proc.wait() | |
| 44 # Check results. | |
| 45 fherr.close() | |
| 46 fhout.close() | |
| 47 check_execution_errors(rc, fstderr, fstdout) | |
| 48 | |
| 49 | |
| 23 def stop_err(msg): | 50 def stop_err(msg): |
| 24 sys.stderr.write(msg) | 51 sys.exit(msg) |
| 25 sys.exit(1) | |
| 26 | 52 |
| 27 | 53 |
| 28 def write_html_output(output, title, dir): | 54 def write_html_output(output, title, dir): |
| 29 with open(output, 'w') as fh: | 55 with open(output, 'w') as fh: |
| 30 fh.write('<html><head><h3>%s</h3></head>\n' % title) | 56 fh.write('<html><head><h3>%s</h3></head>\n' % title) |
