如何在使用 python 模块摊铺机定义的任务中执行批处理文件?我是否必须区分摊铺机任务将在什么操作系统(unix/windows)上执行?
例如,在项目根目录的pavement.py中定义的以下任务确实执行项目中定义的所有单元测试(使用 python 标准库模块unittest定义)
from paver.tasks import task
from paver.easy import sh
@task
def unit_tests():
"""
Run all unit tests.
"""
sh('python -m unittest')
如果确实执行
paver unit_tests
从项目根目录中的命令行。
但是我无法在 Windows 操作系统(位于项目根目录中)上执行批处理文件文件
sh('batchfile.bat')
我也无法使用sh() [摊铺机源代码] 的cwd参数和以下替代方法之一在项目根目录的子目录venv/Scripts中执行批处理文件
# no call does execute the batch file (*cwd* alternatives)
sh('batchfile.bat', cwd='venv/Scripts')
sh('cmd /c batchfile.bat', cwd='venv/Scripts')
# no call does execute the batch file (*sh()* "sequential command" syntax alternatives)
sh('cd venv/Scripts; deactivate.bat')
sh('cd venv/Scripts; cmd /c deactivate.bat')
# this sequence does also not execute the batch file (absolute path alternative)
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'venv\Scripts')
sh('deactivate.bat', cwd=path)
编辑:
我在根目录和子目录venv/Scripts/中创建了一个与“virtualenv processing”无关的批处理文件 hello_world.bat :
@echo off
echo hello world
pause
打电话
paver root_dir_call
或者
paver sub_dir_call
在windows上的项目根目录中,在pavement.py中添加了摊铺机任务,执行批处理文件,执行有副作用的批处理文件或不执行批处理文件,具体取决于未注释的特定sh()命令:
@task
def root_dir_call():
# use one of these two calls!
sh('hello_world.bat') # PASS
#sh('hello_world') # PASS
# do not use other calls like these two because they have side effects or do not execute the batch file at all
#sh('call hello_world.bat') # PASS (execution with side effects)
#sh('cmd /c hello_world.bat') # PASS (execution with side effects)
#sh('start hello_world.bat') # PASS (execution with side effects)
#sh('cmd hello_world.bat') # FAIL (no execution, output of cmd!?)
和
@task
def sub_dir_call():
# use one of these calls!
sh('hello_world.bat', cwd='venv/Scripts/') # PASS
#sh('hello_world', cwd='venv/Scripts') # PASS
#sh('hello_world', cwd='venv\Scripts') # PASS
# following calls do not execute the batch file
#sh('cd venv/Scripts; hello_world.bat') # FAIL
#sh('cd venv/Scripts/; hello_world.bat') # FAIL
#sh('cd venv\Scripts\; hello_world.bat') # FAIL
#sh('cd venv/Scripts; cmd /c hello_world.bat') # FAIL