0

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
Community
  • 1
  • 1
thinwybk
  • 4,193
  • 2
  • 40
  • 76
  • My problem is related to the virtualenv context: I am not able to execute the batchfile deactivate.bat as it does deactivate the virtualenv in which the paver module is installed and in which the paver command is executed. I am able to execute another batch file which is not related to the virtualenv. I will edit the question accoring to its heading. – thinwybk Jan 08 '16 at 16:26
  • It is possible to [call .bat files from linux using wine](http://www.linux.org/threads/running-windows-batch-files-on-linux.7610/) – thinwybk Jan 08 '16 at 16:52
  • Paver has a virtualenv support which allows to use the decorator @virtualenv(dir="venv") to define a virtual environment in which the task shall be executed. This requires that the module **paver** is installed in the python virtualenv which is used to call the paver commands of the project. – thinwybk Jan 08 '16 at 16:59

1 Answers1

0

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

thinwybk
  • 4,193
  • 2
  • 40
  • 76