Navigation

Meta

Maintainers

Classifiers

Introduction

The primary goal of scriptine is to make it easy to write shell scripts
with python.

Scriptine does two things to solve this goal:

Make it easy to create scripts and commands.

Make it easy to work with files, directories and other shell commands.

To create commands with scriptine, you just create a normal python function for each command of your script and scriptine handles the rest. It creates command line option parser and calls the right function with the right options.

The second part of scriptine is a bunch of convenience classes and functions that make it easy to work with files, directories and other shell commands.
It abstracts the different python modules like os, os.path, shutil and subprocess and offers a simple and easy to use interface. scriptine comes with an enhanced version of Jason Orendorff’s path module.

Features

Easy command creation. Each command is just a function.

Automatic option parsing

Automatic help text (--help)

Log handling (with -v, --verbose and -q, --quiet handling)

Testing scripts in dry-mode. All destructive functions/methods are wrapped and will
not be called when the -n or --dry-run option is set.

from scriptine import run, path, log
from scriptine.shell import call
def to_png_command(dirname, outdir='out', extension='jpeg'):
"""
Convert all files with extension in dirname to .png.
Only convert if result does not exists or is older.
:param dirname: where to search for images
:param outdir: where to store the results
:param extension: file extension to convert
"""
outdir = path(outdir)
if not outdir.exists(): outdir.makedirs()
log.mark('converting %s/*.%s to %s/*.png', dirname, extension, outdir)
for f in path(dirname).files('*.'+extension):
outfile = outdir / f.namebase + '.png'
if not outfile.exists() or f.newer(outfile):
call(['convert', f, outfile])
if __name__ == '__main__':
run()

The help text:

% python convert.py to-png --help
Usage: test.py to-png [options] dirname
Convert all files with extension in dirname to .png. Only
convert if result does not exists or is older.
Required arguments:
dirname: where to search for images
Options:
-h, --help show this help message and exit
--outdir=out where to store the results
--extension=jpeg file extension to convert
-n, --dry-run don't actually do anything
-v, --verbose be more verbose
-q, --quiet be more silent