Here are the examples of the python api sys.stdout taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
152 Examples
0
Example 151
Project: repo Source File: forall.py
def Execute(self, opt, args):
if not opt.command:
self.Usage()
cmd = [opt.command[0]]
shell = True
if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]):
shell = False
if shell:
cmd.append(cmd[0])
cmd.extend(opt.command[1:])
if opt.project_header \
and not shell \
and cmd[0] == 'git':
# If this is a direct git command that can enable colorized
# output and the user prefers coloring, add --color into the
# command line because we are going to wrap the command into
# a pipe and git won't know coloring should activate.
#
for cn in cmd[1:]:
if not cn.startswith('-'):
break
if cn in _CAN_COLOR:
class ColorCmd(Coloring):
def __init__(self, config, cmd):
Coloring.__init__(self, config, cmd)
if ColorCmd(self.manifest.manifestProject.config, cn).is_on:
cmd.insert(cmd.index(cn) + 1, '--color')
mirror = self.manifest.IsMirror
out = ForallColoring(self.manifest.manifestProject.config)
out.redirect(sys.stdout)
rc = 0
first = True
for project in self.GetProjects(args):
env = os.environ.copy()
def setenv(name, val):
if val is None:
val = ''
env[name] = val.encode()
setenv('REPO_PROJECT', project.name)
setenv('REPO_PATH', project.relpath)
setenv('REPO_REMOTE', project.remote.name)
setenv('REPO_LREV', project.GetRevisionId())
setenv('REPO_RREV', project.revisionExpr)
if mirror:
setenv('GIT_DIR', project.gitdir)
cwd = project.gitdir
else:
cwd = project.worktree
if not os.path.exists(cwd):
if (opt.project_header and opt.verbose) \
or not opt.project_header:
print >>sys.stderr, 'skipping %s/' % project.relpath
continue
if opt.project_header:
stdin = subprocess.PIPE
stdout = subprocess.PIPE
stderr = subprocess.PIPE
else:
stdin = None
stdout = None
stderr = None
p = subprocess.Popen(cmd,
cwd = cwd,
shell = shell,
env = env,
stdin = stdin,
stdout = stdout,
stderr = stderr)
if opt.project_header:
class sfd(object):
def __init__(self, fd, dest):
self.fd = fd
self.dest = dest
def fileno(self):
return self.fd.fileno()
empty = True
didout = False
errbuf = ''
p.stdin.close()
s_in = [sfd(p.stdout, sys.stdout),
sfd(p.stderr, sys.stderr)]
for s in s_in:
flags = fcntl.fcntl(s.fd, fcntl.F_GETFL)
fcntl.fcntl(s.fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
while s_in:
in_ready, out_ready, err_ready = select.select(s_in, [], [])
for s in in_ready:
buf = s.fd.read(4096)
if not buf:
s.fd.close()
s_in.remove(s)
continue
if not opt.verbose:
if s.fd == p.stdout:
didout = True
else:
errbuf += buf
continue
if empty:
if first:
first = False
else:
out.nl()
out.project('project %s/', project.relpath)
out.nl()
out.flush()
if errbuf:
sys.stderr.write(errbuf)
sys.stderr.flush()
errbuf = ''
empty = False
s.dest.write(buf)
s.dest.flush()
r = p.wait()
if r != 0 and r != rc:
rc = r
if rc != 0:
sys.exit(rc)
0
Example 152
def main():
import optparse
import os.path
import re
import sys
DUMP = 1
LOAD = 2
def options_to_kwargs(options):
args = {}
if options.host:
args['host'] = options.host
if options.port:
args['port'] = int(options.port)
if options.socket:
args['unix_socket_path'] = options.socket
if options.password:
args['password'] = options.password
if options.db:
args['db'] = int(options.db)
if options.encoding:
args['encoding'] = options.encoding
# dump only
if hasattr(options, 'pretty') and options.pretty:
args['pretty'] = True
if hasattr(options, 'keys') and options.keys:
args['keys'] = options.keys
# load only
if hasattr(options, 'use_expireat') and options.use_expireat:
args['use_expireat'] = True
if hasattr(options, 'empty') and options.empty:
args['empty'] = True
if hasattr(options, 'backend') and options.backend:
args['streaming_backend'] = options.backend
return args
def do_dump(options):
if options.output:
output = open(options.output, 'w')
else:
output = sys.stdout
kwargs = options_to_kwargs(options)
dump(output, **kwargs)
if options.output:
output.close()
def do_load(options, args):
if len(args) > 0:
input = open(args[0], 'rb')
else:
input = sys.stdin
kwargs = options_to_kwargs(options)
load(input, **kwargs)
if len(args) > 0:
input.close()
script_name = os.path.basename(sys.argv[0])
if re.search(r'load(?:$|\.)', script_name):
action = help = LOAD
elif re.search(r'dump(?:$|\.)', script_name):
action = help = DUMP
else:
# default is dump, however if dump is specifically requested
# we don't show help text for toggling between dumping and loading
action = DUMP
help = None
if help == LOAD:
usage = "Usage: %prog [options] [FILE]"
usage += "\n\nLoad data from FILE (which must be a JSON dump previously created"
usage += "\nby redisdl) into specified or default redis."
usage += "\n\nIf FILE is omitted standard input is read."
elif help == DUMP:
usage = "Usage: %prog [options]"
usage += "\n\nDump data from specified or default redis."
usage += "\n\nIf no output file is specified, dump to standard output."
else:
usage = "Usage: %prog [options]"
usage += "\n %prog -l [options] [FILE]"
usage += "\n\nDump data from redis or load data into redis."
usage += "\n\nIf input or output file is specified, dump to standard output and load"
usage += "\nfrom standard input."
parser = optparse.OptionParser(usage=usage)
parser.add_option('-H', '--host', help='connect to HOST (default localhost)')
parser.add_option('-p', '--port', help='connect to PORT (default 6379)')
parser.add_option('-s', '--socket', help='connect to SOCKET')
parser.add_option('-w', '--password', help='connect with PASSWORD')
if help == DUMP:
parser.add_option('-d', '--db', help='dump DATABASE (0-N, default 0)')
parser.add_option('-k', '--keys', help='dump only keys matching specified glob-style pattern')
parser.add_option('-o', '--output', help='write to OUTPUT instead of stdout')
parser.add_option('-y', '--pretty', help='split output on multiple lines and indent it', action='store_true')
parser.add_option('-E', '--encoding', help='set encoding to use while decoding data from redis', default='utf-8')
elif help == LOAD:
parser.add_option('-d', '--db', help='load into DATABASE (0-N, default 0)')
parser.add_option('-e', '--empty', help='delete all keys in destination db prior to loading')
parser.add_option('-E', '--encoding', help='set encoding to use while encoding data to redis', default='utf-8')
parser.add_option('-B', '--backend', help='use specified streaming backend')
parser.add_option('-A', '--use-expireat', help='use EXPIREAT rather than TTL/EXPIRE', action='store_true')
else:
parser.add_option('-l', '--load', help='load data into redis (default is to dump data from redis)', action='store_true')
parser.add_option('-d', '--db', help='dump or load into DATABASE (0-N, default 0)')
parser.add_option('-k', '--keys', help='dump only keys matching specified glob-style pattern')
parser.add_option('-o', '--output', help='write to OUTPUT instead of stdout (dump mode only)')
parser.add_option('-y', '--pretty', help='split output on multiple lines and indent it (dump mode only)', action='store_true')
parser.add_option('-e', '--empty', help='delete all keys in destination db prior to loading (load mode only)', action='store_true')
parser.add_option('-E', '--encoding', help='set encoding to use while decoding data from redis', default='utf-8')
parser.add_option('-A', '--use-expireat', help='use EXPIREAT rather than TTL/EXPIRE', action='store_true')
parser.add_option('-B', '--backend', help='use specified streaming backend (load mode only)')
options, args = parser.parse_args()
if hasattr(options, 'load') and options.load:
action = LOAD
if action == DUMP:
if len(args) > 0:
parser.print_help()
exit(4)
do_dump(options)
else:
if len(args) > 1:
parser.print_help()
exit(4)
do_load(options, args)