Mercurial > repos > greg > gene_family_classifier
comparison utils.py @ 142:9fdc9793f9d3 draft
Uploaded
author | greg |
---|---|
date | Fri, 05 May 2017 09:32:30 -0400 |
parents | |
children | 5b1873b55da5 |
comparison
equal
deleted
inserted
replaced
141:07bf26b7a1ab | 142:9fdc9793f9d3 |
---|---|
1 import os | |
2 import shutil | |
3 import subprocess | |
4 import sys | |
5 | |
6 FSTDERR = 'stderr.txt' | |
7 FSTDOUT = 'stdout.txt' | |
8 | |
9 | |
10 def check_execution_errors(rc, fstderr, fstdout): | |
11 if rc != 0: | |
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 | |
28 | |
29 | |
30 def move_directory_files(source_dir, destination_dir): | |
31 source_directory = os.path.abspath(source_dir) | |
32 destination_directory = os.path.abspath(destination_dir) | |
33 if not os.path.isdir(destination_directory): | |
34 os.makedirs(destination_directory) | |
35 for dir_entry in os.listdir(source_directory): | |
36 source_entry = os.path.join(source_directory, dir_entry) | |
37 shutil.move(source_entry, destination_directory) | |
38 | |
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 | |
50 def stop_err(msg): | |
51 sys.exit(msg) | |
52 | |
53 | |
54 def write_html_output(output, title, dir): | |
55 with open(output, 'w') as fh: | |
56 fh.write('<html><head><h3>%s</h3></head>\n' % title) | |
57 fh.write('<body><p/><table cellpadding="2">\n') | |
58 fh.write('<tr><th>Size</th><th>Name</th></tr>\n') | |
59 for index, fname in enumerate(sorted(os.listdir(dir))): | |
60 if index % 2 == 0: | |
61 bgcolor = '#D8D8D8' | |
62 else: | |
63 bgcolor = '#FFFFFF' | |
64 try: | |
65 size = str(os.path.getsize(os.path.join(dir, fname))) | |
66 except: | |
67 size = 'unknown' | |
68 link = '<a href="%s" type="text/plain">%s</a>\n' % (fname, fname) | |
69 fh.write('<tr bgcolor="%s"><td>%s</td><td>%s</td></tr>\n' % (bgcolor, size, link)) | |
70 fh.write('</table></body></html>\n') |