views:

55

answers:

0

Hi, I have a web app which reads and displays log files generated by python test files. Currently I can launch the python files from cli and it works fine.

However I would like to be able to launch python tests from within the application.

At the minute I have a "Launch test" button which calls a function in the views.py file

def launch_tests(request, test_txt_file):
    test_list_file = test_txt_file
    launcher2.main(test_list_file)

    return render_to_response('front_page.html')

The text file contains the names of the .py files to be executed

In launcher2.main

import os,time, string, pdb, sys
import getopt,subprocess
import pdb

bi_python_home = '//belfiler1/scratch-disk/conor.doherty/dev/python/bi_python'


def main(test_list_file):

    # if argument passed in for test list file  
    if test_list_file != None:
        test_list = bi_python_home + "/launcher/" + test_list_file
    else:
        # default
        test_list =  bi_python_home + "/launcher/test_list.txt"



    tests_dir =  bi_python_home + "/tests/"
    log_dir =bi_python_home + "/logs/"
    month = '%Y%m'
    day = '%d'
    try :
        for test_to_run in open(test_list):
            sub_dir=os.path.dirname(test_to_run) 
            test_filename = os.path.basename(test_to_run)

            # Create log directory for date if doesn't exist
            cur_log_dir = log_dir + time.strftime(month, time.localtime()) + '/' + time.strftime(month+day, time.localtime()) + '/' + sub_dir 
            if not os.path.exists(cur_log_dir):
                print "creating cur_log_dir "  + cur_log_dir
                os.makedirs(cur_log_dir)


            full_path = string.rstrip(tests_dir + test_to_run)
            #print ' full_path is "' + full_path + '"'
            cmd = 'python ' + full_path


            if os.path.isfile(full_path) == True :
                print'\n'*2
                print "Processing file " + test_to_run,

                log_timestamp = time.strftime("%Y%m%d_%H%M%S", time.localtime())
                log_filename = cur_log_dir + '/' + string.rstrip(test_filename) + "_" + log_timestamp + '.log' 
                print 'log file to use is' + log_filename
                # Redirect stdout and stderr to logfile
                cmd = string.rstrip(cmd) + ' > ' + log_filename
                popen_obj = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                (stdout, stderr) = popen_obj.communicate()
                print 'executing: ' + cmd
                print 'stderr is: ', stderr





            # if need to debug , remove stream redirection &> above and print stdout, stderr

        #else :
            #print 'ignoring ' , full_path



except IOError, err:
    print str(err) 
    sys.exit(2)

The Code works fine apart from one thing. When I kick off the subprocess it seems to be executed on windows by default. Since the tests use a module called pexpect i need it to be executed on linux.

I keep thinking there has to be a simple solution but so far I have had no luck.

Any help would be greatly appreciated,

Thanks