| 
0
 | 
     1 import os
 | 
| 
 | 
     2 import shutil
 | 
| 
15
 | 
     3 import subprocess
 | 
| 
0
 | 
     4 import sys
 | 
| 
 | 
     5 
 | 
| 
15
 | 
     6 FSTDERR = 'stderr.txt'
 | 
| 
 | 
     7 FSTDOUT = 'stdout.txt'
 | 
| 
0
 | 
     8 
 | 
| 
15
 | 
     9 
 | 
| 
 | 
    10 def check_execution_errors(rc, fstderr, fstdout):
 | 
| 
0
 | 
    11     if rc != 0:
 | 
| 
15
 | 
    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')
 | 
| 
17
 | 
    27     return fstderr, fherr, fstdout, fhout
 | 
| 
0
 | 
    28 
 | 
| 
 | 
    29 
 | 
| 
28
 | 
    30 def move_directory_files(source_dir, destination_dir, copy=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)
 | 
| 
28
 | 
    37         if copy:
 | 
| 
 | 
    38             shutil.copy(source_entry, destination_directory)
 | 
| 
 | 
    39         else:
 | 
| 
 | 
    40             shutil.move(source_entry, destination_directory)
 | 
| 
0
 | 
    41 
 | 
| 
 | 
    42 
 | 
| 
15
 | 
    43 def run_command(cmd):
 | 
| 
17
 | 
    44     fstderr, fherr, fstdout, fhout = get_response_buffers()
 | 
| 
15
 | 
    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 
 | 
| 
0
 | 
    53 def stop_err(msg):
 | 
| 
15
 | 
    54     sys.exit(msg)
 | 
| 
0
 | 
    55 
 | 
| 
 | 
    56 
 | 
| 
 | 
    57 def write_html_output(output, title, dir):
 | 
| 
 | 
    58     with open(output, 'w') as fh:
 | 
| 
33
 | 
    59         dir_items = sorted(os.listdir(dir))
 | 
| 
35
 | 
    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'
 | 
| 
33
 | 
    68         else:
 | 
| 
35
 | 
    69             header = ''
 | 
| 
33
 | 
    70         fh.write('<html><head><h3>%s: %d items</h3></head>\n' % (title, len(dir_items)))
 | 
| 
0
 | 
    71         fh.write('<body><p/><table cellpadding="2">\n')
 | 
| 
35
 | 
    72         fh.write('<tr><b>%s</th></b>\n' % header)
 | 
| 
33
 | 
    73         for index, fname in enumerate(dir_items):
 | 
| 
0
 | 
    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)
 | 
| 
33
 | 
    79             fh.write('<tr bgcolor="%s"><td>%s</td></tr>\n' % (bgcolor, link))
 | 
| 
0
 | 
    80         fh.write('</table></body></html>\n')
 |