簡體   English   中英

從攤鋪機任務中執行.bat文件

[英]Execute .bat file from within paver task

我如何在用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() [ 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)

編輯:

我在根目錄和子目錄venv / Scripts /中創建了一個與“ virtualenv處理”不相關的批處理文件hello_world.bat

@echo off
echo hello world
pause

呼喚

paver root_dir_call

要么

paver sub_dir_call

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!?)

@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