0
|
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 fstderr, fherr, fstdout, fhout
|
|
28
|
|
29
|
|
30 def move_directory_files(source_dir, destination_dir, copy=False):
|
|
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 if copy:
|
|
38 shutil.copy(source_entry, destination_directory)
|
|
39 else:
|
|
40 shutil.move(source_entry, destination_directory)
|
|
41
|
|
42
|
|
43 def run_command(cmd):
|
|
44 fstderr, fherr, fstdout, fhout = get_response_buffers()
|
|
45 proc = subprocess.Popen(args=cmd, stderr=fherr, stdout=fhout, shell=True)
|
|
46 rc = proc.wait()
|
|
47 # Check results.
|
|
48 fherr.close()
|
|
49 fhout.close()
|
|
50 check_execution_errors(rc, fstderr, fstdout)
|
|
51
|
|
52
|
|
53 def stop_err(msg):
|
|
54 sys.exit(msg)
|
|
55
|
|
56
|
|
57 def write_html_output(output, title, dir):
|
|
58 with open(output, 'w') as fh:
|
|
59 dir_items = sorted(os.listdir(dir))
|
|
60 # Directories can only contain either files or directories,
|
|
61 # but not both.
|
|
62 if len(dir_items) > 0:
|
|
63 item_path = os.path.join(dir, dir_items[0])
|
|
64 if os.path.isdir(item_path):
|
|
65 header = 'Directories'
|
|
66 else:
|
|
67 header = 'Datasets'
|
|
68 else:
|
|
69 header = ''
|
|
70 fh.write('<html><head><h3>%s: %d items</h3></head>\n' % (title, len(dir_items)))
|
|
71 fh.write('<body><p/><table cellpadding="2">\n')
|
|
72 fh.write('<tr><b>%s</th></b>\n' % header)
|
|
73 for index, fname in enumerate(dir_items):
|
|
74 if index % 2 == 0:
|
|
75 bgcolor = '#D8D8D8'
|
|
76 else:
|
|
77 bgcolor = '#FFFFFF'
|
|
78 link = '<a href="%s" type="text/plain">%s</a>\n' % (fname, fname)
|
|
79 fh.write('<tr bgcolor="%s"><td>%s</td></tr>\n' % (bgcolor, link))
|
|
80 fh.write('</table></body></html>\n')
|