用于相互传递子流程的轻量级工具
vorlauf的Python项目详细描述
沃罗夫
什么?
vorlauf是一个非常简单的工具,它可以帮助您创建流程管道(在 壳感)。它还有助于将流程的定义与 使用给定的stdin、stdout和stderr运行该进程。
为什么?
因为用于将流程链接在一起的子流程api很麻烦,而且 没有很好的记录。
怎么做?
这个库基本上什么也不做-它只实现了不到100行。 有两个可用的类,Process和Pipeline。
过程
Process类传递args、cwd和env,并通过调用 Process.run带有可选的stdin、stdout和stderr参数。
通过从 Process类,我们可以创建可重用的Process定义:
critical_grepper = Process('grep', 'CRITICAL') syslog = open('/var/log/syslog', 'r') apachelog = open('/var/log/httpd/error.log', 'r') filtered = open('critical.log', 'w') for logfile in (syslog, apachelog): critical_grepper.run(stdin=logfile, stdout=filtered)
管道
Pipeline类存储一个Process类的列表,当运行时 使用Pipeline.run和可选的stdin和stdout,管道 一起处理。如果存在,stdin将传递给第一个进程,并且 如果存在,stdout将传递给最后一个进程。
示例
from vorlauf import Pipeline, Process pipeline = Pipeline() pipeline.add(Process('cat', 'foo.txt')) pipeline.add(Process('grep', 'something')) pipeline.add(Process('uniq')) with open('new.txt', 'wb') as fd: pipeline.run(stdout=fd)
并且由于Process和^{tt2}中内置的运算符重载$ 类,这可以简化为:
from vorlauf import Process as P pipeline = P('cat', 'foo.txt') | P('grep', 'something') | P('uniq') with open('new.txt', 'wb') as fd: pipeline.run(stdout=fd)
最后,可以使用Process类创建可重用组件:
from vorlauf import Process class GPG(Process): def __init__(self, passphrase): super(GPG, self).__init__('gpg', '-c', '--passphrase', passphrase, '-') class MySQLDump(Process): def __init__(self, password, dbname, **kwargs): super(MySQLDump, self).__init__( 'mysqldump', '-u', 'root', '-p{}'.format(password), dbname ) with open('mysql.dump', 'wb') as fd: pipeline = MySQLDump('loldongs', 'foo') | GPG('supersekrit') pipeline.run(stdout=fd)
测试
运行:
virtualenv venv venv/bin/pip install -e . venv/bin/python test.py
更改日志
1.0.0(2015-09-22)
- 初始版本