Mercurial > repos > greg > gene_family_integrator
comparison utils.py @ 14:0dd84595cf37 draft
Uploaded
author | greg |
---|---|
date | Fri, 05 May 2017 09:33:13 -0400 |
parents | 109a0eb7791f |
children | 27c0651f04e3 |
comparison
equal
deleted
inserted
replaced
13:49bdc3b38b4a | 14:0dd84595cf37 |
---|---|
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): | 9 |
10 def check_execution_errors(rc, fstderr, fstdout): | |
7 if rc != 0: | 11 if rc != 0: |
8 stop_err(stderr.read()) | 12 fh = open(fstdout, 'rb') |
13 out_msg = fh.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 | |
9 | 28 |
10 | 29 |
11 def move_directory_files(source_dir, destination_dir): | 30 def move_directory_files(source_dir, destination_dir): |
12 source_directory = os.path.abspath(source_dir) | 31 source_directory = os.path.abspath(source_dir) |
13 destination_directory = os.path.abspath(destination_dir) | 32 destination_directory = os.path.abspath(destination_dir) |
16 for dir_entry in os.listdir(source_directory): | 35 for dir_entry in os.listdir(source_directory): |
17 source_entry = os.path.join(source_directory, dir_entry) | 36 source_entry = os.path.join(source_directory, dir_entry) |
18 shutil.move(source_entry, destination_directory) | 37 shutil.move(source_entry, destination_directory) |
19 | 38 |
20 | 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 | |
21 def stop_err(msg): | 50 def stop_err(msg): |
22 sys.stderr.write(msg) | 51 sys.exit(msg) |
23 sys.exit(1) | |
24 | 52 |
25 | 53 |
26 def write_html_output(output, title, dir): | 54 def write_html_output(output, title, dir): |
27 with open(output, 'w') as fh: | 55 with open(output, 'w') as fh: |
28 fh.write('<html><head><h3>%s</h3></head>\n' % title) | 56 fh.write('<html><head><h3>%s</h3></head>\n' % title) |