【发布时间】:2010-09-20 09:36:31
【问题描述】:
我有一个网络应用程序,它读取并显示由 python 测试文件生成的日志文件。目前我可以从 cli 启动 python 文件,它工作正常。
但是我希望能够从应用程序中启动 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 上执行它。
我一直认为必须有一个简单的解决方案,但到目前为止我没有运气。
任何帮助将不胜感激,
谢谢
【问题讨论】:
-
我不太明白这个问题。是不是在linux中执行不了的问题?什么错误?
-
是的,这基本上就是问题所在。如果我在 linux cli 甚至 cygwin 中启动文件,它工作正常。但是当我从应用程序中尝试它时,我收到错误:“找不到关键模块。可能这个操作系统不支持它。Pexpect 适用于类 UNIX 操作系统。”
-
"当我启动子进程时,它似乎默认在 Windows 上执行。" - 你能详细说明一下这句话背后的原因吗?
标签: python linux django django-views