python shell脚本变得简单

scriptine的Python项目详细描述


Author:Oliver Tonnhofer <olt@bogosoft.com>

简介

scriptine的主要目标是简化shell脚本的编写 和Python一起。

Scriptine做了两件事来解决这个目标:

  • 使创建脚本和命令变得容易。
  • 使使用文件、目录和其他shell命令更容易。

要使用scriptine创建命令,只需为脚本的每个命令创建一个普通的python函数,scriptine处理其余命令。它创建命令行选项解析器并用正确的选项调用正确的函数。

Scriptine的第二部分是一系列方便的类和函数,这些类和函数使得使用文件、目录和其他shell命令更加容易。 它抽象了不同的python模块,如osos.pathshutil子流程,并提供了一个简单易用的界面。脚本附带了jason orendorff的路径模块的增强版本。

功能

  • 轻松创建命令。每个命令只是一个函数。
    • 自动选项分析
    • 自动帮助文本(--help
  • 日志处理(使用-v--verbose-q--quiet处理)
  • 在干燥模式下测试脚本。所有破坏性函数/方法都被包装并将 设置-n--dry-run选项时不调用。
  • 轻松执行其他shell脚本。
  • 方便地处理文件和目录。

示例

下面是一个小示例脚本:

import scriptine

def example_command(name, countdown=False, repeat=10):
    """Show how scriptine works."""
    if countdown:
        for i in range(repeat, 0, -1):
            print i,
    print 'Hello, %s!' % name

if __name__ == '__main__':
    scriptine.run()

使用我们的小脚本:

% python test.py
Usage: test.py command [options]

Options:
  -h, --help  show this help message and exit

Commands:
  example  Show how scriptine works.
% python test.py example Pete
Hello, Pete!
% python test.py example Pete --countdown --repeat 5
5 4 3 2 1 Hello, Pete!

一个更复杂的例子:

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()

帮助文本:

% 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

结果是:

% python convert.py to-png ~/images/ --extension gif
---> converting /Users/olt/images/*.gif to out/*.png
% python convert.py to-png ~/images/ --extension gif -v
---> converting /Users/olt/images/*.gif to out/*.png
INFO: call ['convert', '/Users/olt/images/foo.gif', 'out/foo.png']
INFO: call ['convert', '/Users/olt/images/bar.gif', 'out/foo.png']

开发

请关注https://github.com/olt/scriptine上的开发,我们欢迎您提供评论和错误修复。

更改日志

0.2.1(2015-06-08)
  • 修复–静音选项
0.2.0(2013-04-10)
  • 改进二进制数据在写入字节中的处理
  • Windows上的fixed path.isdir
  • 添加路径。确保目录
0.2.0a3(2009-11-16)
  • 仅使用一个命令的脚本的新command.cmd函数
  • 添加command.autocmds作为run的替代
0.2.0a2(2009-11-11)
  • 修复了manifest.in中丢失的文件
0.2.0a1(2009-11-09)
  • 已将path.getcwd重命名为path.cwd
  • 将属性转换为可能 更改(例如大小、时间等)
  • 添加path.newer方法
0.1.0(2009-11-05)
  • 首次发布

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java应用程序不是在Eclipse中运行,而是在命令行中运行   swing Java图形组件问题。似乎找不到错误   我需要键盘。close();让代码正常工作?   Springboot中的java HttpSession   抽象语法树我想添加一个语句。我试图解析它,java解析器异常被抛出。如何克服它?   java Hibernate:清理会话   具有不连续子集的java划分问题   java正则表达式查找最后一个冒号后的字符串   java从SpringShell执行OS命令   Java扫描器字符串输入   java字符串索引越界异常(charAt)   java执行器服务终止被卡住   Springockito没有继承java@ContextConfiguration   java如何为一个servlet映射多个url   java安卓获取命令的stderr   java生成类型。表:数据库中的大数字   安卓 Getter Setter返回NothingJava