[PYTHON] Scripting with Paver-External Commands

The least scripting language part of Python is that you have to do ʻimport subprocess; subprocess.call ("ls -l ") `to execute an external command, which is annoying.

Paver defines a sh () function that makes it easier to execute external commands. This can also be imported with from paver.easy import *.

How to use

If you write sh ("ls -l /"), the command will be executed via the shell.

capture

By default, the output of the executed command is displayed as it is in the standard output, but if you specify capture = True,

pavement.py


from paver.easy import *
import os

@task
def list():
    ret = sh("ls -l /", capture=True)
    print(len(ret.splitlines()))
$ paver list
---> pavement.list
ls -l /
22

dryrun Like the methods of the path class, the sh () function only displays the command and does not actually execute it when paver is run in dryrun mode.

pavement.py


from paver.easy import *

@task
def barusu():
    sh("rm -rf /")
$ paver -n barusu
---> pavement.barusu
rm -rf /

Recommended Posts

Scripting with Paver-External Commands
Scripting with Paver-Command Definition
Periodically execute commands with crontab
Let's execute commands regularly with cron!
Commands for creating SNS with Django
Treat external commands like functions with sh