annotate utils.py @ 4:d3f8c8b77dbe draft

Uploaded
author greg
date Thu, 24 Aug 2017 13:18:06 -0400
parents 7ba9469800b9
children 3384b6a842b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7ba9469800b9 Uploaded
greg
parents:
diff changeset
1 import os
7ba9469800b9 Uploaded
greg
parents:
diff changeset
2 import shutil
7ba9469800b9 Uploaded
greg
parents:
diff changeset
3 import subprocess
7ba9469800b9 Uploaded
greg
parents:
diff changeset
4 import sys
7ba9469800b9 Uploaded
greg
parents:
diff changeset
5
7ba9469800b9 Uploaded
greg
parents:
diff changeset
6 FSTDERR = 'stderr.txt'
7ba9469800b9 Uploaded
greg
parents:
diff changeset
7 FSTDOUT = 'stdout.txt'
7ba9469800b9 Uploaded
greg
parents:
diff changeset
8
7ba9469800b9 Uploaded
greg
parents:
diff changeset
9
7ba9469800b9 Uploaded
greg
parents:
diff changeset
10 def check_execution_errors(rc, fstderr, fstdout):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
11 if rc != 0:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
12 fh = open(fstdout, 'rb')
7ba9469800b9 Uploaded
greg
parents:
diff changeset
13 out_msg = fh.read()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
14 fh.close()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
15 fh = open(fstderr, 'rb')
7ba9469800b9 Uploaded
greg
parents:
diff changeset
16 err_msg = fh.read()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
17 fh.close()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
18 msg = '%s\n%s\n' % (str(out_msg), str(err_msg))
7ba9469800b9 Uploaded
greg
parents:
diff changeset
19 stop_err(msg)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
20
7ba9469800b9 Uploaded
greg
parents:
diff changeset
21
7ba9469800b9 Uploaded
greg
parents:
diff changeset
22 def get_response_buffers():
7ba9469800b9 Uploaded
greg
parents:
diff changeset
23 fstderr = os.path.join(os.getcwd(), FSTDERR)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
24 fherr = open(fstderr, 'wb')
7ba9469800b9 Uploaded
greg
parents:
diff changeset
25 fstdout = os.path.join(os.getcwd(), FSTDOUT)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
26 fhout = open(fstdout, 'wb')
7ba9469800b9 Uploaded
greg
parents:
diff changeset
27 return fstderr, fherr, fstdout, fhout
7ba9469800b9 Uploaded
greg
parents:
diff changeset
28
7ba9469800b9 Uploaded
greg
parents:
diff changeset
29
7ba9469800b9 Uploaded
greg
parents:
diff changeset
30 def move_directory_files(source_dir, destination_dir, copy=False):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
31 source_directory = os.path.abspath(source_dir)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
32 destination_directory = os.path.abspath(destination_dir)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
33 if not os.path.isdir(destination_directory):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
34 os.makedirs(destination_directory)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
35 for dir_entry in os.listdir(source_directory):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
36 source_entry = os.path.join(source_directory, dir_entry)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
37 if copy:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
38 shutil.copy(source_entry, destination_directory)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
39 else:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
40 shutil.move(source_entry, destination_directory)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
41
7ba9469800b9 Uploaded
greg
parents:
diff changeset
42
7ba9469800b9 Uploaded
greg
parents:
diff changeset
43 def run_command(cmd):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
44 fstderr, fherr, fstdout, fhout = get_response_buffers()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
45 proc = subprocess.Popen(args=cmd, stderr=fherr, stdout=fhout, shell=True)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
46 rc = proc.wait()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
47 # Check results.
7ba9469800b9 Uploaded
greg
parents:
diff changeset
48 fherr.close()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
49 fhout.close()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
50 check_execution_errors(rc, fstderr, fstdout)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
51
7ba9469800b9 Uploaded
greg
parents:
diff changeset
52
7ba9469800b9 Uploaded
greg
parents:
diff changeset
53 def stop_err(msg):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
54 sys.exit(msg)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
55
7ba9469800b9 Uploaded
greg
parents:
diff changeset
56
7ba9469800b9 Uploaded
greg
parents:
diff changeset
57 def write_html_output(output, title, dir):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
58 with open(output, 'w') as fh:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
59 dir_items = sorted(os.listdir(dir))
7ba9469800b9 Uploaded
greg
parents:
diff changeset
60 # Directories can only contain either files or directories,
7ba9469800b9 Uploaded
greg
parents:
diff changeset
61 # but not both.
7ba9469800b9 Uploaded
greg
parents:
diff changeset
62 if len(dir_items) > 0:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
63 item_path = os.path.join(dir, dir_items[0])
7ba9469800b9 Uploaded
greg
parents:
diff changeset
64 if os.path.isdir(item_path):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
65 header = 'Directories'
7ba9469800b9 Uploaded
greg
parents:
diff changeset
66 else:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
67 header = 'Datasets'
7ba9469800b9 Uploaded
greg
parents:
diff changeset
68 else:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
69 header = ''
7ba9469800b9 Uploaded
greg
parents:
diff changeset
70 fh.write('<html><head><h3>%s: %d items</h3></head>\n' % (title, len(dir_items)))
7ba9469800b9 Uploaded
greg
parents:
diff changeset
71 fh.write('<body><p/><table cellpadding="2">\n')
7ba9469800b9 Uploaded
greg
parents:
diff changeset
72 fh.write('<tr><b>%s</th></b>\n' % header)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
73 for index, fname in enumerate(dir_items):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
74 if index % 2 == 0:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
75 bgcolor = '#D8D8D8'
7ba9469800b9 Uploaded
greg
parents:
diff changeset
76 else:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
77 bgcolor = '#FFFFFF'
7ba9469800b9 Uploaded
greg
parents:
diff changeset
78 link = '<a href="%s" type="text/plain">%s</a>\n' % (fname, fname)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
79 fh.write('<tr bgcolor="%s"><td>%s</td></tr>\n' % (bgcolor, link))
7ba9469800b9 Uploaded
greg
parents:
diff changeset
80 fh.write('</table></body></html>\n')