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