说我fabfile.py
看起来像这样:
def setup(): pwd = getpass('mysql password: ') run('mysql -umoo -p%s something' % pwd)
这个输出是:
[host] run: mysql -umoo -pTheActualPassword
有没有办法让输出看起来像这样?
[host] run: mysql -umoo -p*******
注意:这不是一个mysql问题!
您可以使用过滤器替换stdout(或任何iostream),而不是修改/重写Fabric.
这是一个覆盖stdout来审查特定密码的示例.它从Fabric的env.password
变量获取密码,由-I
参数设置.请注意,您可以使用正则表达式执行相同的操作,这样您就不必在过滤器中指定密码.
I should also mention, this isn't the most efficient code in the world, but if you're using fabric you're likely gluing a couple things together and care more about manageability than speed.
#!/usr/bin/python import sys import string from fabric.api import * from fabric.tasks import * from fabric.contrib import * class StreamFilter(object): def __init__(self, filter, stream): self.stream = stream self.filter = filter def write(self,data): data = data.replace(self.filter, '[[TOP SECRET]]') self.stream.write(data) self.stream.flush() def flush(self): self.stream.flush() @task def can_you_see_the_password(): sys.stdout = StreamFilter(env.password, sys.stdout) print 'Hello there' print 'My password is %s' % env.password
When run:
fab -I can_you_see_the_password Initial value for env.password:
this will produce:
Hello there My password is [[TOP SECRET]]