从摊铺机任务中执行.bat文件

时间:2016-01-07 07:51:15

标签: python batch-file paver

如何在使用python模块 paver 定义的任务中执行批处理文件?我是否必须区分操作系统(unix / windows)将执行的摊铺机任务?

E.g。项目根目录中 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 中执行批处理文件( ) [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)

编辑:

我创建了一个与" virtualenv处理"无关的批处理文件 hello_world.bat 。在根目录和子目录venv / Scripts /:

@echo off
echo hello world
pause

调用

paver root_dir_call

paver sub_dir_call
windows 上的项目根目录中

,并在 pavement.py 中添加了摊铺机任务,执行批处理文件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

1 个答案:

答案 0 :(得分:0)