##// END OF EJS Templates
ui: use I/O descriptors internally...
Idan Kamara -
r14614:afccc64e default
parent child Browse files
Show More
@@ -5,16 +5,17 b''
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 import cgi, cStringIO, zlib, sys, urllib
8 import cgi, cStringIO, zlib, urllib
9 from mercurial import util, wireproto
9 from mercurial import util, wireproto
10 from common import HTTP_OK
10 from common import HTTP_OK
11
11
12 HGTYPE = 'application/mercurial-0.1'
12 HGTYPE = 'application/mercurial-0.1'
13
13
14 class webproto(object):
14 class webproto(object):
15 def __init__(self, req):
15 def __init__(self, req, ui):
16 self.req = req
16 self.req = req
17 self.response = ''
17 self.response = ''
18 self.ui = ui
18 def getargs(self, args):
19 def getargs(self, args):
19 knownargs = self._args()
20 knownargs = self._args()
20 data = {}
21 data = {}
@@ -46,8 +47,12 b' class webproto(object):'
46 for s in util.filechunkiter(self.req, limit=length):
47 for s in util.filechunkiter(self.req, limit=length):
47 fp.write(s)
48 fp.write(s)
48 def redirect(self):
49 def redirect(self):
49 self.oldio = sys.stdout, sys.stderr
50 self.oldio = self.ui.fout, self.ui.ferr
50 sys.stderr = sys.stdout = cStringIO.StringIO()
51 self.ui.ferr = self.ui.fout = cStringIO.StringIO()
52 def restore(self):
53 val = self.ui.fout.getvalue()
54 self.ui.ferr, self.ui.fout = self.oldio
55 return val
51 def groupchunks(self, cg):
56 def groupchunks(self, cg):
52 z = zlib.compressobj()
57 z = zlib.compressobj()
53 while True:
58 while True:
@@ -66,7 +71,7 b' def iscmd(cmd):'
66 return cmd in wireproto.commands
71 return cmd in wireproto.commands
67
72
68 def call(repo, req, cmd):
73 def call(repo, req, cmd):
69 p = webproto(req)
74 p = webproto(req, repo.ui)
70 rsp = wireproto.dispatch(repo, p, cmd)
75 rsp = wireproto.dispatch(repo, p, cmd)
71 if isinstance(rsp, str):
76 if isinstance(rsp, str):
72 req.respond(HTTP_OK, HGTYPE, length=len(rsp))
77 req.respond(HTTP_OK, HGTYPE, length=len(rsp))
@@ -75,14 +80,13 b' def call(repo, req, cmd):'
75 req.respond(HTTP_OK, HGTYPE)
80 req.respond(HTTP_OK, HGTYPE)
76 return rsp.gen
81 return rsp.gen
77 elif isinstance(rsp, wireproto.pushres):
82 elif isinstance(rsp, wireproto.pushres):
78 val = sys.stdout.getvalue()
83 val = p.restore()
79 sys.stdout, sys.stderr = p.oldio
80 req.respond(HTTP_OK, HGTYPE)
84 req.respond(HTTP_OK, HGTYPE)
81 return ['%d\n%s' % (rsp.res, val)]
85 return ['%d\n%s' % (rsp.res, val)]
82 elif isinstance(rsp, wireproto.pusherr):
86 elif isinstance(rsp, wireproto.pusherr):
83 # drain the incoming bundle
87 # drain the incoming bundle
84 req.drain()
88 req.drain()
85 sys.stdout, sys.stderr = p.oldio
89 p.restore()
86 rsp = '0\n%s\n' % rsp.res
90 rsp = '0\n%s\n' % rsp.res
87 req.respond(HTTP_OK, HGTYPE, length=len(rsp))
91 req.respond(HTTP_OK, HGTYPE, length=len(rsp))
88 return [rsp]
92 return [rsp]
@@ -14,11 +14,11 b' class sshserver(object):'
14 self.ui = ui
14 self.ui = ui
15 self.repo = repo
15 self.repo = repo
16 self.lock = None
16 self.lock = None
17 self.fin = sys.stdin
17 self.fin = ui.fin
18 self.fout = sys.stdout
18 self.fout = ui.fout
19
19
20 hook.redirect(True)
20 hook.redirect(True)
21 sys.stdout = sys.stderr
21 ui.fout = repo.ui.fout = ui.ferr
22
22
23 # Prevent insertion/deletion of CRs
23 # Prevent insertion/deletion of CRs
24 util.setbinary(self.fin)
24 util.setbinary(self.fin)
@@ -443,26 +443,26 b' class ui(object):'
443 self._buffers[-1].extend([str(a) for a in args])
443 self._buffers[-1].extend([str(a) for a in args])
444 else:
444 else:
445 for a in args:
445 for a in args:
446 sys.stdout.write(str(a))
446 self.fout.write(str(a))
447
447
448 def write_err(self, *args, **opts):
448 def write_err(self, *args, **opts):
449 try:
449 try:
450 if not getattr(sys.stdout, 'closed', False):
450 if not getattr(self.fout, 'closed', False):
451 sys.stdout.flush()
451 self.fout.flush()
452 for a in args:
452 for a in args:
453 sys.stderr.write(str(a))
453 self.ferr.write(str(a))
454 # stderr may be buffered under win32 when redirected to files,
454 # stderr may be buffered under win32 when redirected to files,
455 # including stdout.
455 # including stdout.
456 if not getattr(sys.stderr, 'closed', False):
456 if not getattr(self.ferr, 'closed', False):
457 sys.stderr.flush()
457 self.ferr.flush()
458 except IOError, inst:
458 except IOError, inst:
459 if inst.errno not in (errno.EPIPE, errno.EIO):
459 if inst.errno not in (errno.EPIPE, errno.EIO):
460 raise
460 raise
461
461
462 def flush(self):
462 def flush(self):
463 try: sys.stdout.flush()
463 try: self.fout.flush()
464 except: pass
464 except: pass
465 try: sys.stderr.flush()
465 try: self.ferr.flush()
466 except: pass
466 except: pass
467
467
468 def interactive(self):
468 def interactive(self):
@@ -483,7 +483,7 b' class ui(object):'
483 if i is None:
483 if i is None:
484 # some environments replace stdin without implementing isatty
484 # some environments replace stdin without implementing isatty
485 # usually those are non-interactive
485 # usually those are non-interactive
486 return util.isatty(sys.stdin)
486 return util.isatty(self.fin)
487
487
488 return i
488 return i
489
489
@@ -521,12 +521,12 b' class ui(object):'
521 if i is None:
521 if i is None:
522 # some environments replace stdout without implementing isatty
522 # some environments replace stdout without implementing isatty
523 # usually those are non-interactive
523 # usually those are non-interactive
524 return util.isatty(sys.stdout)
524 return util.isatty(self.fout)
525
525
526 return i
526 return i
527
527
528 def _readline(self, prompt=''):
528 def _readline(self, prompt=''):
529 if util.isatty(sys.stdin):
529 if util.isatty(self.fin):
530 try:
530 try:
531 # magically add command line editing support, where
531 # magically add command line editing support, where
532 # available
532 # available
@@ -536,7 +536,14 b' class ui(object):'
536 # windows sometimes raises something other than ImportError
536 # windows sometimes raises something other than ImportError
537 except Exception:
537 except Exception:
538 pass
538 pass
539
540 # instead of trying to emulate raw_input, swap our in/out
541 # with sys.stdin/out
542 old = sys.stdout, sys.stdin
543 sys.stdout, sys.stdin = self.fout, self.fin
539 line = raw_input(prompt)
544 line = raw_input(prompt)
545 sys.stdout, sys.stdin = old
546
540 # When stdin is in binary mode on Windows, it can cause
547 # When stdin is in binary mode on Windows, it can cause
541 # raw_input() to emit an extra trailing carriage return
548 # raw_input() to emit an extra trailing carriage return
542 if os.linesep == '\r\n' and line and line[-1] == '\r':
549 if os.linesep == '\r\n' and line and line[-1] == '\r':
@@ -19,13 +19,13 b' hgrc.close()'
19 ui_ = ui.ui()
19 ui_ = ui.ui()
20 ui_.setconfig('ui', 'formatted', 'True')
20 ui_.setconfig('ui', 'formatted', 'True')
21
21
22 # we're not interested in the output, so write that to devnull
23 ui_.fout = open(os.devnull, 'w')
24
22 # call some arbitrary command just so we go through
25 # call some arbitrary command just so we go through
23 # color's wrapped _runcommand twice.
26 # color's wrapped _runcommand twice.
24 # we're not interested in the output, so write that to devnull
25 def runcmd():
27 def runcmd():
26 sys.stdout = open(os.devnull, 'w')
27 dispatch.dispatch(dispatch.request(['version', '-q'], ui_))
28 dispatch.dispatch(dispatch.request(['version', '-q'], ui_))
28 sys.stdout = sys.__stdout__
29
29
30 runcmd()
30 runcmd()
31 print "colored? " + str(issubclass(ui_.__class__, color.colorui))
31 print "colored? " + str(issubclass(ui_.__class__, color.colorui))
General Comments 0
You need to be logged in to leave comments. Login now