# HG changeset patch # User greg # Date 1493991150 14400 # Node ID 9fdc9793f9d3fb4646b918f1c3a35ffe53982e36 # Parent 07bf26b7a1ab5315d728ab5f0e088dcb51a01ee5 Uploaded diff -r 07bf26b7a1ab -r 9fdc9793f9d3 utils.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils.py Fri May 05 09:32:30 2017 -0400 @@ -0,0 +1,70 @@ +import os +import shutil +import subprocess +import sys + +FSTDERR = 'stderr.txt' +FSTDOUT = 'stdout.txt' + + +def check_execution_errors(rc, fstderr, fstdout): + if rc != 0: + fh = open(fstdout, 'rb') + out_msg = fh.read() + fh.close() + fh = open(fstderr, 'rb') + err_msg = fh.read() + fh.close() + msg = '%s\n%s\n' % (str(out_msg), str(err_msg)) + stop_err(msg) + + +def get_response_buffers(): + fstderr = os.path.join(os.getcwd(), FSTDERR) + fherr = open(fstderr, 'wb') + fstdout = os.path.join(os.getcwd(), FSTDOUT) + fhout = open(fstdout, 'wb') + return fherr, fhout + + +def move_directory_files(source_dir, destination_dir): + source_directory = os.path.abspath(source_dir) + destination_directory = os.path.abspath(destination_dir) + if not os.path.isdir(destination_directory): + os.makedirs(destination_directory) + for dir_entry in os.listdir(source_directory): + source_entry = os.path.join(source_directory, dir_entry) + shutil.move(source_entry, destination_directory) + + +def run_command(cmd): + fherr, fhout = get_response_buffers() + proc = subprocess.Popen(args=cmd, stderr=fherr, stdout=fhout, shell=True) + rc = proc.wait() + # Check results. + fherr.close() + fhout.close() + check_execution_errors(rc, fstderr, fstdout) + + +def stop_err(msg): + sys.exit(msg) + + +def write_html_output(output, title, dir): + with open(output, 'w') as fh: + fh.write('

%s

\n' % title) + fh.write('

\n') + fh.write('\n') + for index, fname in enumerate(sorted(os.listdir(dir))): + if index % 2 == 0: + bgcolor = '#D8D8D8' + else: + bgcolor = '#FFFFFF' + try: + size = str(os.path.getsize(os.path.join(dir, fname))) + except: + size = 'unknown' + link = '%s\n' % (fname, fname) + fh.write('\n' % (bgcolor, size, link)) + fh.write('
SizeName
%s%s
\n')