У меня есть веб-приложение, которое читает и отображает файлы журнала, созданные тестовыми файлами Python.В настоящее время я могу запустить файлы Python из Cli, и он отлично работает.
Однако я хотел бы иметь возможность запускать тесты Python из приложения.
В данный момент у меня есть кнопка «Запуск теста», которая вызывает функцию в файле views.py
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')
Текстовый файл содержит имена файлов .py, которые должны быть выполнены
В 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)
Код прекрасно работает, кроме одной вещи.Когда я запускаю подпроцесс, он, по-видимому, выполняется в Windows по умолчанию.Поскольку в тестах используется модуль pexpect, мне нужно, чтобы он выполнялся в Linux.
Я продолжаю думать, что должно быть простое решение, но пока мне не повезло.
Любая помощьбыл бы очень признателен,
Спасибо