pythonbatch-filepaver

Execute .bat file from within paver task


How can i execute a batch file in a task defined with the python module paver? Do i have to distinguish on what operating system (unix/windows) the paver task will be executed?

E.g. 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

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

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

# 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/:

@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:

@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

Solution

  • Seems like the current release of paver has a bug related to the virtualenv support on windows systems see this issue.