【发布时间】:2016-04-11 12:47:22
【问题描述】:
如何在使用 python 模块 paver 定义的任务中执行批处理文件?我是否必须区分摊铺机任务将在什么操作系统(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( ) [paver source code] 使用以下选项之一
# 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 中添加 paver 任务,执行批处理文件,do execute the batch file with side effects 或不执行批处理文件依赖在特定的未注释 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
【问题讨论】:
-
我的问题与 virtualenv 上下文有关:我无法执行批处理文件 deactivate.bat,因为它确实停用了安装 paver 模块和执行 paver 命令的 virtualenv。我能够执行另一个与 virtualenv 无关的批处理文件。我将根据标题编辑问题。
-
Paver 有一个 virtualenv 支持,它允许使用装饰器 @virtualenv(dir="venv") 来定义一个虚拟环境,在其中执行任务。这需要在python virtualenv中安装模块paver,用于调用项目的paver命令。
标签: python batch-file paver