简体   繁体   English

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

[英]Execute .bat file from within paver task

How can i execute a batch file in a task defined with the python module paver ? 我如何在用python模块摊铺机定义的任务中执行批处理文件? Do i have to distinguish on what operating system (unix/windows) the paver task will be executed? 我是否必须区分摊铺机任务将在哪种操作系统(unix / windows)上执行?

Eg the following task defined in pavement.py in the projects root directory does execute all unit tests (defined with python standard library module unittest ) defined in the project 例如,在项目根目录的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')

if one does execute 如果有人执行

paver unit_tests

from the command line in the projects root directory. 从项目根目录中的命令行。

However i am not able to execute a batchfile file on a windows operating system (located in the project root directory) with 但是我无法在Windows操作系统(位于项目根目录中)上执行批处理文件

sh('batchfile.bat')

I am also not able to execute a batch file in a sub-directory venv/Scripts of the projects root directory using the cwd argument of sh() [ paver source code ] with one of the following alternatives 我也无法使用sh() [ paver源代码 ]的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)

EDIT: 编辑:

I created a batch file hello_world.bat not related with "virtualenv processing" in the root directory and the sub directory venv/Scripts/: 我在根目录和子目录venv / Scripts /中创建了一个与“ virtualenv处理”不相关的批处理文件hello_world.bat

@echo off
echo hello world
pause

Calling 呼唤

paver root_dir_call

or 要么

paver sub_dir_call

in the project root directory on windows with the added paver tasks in pavement.py do execute the batch file, do execute the batch file with side effects or do not execute the batch file dependent on the specific uncommented sh() command: Windows的项目根目录中,在pavement.py中添加了paver任务,则执行批处理文件, 执行带有副作用的批处理文件或不执行批处理文件,具体取决于未注释的特定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!?)

and

@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

好像当前的Paver发行版中存在一个与Windows系统对virtualenv支持有关错误,请参见此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM