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