procutil: bulk-replace util.std* to point to new module
authorYuya Nishihara <yuya@tcha.org>
Sat, 24 Mar 2018 15:09:33 +0900
changeset 44525 d4a2e0d5d04283d2ba955143d6a7e8bcb45b88f0
parent 44524 5be286db5fb5d439d3daaa71e075193d27f62ce4
child 44526 a8a902d7176e68cd60a48d5e8a211b069ccd2592
push id767
push usergszorc@mozilla.com
push dateTue, 27 Mar 2018 00:12:33 +0000
procutil: bulk-replace util.std* to point to new module
mercurial/chgserver.py
mercurial/commandserver.py
mercurial/dispatch.py
mercurial/hgweb/wsgicgi.py
mercurial/hook.py
mercurial/keepalive.py
mercurial/server.py
mercurial/ui.py
mercurial/wireproto.py
tests/test-commit-interactive.t
tests/test-config-env.py
--- a/mercurial/chgserver.py
+++ b/mercurial/chgserver.py
@@ -56,16 +56,20 @@ from . import (
     encoding,
     error,
     extensions,
     node,
     pycompat,
     util,
 )
 
+from .utils import (
+    procutil,
+)
+
 _log = commandserver.log
 
 def _hashlist(items):
     """return sha1 hexdigest for a list"""
     return node.hex(hashlib.sha1(str(items)).digest())
 
 # sensitive config sections affecting confighash
 _configsections = [
@@ -195,17 +199,17 @@ def _newchgui(srcui, csystem, attachio):
 
         def _runsystem(self, cmd, environ, cwd, out):
             # fallback to the original system method if the output needs to be
             # captured (to self._buffers), or the output stream is not stdout
             # (e.g. stderr, cStringIO), because the chg client is not aware of
             # these situations and will behave differently (write to stdout).
             if (out is not self.fout
                 or not util.safehasattr(self.fout, 'fileno')
-                or self.fout.fileno() != util.stdout.fileno()):
+                or self.fout.fileno() != procutil.stdout.fileno()):
                 return util.system(cmd, environ=environ, cwd=cwd, out=out)
             self.flush()
             return self._csystem(cmd, util.shellenviron(environ), cwd)
 
         def _runpager(self, cmd, env=None):
             self._csystem(cmd, util.shellenviron(env), type='pager',
                           cmdtable={'attachio': attachio})
             return True
--- a/mercurial/commandserver.py
+++ b/mercurial/commandserver.py
@@ -24,16 +24,19 @@ except ImportError:
 
 from .i18n import _
 from . import (
     encoding,
     error,
     pycompat,
     util,
 )
+from .utils import (
+    procutil,
+)
 
 logfile = None
 
 def log(*args):
     if not logfile:
         return
 
     for a in args:
@@ -303,18 +306,18 @@ class server(object):
 
         return 0
 
 def _protectio(ui):
     """ duplicates streams and redirect original to null if ui uses stdio """
     ui.flush()
     newfiles = []
     nullfd = os.open(os.devnull, os.O_RDWR)
-    for f, sysf, mode in [(ui.fin, util.stdin, r'rb'),
-                          (ui.fout, util.stdout, r'wb')]:
+    for f, sysf, mode in [(ui.fin, procutil.stdin, r'rb'),
+                          (ui.fout, procutil.stdout, r'wb')]:
         if f is sysf:
             newfd = os.dup(f.fileno())
             os.dup2(nullfd, f.fileno())
             f = os.fdopen(newfd, mode)
         newfiles.append(f)
     os.close(nullfd)
     return tuple(newfiles)
 
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -37,16 +37,17 @@ from . import (
     pycompat,
     registrar,
     scmutil,
     ui as uimod,
     util,
 )
 
 from .utils import (
+    procutil,
     stringutil,
 )
 
 unrecoverablewrite = registrar.command.unrecoverablewrite
 
 class request(object):
     def __init__(self, args, ui=None, repo=None, fin=None, fout=None,
                  ferr=None, prereposetups=None):
@@ -122,18 +123,18 @@ if pycompat.ispy3:
             # Check if the file is okay
             try:
                 fp.flush()
                 continue
             except IOError:
                 pass
             # Otherwise mark it as closed to silence "Exception ignored in"
             # message emitted by the interpreter finalizer. Be careful to
-            # not close util.stdout, which may be a fdopen-ed file object and
-            # its close() actually closes the underlying file descriptor.
+            # not close procutil.stdout, which may be a fdopen-ed file object
+            # and its close() actually closes the underlying file descriptor.
             try:
                 fp.close()
             except IOError:
                 pass
 else:
     def _initstdio():
         for fp in (sys.stdin, sys.stdout, sys.stderr):
             util.setbinary(fp)
@@ -175,17 +176,17 @@ def _formatargs(args):
 
 def dispatch(req):
     "run the command specified in req.args"
     if req.ferr:
         ferr = req.ferr
     elif req.ui:
         ferr = req.ui.ferr
     else:
-        ferr = util.stderr
+        ferr = procutil.stderr
 
     try:
         if not req.ui:
             req.ui = uimod.ui.load()
         req.earlyoptions.update(_earlyparseopts(req.ui, req.args))
         if req.earlyoptions['traceback']:
             req.ui.setconfig('ui', 'traceback', 'on', '--traceback')
 
--- a/mercurial/hgweb/wsgicgi.py
+++ b/mercurial/hgweb/wsgicgi.py
@@ -10,51 +10,55 @@
 
 from __future__ import absolute_import
 
 from .. import (
     encoding,
     util,
 )
 
+from ..utils import (
+    procutil,
+)
+
 from . import (
     common,
 )
 
 def launch(application):
-    util.setbinary(util.stdin)
-    util.setbinary(util.stdout)
+    util.setbinary(procutil.stdin)
+    util.setbinary(procutil.stdout)
 
     environ = dict(encoding.environ.iteritems())
     environ.setdefault(r'PATH_INFO', '')
     if environ.get(r'SERVER_SOFTWARE', r'').startswith(r'Microsoft-IIS'):
         # IIS includes script_name in PATH_INFO
         scriptname = environ[r'SCRIPT_NAME']
         if environ[r'PATH_INFO'].startswith(scriptname):
             environ[r'PATH_INFO'] = environ[r'PATH_INFO'][len(scriptname):]
 
-    stdin = util.stdin
+    stdin = procutil.stdin
     if environ.get(r'HTTP_EXPECT', r'').lower() == r'100-continue':
-        stdin = common.continuereader(stdin, util.stdout.write)
+        stdin = common.continuereader(stdin, procutil.stdout.write)
 
     environ[r'wsgi.input'] = stdin
-    environ[r'wsgi.errors'] = util.stderr
+    environ[r'wsgi.errors'] = procutil.stderr
     environ[r'wsgi.version'] = (1, 0)
     environ[r'wsgi.multithread'] = False
     environ[r'wsgi.multiprocess'] = True
     environ[r'wsgi.run_once'] = True
 
     if environ.get(r'HTTPS', r'off').lower() in (r'on', r'1', r'yes'):
         environ[r'wsgi.url_scheme'] = r'https'
     else:
         environ[r'wsgi.url_scheme'] = r'http'
 
     headers_set = []
     headers_sent = []
-    out = util.stdout
+    out = procutil.stdout
 
     def write(data):
         if not headers_set:
             raise AssertionError("write() before start_response()")
 
         elif not headers_sent:
             # Before the first output, send the stored headers
             status, response_headers = headers_sent[:] = headers_set
--- a/mercurial/hook.py
+++ b/mercurial/hook.py
@@ -14,16 +14,19 @@ from .i18n import _
 from . import (
     demandimport,
     encoding,
     error,
     extensions,
     pycompat,
     util,
 )
+from .utils import (
+    procutil,
+)
 
 def _pythonhook(ui, repo, htype, hname, funcname, args, throw):
     '''call python hook. hook is callable object, looked up as
     name in python module. if callable returns "true", hook
     fails, else passes. if hook raises exception, treated as
     hook failure. exception propagates if throw is "true".
 
     reason for "true" meaning "hook failed" is so that
@@ -217,21 +220,21 @@ def runhooks(ui, repo, htype, hooks, thr
     args = pycompat.byteskwargs(args)
     res = {}
     oldstdout = -1
 
     try:
         for hname, cmd in hooks:
             if oldstdout == -1 and _redirect:
                 try:
-                    stdoutno = util.stdout.fileno()
-                    stderrno = util.stderr.fileno()
+                    stdoutno = procutil.stdout.fileno()
+                    stderrno = procutil.stderr.fileno()
                     # temporarily redirect stdout to stderr, if possible
                     if stdoutno >= 0 and stderrno >= 0:
-                        util.stdout.flush()
+                        procutil.stdout.flush()
                         oldstdout = os.dup(stdoutno)
                         os.dup2(stderrno, stdoutno)
                 except (OSError, AttributeError):
                     # files seem to be bogus, give up on redirecting (WSGI, etc)
                     pass
 
             if cmd is _fromuntrusted:
                 if throw:
@@ -264,16 +267,16 @@ def runhooks(ui, repo, htype, hooks, thr
                 r = _exthook(ui, repo, htype, hname, cmd, args, throw)
                 raised = False
 
             res[hname] = r, raised
     finally:
         # The stderr is fully buffered on Windows when connected to a pipe.
         # A forcible flush is required to make small stderr data in the
         # remote side available to the client immediately.
-        util.stderr.flush()
+        procutil.stderr.flush()
 
         if _redirect and oldstdout >= 0:
-            util.stdout.flush()  # write hook output to stderr fd
+            procutil.stdout.flush()  # write hook output to stderr fd
             os.dup2(oldstdout, stdoutno)
             os.close(oldstdout)
 
     return res
--- a/mercurial/keepalive.py
+++ b/mercurial/keepalive.py
@@ -92,16 +92,19 @@ import threading
 
 from .i18n import _
 from . import (
     node,
     pycompat,
     urllibcompat,
     util,
 )
+from .utils import (
+    procutil,
+)
 
 httplib = util.httplib
 urlerr = util.urlerr
 urlreq = util.urlreq
 
 DEBUG = None
 
 class ConnectionManager(object):
@@ -630,24 +633,24 @@ def continuity(url):
             break
     fo.close()
     m = md5(foo)
     print(format % ('keepalive readline', node.hex(m.digest())))
 
 def comp(N, url):
     print('  making %i connections to:\n  %s' % (N, url))
 
-    util.stdout.write('  first using the normal urllib handlers')
+    procutil.stdout.write('  first using the normal urllib handlers')
     # first use normal opener
     opener = urlreq.buildopener()
     urlreq.installopener(opener)
     t1 = fetch(N, url)
     print('  TIME: %.3f s' % t1)
 
-    util.stdout.write('  now using the keepalive handler       ')
+    procutil.stdout.write('  now using the keepalive handler       ')
     # now install the keepalive handler and try again
     opener = urlreq.buildopener(HTTPHandler())
     urlreq.installopener(opener)
     t2 = fetch(N, url)
     print('  TIME: %.3f s' % t2)
     print('  improvement factor: %.2f' % (t1 / t2))
 
 def fetch(N, url, delay=0):
@@ -682,21 +685,21 @@ def test_timeout(url):
     print("  fetching the file to establish a connection")
     fo = urlreq.urlopen(url)
     data1 = fo.read()
     fo.close()
 
     i = 20
     print("  waiting %i seconds for the server to close the connection" % i)
     while i > 0:
-        util.stdout.write('\r  %2i' % i)
-        util.stdout.flush()
+        procutil.stdout.write('\r  %2i' % i)
+        procutil.stdout.flush()
         time.sleep(1)
         i -= 1
-    util.stderr.write('\r')
+    procutil.stderr.write('\r')
 
     print("  fetching the file a second time")
     fo = urlreq.urlopen(url)
     data2 = fo.read()
     fo.close()
 
     if data1 == data2:
         print('  data are identical')
--- a/mercurial/server.py
+++ b/mercurial/server.py
@@ -17,16 +17,20 @@ from . import (
     cmdutil,
     commandserver,
     error,
     hgweb,
     pycompat,
     util,
 )
 
+from .utils import (
+    procutil,
+)
+
 def runservice(opts, parentfn=None, initfn=None, runfn=None, logfile=None,
                runargs=None, appendpid=False):
     '''Run a command as a service.'''
 
     def writepid(pid):
         if opts['pid_file']:
             if appendpid:
                 mode = 'ab'
@@ -82,18 +86,18 @@ def runservice(opts, parentfn=None, init
                 lockpath = inst[7:]
                 os.unlink(lockpath)
             elif inst.startswith('chdir:'):
                 os.chdir(inst[6:])
             elif inst != 'none':
                 raise error.Abort(_('invalid value for --daemon-postexec: %s')
                                   % inst)
         util.hidewindow()
-        util.stdout.flush()
-        util.stderr.flush()
+        procutil.stdout.flush()
+        procutil.stderr.flush()
 
         nullfd = os.open(os.devnull, os.O_RDWR)
         logfilefd = nullfd
         if logfile:
             logfilefd = os.open(logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND,
                                 0o666)
         os.dup2(nullfd, 0)
         os.dup2(logfilefd, 1)
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -34,16 +34,17 @@ from . import (
     progress,
     pycompat,
     rcutil,
     scmutil,
     util,
 )
 from .utils import (
     dateutil,
+    procutil,
     stringutil,
 )
 
 urlreq = util.urlreq
 
 # for use with str.translate(None, _keepalnum), to keep just alphanumerics
 _keepalnum = ''.join(c for c in map(pycompat.bytechr, range(256))
                      if not c.isalnum())
@@ -245,19 +246,19 @@ class ui(object):
             self._terminfoparams = src._terminfoparams.copy()
             self._styles = src._styles.copy()
 
             self.fixconfig()
 
             self.httppasswordmgrdb = src.httppasswordmgrdb
             self._blockedtimes = src._blockedtimes
         else:
-            self.fout = util.stdout
-            self.ferr = util.stderr
-            self.fin = util.stdin
+            self.fout = procutil.stdout
+            self.ferr = procutil.stderr
+            self.fin = procutil.stdin
             self.pageractive = False
             self._disablepager = False
             self._tweaked = False
 
             # shared read-only environment
             self.environ = encoding.environ
 
             self.httppasswordmgrdb = httppasswordmgrdbproxy()
@@ -1094,40 +1095,40 @@ class ui(object):
                 return False
 
             command = fullcmd
 
         try:
             pager = subprocess.Popen(
                 command, shell=shell, bufsize=-1,
                 close_fds=util.closefds, stdin=subprocess.PIPE,
-                stdout=util.stdout, stderr=util.stderr,
+                stdout=procutil.stdout, stderr=procutil.stderr,
                 env=util.shellenviron(env))
         except OSError as e:
             if e.errno == errno.ENOENT and not shell:
                 self.warn(_("missing pager command '%s', skipping pager\n")
                           % command)
                 return False
             raise
 
         # back up original file descriptors
-        stdoutfd = os.dup(util.stdout.fileno())
-        stderrfd = os.dup(util.stderr.fileno())
+        stdoutfd = os.dup(procutil.stdout.fileno())
+        stderrfd = os.dup(procutil.stderr.fileno())
 
-        os.dup2(pager.stdin.fileno(), util.stdout.fileno())
-        if self._isatty(util.stderr):
-            os.dup2(pager.stdin.fileno(), util.stderr.fileno())
+        os.dup2(pager.stdin.fileno(), procutil.stdout.fileno())
+        if self._isatty(procutil.stderr):
+            os.dup2(pager.stdin.fileno(), procutil.stderr.fileno())
 
         @self.atexit
         def killpager():
             if util.safehasattr(signal, "SIGINT"):
                 signal.signal(signal.SIGINT, signal.SIG_IGN)
             # restore original fds, closing pager.stdin copies in the process
-            os.dup2(stdoutfd, util.stdout.fileno())
-            os.dup2(stderrfd, util.stderr.fileno())
+            os.dup2(stdoutfd, procutil.stdout.fileno())
+            os.dup2(stderrfd, procutil.stderr.fileno())
             pager.stdin.close()
             pager.wait()
 
         return True
 
     @property
     def _exithandlers(self):
         return _reqexithandlers
--- a/mercurial/wireproto.py
+++ b/mercurial/wireproto.py
@@ -30,16 +30,17 @@ from . import (
     pycompat,
     repository,
     streamclone,
     util,
     wireprototypes,
 )
 
 from .utils import (
+    procutil,
     stringutil,
 )
 
 urlerr = util.urlerr
 urlreq = util.urlreq
 
 bytesresponse = wireprototypes.bytesresponse
 ooberror = wireprototypes.ooberror
@@ -514,18 +515,18 @@ def dispatch(repo, proto, command):
 
 def options(cmd, keys, others):
     opts = {}
     for k in keys:
         if k in others:
             opts[k] = others[k]
             del others[k]
     if others:
-        util.stderr.write("warning: %s ignored unexpected arguments %s\n"
-                          % (cmd, ",".join(others)))
+        procutil.stderr.write("warning: %s ignored unexpected arguments %s\n"
+                              % (cmd, ",".join(others)))
     return opts
 
 def bundle1allowed(repo, action):
     """Whether a bundle1 operation is allowed from the server.
 
     Priority is:
 
     1. server.bundle1gd.<action> (if generaldelta active)
@@ -1076,24 +1077,24 @@ def unbundle(repo, proto, heads):
                 os.unlink(tempname)
 
         except (error.BundleValueError, error.Abort, error.PushRaced) as exc:
             # handle non-bundle2 case first
             if not getattr(exc, 'duringunbundle2', False):
                 try:
                     raise
                 except error.Abort:
-                    # The old code we moved used util.stderr directly.
+                    # The old code we moved used procutil.stderr directly.
                     # We did not change it to minimise code change.
                     # This need to be moved to something proper.
                     # Feel free to do it.
-                    util.stderr.write("abort: %s\n" % exc)
+                    procutil.stderr.write("abort: %s\n" % exc)
                     if exc.hint is not None:
-                        util.stderr.write("(%s)\n" % exc.hint)
-                    util.stderr.flush()
+                        procutil.stderr.write("(%s)\n" % exc.hint)
+                    procutil.stderr.flush()
                     return pushres(0, output.getvalue() if output else '')
                 except error.PushRaced:
                     return pusherr(pycompat.bytestr(exc),
                                    output.getvalue() if output else '')
 
             bundler = bundle2.bundle20(repo.ui)
             for out in getattr(exc, '_bundle2salvagedoutput', ()):
                 bundler.addpart(out)
--- a/tests/test-commit-interactive.t
+++ b/tests/test-commit-interactive.t
@@ -895,26 +895,29 @@ This tests that translated help message 
 
   $ LANGUAGE=ja
   $ export LANGUAGE
 
   $ cat > $TESTTMP/escape.py <<EOF
   > from __future__ import absolute_import
   > from mercurial import (
   >     pycompat,
-  >     util,
+  > )
+  > from mercurial.utils import (
+  >     procutil,
   > )
   > def escape(c):
   >     o = ord(c)
   >     if o < 0x80:
   >         return c
   >     else:
   >         return br'\x%02x' % o # escape char setting MSB
-  > for l in util.stdin:
-  >     util.stdout.write(b''.join(escape(c) for c in pycompat.iterbytestr(l)))
+  > for l in procutil.stdin:
+  >     procutil.stdout.write(
+  >         b''.join(escape(c) for c in pycompat.iterbytestr(l)))
   > EOF
 
   $ hg commit -i --encoding cp932 2>&1 <<EOF | $PYTHON $TESTTMP/escape.py | grep '^y - '
   > ?
   > q
   > EOF
   y - \x82\xb1\x82\xcc\x95\xcf\x8dX\x82\xf0\x8bL\x98^(yes)
 
--- a/tests/test-config-env.py
+++ b/tests/test-config-env.py
@@ -6,16 +6,20 @@ import os
 
 from mercurial import (
     encoding,
     rcutil,
     ui as uimod,
     util,
 )
 
+from mercurial.utils import (
+    procutil,
+)
+
 testtmp = encoding.environ[b'TESTTMP']
 
 # prepare hgrc files
 def join(name):
     return os.path.join(testtmp, name)
 
 with open(join(b'sysrc'), 'wb') as f:
     f.write(b'[ui]\neditor=e0\n[pager]\npager=p0\n')
@@ -36,15 +40,15 @@ os.path.isdir = lambda x: False # hack: 
 
 # utility to print configs
 def printconfigs(env):
     encoding.environ = env
     rcutil._rccomponents = None # reset cache
     ui = uimod.ui.load()
     for section, name, value in ui.walkconfig():
         source = ui.configsource(section, name)
-        util.stdout.write(b'%s.%s=%s # %s\n'
-                          % (section, name, value, util.pconvert(source)))
-    util.stdout.write(b'\n')
+        procutil.stdout.write(b'%s.%s=%s # %s\n'
+                              % (section, name, value, util.pconvert(source)))
+    procutil.stdout.write(b'\n')
 
 # environment variable overrides
 printconfigs({})
 printconfigs({b'EDITOR': b'e2', b'PAGER': b'p2'})