##// END OF EJS Templates
push: indent the some part of the command...
push: indent the some part of the command That code will be put in a loop in the next changeset, pre-indenting make the next change clearer. Differential Revision: https://phab.mercurial-scm.org/D10160

File last commit:

r46761:6383bb86 default
r47535:25850879 default
Show More
chgserver.py
763 lines | 26.1 KiB | text/x-python | PythonLexer
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 # chgserver.py - command server extension for cHg
#
# Copyright 2011 Yuya Nishihara <yuya@tcha.org>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
"""command server extension for cHg
'S' channel (read/write)
propagate ui.system() request to client
'attachio' command
attach client's stdio passed by sendmsg()
'chdir' command
change current directory
'setenv' command
replace os.environ completely
Yuya Nishihara
chgserver: add "setumask2" command which uses correct message frame...
r40144 'setumask' command (DEPRECATED)
'setumask2' command
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 set umask
'validate' command
reload the config and check if the server is up to date
Config
------
::
[chgserver]
Jun Wu
chgserver: move comments in config example...
r30990 # how long (in seconds) should an idle chg server exit
idletimeout = 3600
# whether to skip config or env change checks
skiphash = False
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 """
from __future__ import absolute_import
import inspect
import os
import re
Jun Wu
commandserver: move "listen" responsibility from service to handler...
r32232 import socket
Augie Fackler
cleanup: use stat_result[stat.ST_MTIME] instead of stat_result.st_mtime...
r36799 import stat
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 import struct
import time
from .i18n import _
Gregory Szorc
py3: manually import getattr where it is needed...
r43359 from .pycompat import (
getattr,
setattr,
)
Joerg Sonnenberger
node: import symbols explicitly...
r46729 from .node import hex
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
from . import (
commandserver,
Pulkit Goyal
py3: replace os.environ with encoding.environ (part 2 of 5)
r30635 encoding,
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 error,
extensions,
Pulkit Goyal
py3: replace sys.executable with pycompat.sysexecutable...
r30669 pycompat,
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 util,
)
Yuya Nishihara
procutil: bulk-replace util.std* to point to new module
r37137 from .utils import (
Augie Fackler
core: migrate uses of hashlib.sha1 to hashutil.sha1...
r44517 hashutil,
Yuya Nishihara
procutil: bulk-replace util.std* to point to new module
r37137 procutil,
Pulkit Goyal
py3: port things from chgserver.py...
r41994 stringutil,
Yuya Nishihara
procutil: bulk-replace util.std* to point to new module
r37137 )
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 def _hashlist(items):
"""return sha1 hexdigest for a list"""
Joerg Sonnenberger
node: import symbols explicitly...
r46729 return hex(hashutil.sha1(stringutil.pprint(items)).digest())
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 # sensitive config sections affecting confighash
_configsections = [
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'alias', # affects global state commands.table
Pulkit Goyal
chgserver: add merge-tools to sensitive config items...
r45106 b'diff-tools', # affects whether gui or not in extdiff's uisetup
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'eol', # uses setconfig('eol', ...)
b'extdiff', # uisetup will register new commands
b'extensions',
Pulkit Goyal
chgserver: add fastannotate config section to sensitive list...
r45102 b'fastannotate', # affects annotate command and adds fastannonate cmd
Pulkit Goyal
chgserver: add merge-tools to sensitive config items...
r45106 b'merge-tools', # affects whether gui or not in extdiff's uisetup
Yuya Nishihara
chgserver: spawn new process if schemes change...
r44832 b'schemes', # extsetup will update global hg.schemes
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 ]
Jun Wu
test-show: make it compatible with chg...
r34840 _configsectionitems = [
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 (b'commands', b'show.aliasprefix'), # show.py reads it in extsetup
Jun Wu
test-show: make it compatible with chg...
r34840 ]
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 # sensitive environment variables affecting confighash
Augie Fackler
formatting: blacken the codebase...
r43346 _envre = re.compile(
br'''\A(?:
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 CHGHG
Jun Wu
chgserver: more explicit about sensitive environ variables...
r32271 |HG(?:DEMANDIMPORT|EMITWARNINGS|MODULEPOLICY|PROF|RCPATH)?
|HG(?:ENCODING|PLAIN).*
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 |LANG(?:UAGE)?
|LC_.*
|LD_.*
|PATH
|PYTHON.*
|TERM(?:INFO)?
|TZ
Augie Fackler
formatting: blacken the codebase...
r43346 )\Z''',
re.X,
)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
def _confighash(ui):
"""return a quick hash for detecting config/env changes
confighash is the hash of sensitive config items and environment variables.
for chgserver, it is designed that once confighash changes, the server is
not qualified to serve its client and should redirect the client to a new
server. different from mtimehash, confighash change will not mark the
server outdated and exit since the user can have different configs at the
same time.
"""
sectionitems = []
for section in _configsections:
sectionitems.append(ui.configitems(section))
Jun Wu
test-show: make it compatible with chg...
r34840 for section, item in _configsectionitems:
sectionitems.append(ui.config(section, item))
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 sectionhash = _hashlist(sectionitems)
Jun Wu
chgserver: do not treat HG as sensitive environ when CHGHG is set...
r34888 # If $CHGHG is set, the change to $HG should not trigger a new chg server
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b'CHGHG' in encoding.environ:
ignored = {b'HG'}
Jun Wu
chgserver: do not treat HG as sensitive environ when CHGHG is set...
r34888 else:
ignored = set()
Augie Fackler
formatting: blacken the codebase...
r43346 envitems = [
(k, v)
Gregory Szorc
py3: finish porting iteritems() to pycompat and remove source transformer...
r43376 for k, v in pycompat.iteritems(encoding.environ)
Augie Fackler
formatting: blacken the codebase...
r43346 if _envre.match(k) and k not in ignored
]
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 envhash = _hashlist(sorted(envitems))
return sectionhash[:6] + envhash[:6]
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 def _getmtimepaths(ui):
"""get a list of paths that should be checked to detect change
The list will include:
- extensions (will not cover all files for complex extensions)
- mercurial/__version__.py
- python binary
"""
modules = [m for n, m in extensions.extensions(ui)]
try:
from . import __version__
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 modules.append(__version__)
except ImportError:
pass
Rodrigo Damazio Bovendorp
pycompat: make fewer assumptions about sys.executable...
r42723 files = []
if pycompat.sysexecutable:
files.append(pycompat.sysexecutable)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 for m in modules:
try:
Pulkit Goyal
py3: convert return values of inspect.getabsfile() to bytes...
r41982 files.append(pycompat.fsencode(inspect.getabsfile(m)))
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 except TypeError:
pass
return sorted(set(files))
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 def _mtimehash(paths):
"""return a quick hash for detecting file changes
mtimehash calls stat on given paths and calculate a hash based on size and
mtime of each file. mtimehash does not read file content because reading is
expensive. therefore it's not 100% reliable for detecting content changes.
it's possible to return different hashes for same file contents.
it's also possible to return a same hash for different file contents for
some carefully crafted situation.
for chgserver, it is designed that once mtimehash changes, the server is
considered outdated immediately and should no longer provide service.
mtimehash is not included in confighash because we only know the paths of
extensions after importing them (there is imp.find_module but that faces
race conditions). We need to calculate confighash without importing.
"""
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 def trystat(path):
try:
st = os.stat(path)
Augie Fackler
cleanup: use stat_result[stat.ST_MTIME] instead of stat_result.st_mtime...
r36799 return (st[stat.ST_MTIME], st.st_size)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 except OSError:
# could be ENOENT, EPERM etc. not fatal in any case
pass
Augie Fackler
formatting: blacken the codebase...
r43346
Martin von Zweigbergk
py3: use pycompat.maplist() in chgserver...
r43086 return _hashlist(pycompat.maplist(trystat, paths))[:12]
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 class hashstate(object):
"""a structure storing confighash, mtimehash, paths used for mtimehash"""
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 def __init__(self, confighash, mtimehash, mtimepaths):
self.confighash = confighash
self.mtimehash = mtimehash
self.mtimepaths = mtimepaths
@staticmethod
def fromui(ui, mtimepaths=None):
if mtimepaths is None:
mtimepaths = _getmtimepaths(ui)
confighash = _confighash(ui)
mtimehash = _mtimehash(mtimepaths)
Augie Fackler
formatting: blacken the codebase...
r43346 ui.log(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'cmdserver',
b'confighash = %s mtimehash = %s\n',
Augie Fackler
formatting: blacken the codebase...
r43346 confighash,
mtimehash,
)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 return hashstate(confighash, mtimehash, mtimepaths)
Augie Fackler
formatting: blacken the codebase...
r43346
Jun Wu
chgserver: implement chgui._runpager...
r30740 def _newchgui(srcui, csystem, attachio):
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 class chgui(srcui.__class__):
def __init__(self, src=None):
super(chgui, self).__init__(src)
if src:
self._csystem = getattr(src, '_csystem', csystem)
else:
self._csystem = csystem
Yuya Nishihara
chg: deduplicate error handling of ui.system()...
r31108 def _runsystem(self, cmd, environ, cwd, out):
Yuya Nishihara
chgserver: update comment describing when to fall back to core _runsystem()...
r39874 # fallback to the original system method if
# a. the output stream is not stdout (e.g. stderr, cStringIO),
Yuya Nishihara
ui: proxy protect/restorestdio() calls to update internal flag...
r41321 # b. or stdout is redirected by protectfinout(),
Yuya Nishihara
chgserver: update comment describing when to fall back to core _runsystem()...
r39874 # because the chg client is not aware of these situations and
# will behave differently (i.e. write to stdout).
Augie Fackler
formatting: blacken the codebase...
r43346 if (
out is not self.fout
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 or not util.safehasattr(self.fout, b'fileno')
Yuya Nishihara
chgserver: do not send system() back to client if stdio redirected (issue5992)...
r39875 or self.fout.fileno() != procutil.stdout.fileno()
Augie Fackler
formatting: blacken the codebase...
r43346 or self._finoutredirected
):
Yuya Nishihara
procutil: bulk-replace function calls to point to new module
r37138 return procutil.system(cmd, environ=environ, cwd=cwd, out=out)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 self.flush()
Yuya Nishihara
procutil: bulk-replace function calls to point to new module
r37138 return self._csystem(cmd, procutil.shellenviron(environ), cwd)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
Jun Wu
pager: set some environment variables if they're not set...
r31954 def _runpager(self, cmd, env=None):
Augie Fackler
formatting: blacken the codebase...
r43346 self._csystem(
cmd,
procutil.shellenviron(env),
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 type=b'pager',
cmdtable={b'attachio': attachio},
Augie Fackler
formatting: blacken the codebase...
r43346 )
Matt Harbison
ui: defer setting pager related properties until the pager has spawned...
r31690 return True
Jun Wu
chgserver: implement chgui._runpager...
r30740
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 return chgui(srcui)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
commandserver: install logger to record server events through canonical API...
r40859 def _loadnewui(srcui, args, cdebug):
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 from . import dispatch # avoid cycle
Jun Wu
chgserver: call "load" for new ui objects...
r30572 newui = srcui.__class__.load()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 for a in [b'fin', b'fout', b'ferr', b'environ']:
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 setattr(newui, a, getattr(srcui, a))
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if util.safehasattr(srcui, b'_csystem'):
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 newui._csystem = srcui._csystem
# command line args
Yuya Nishihara
dispatch: replace _earlyreq*() with new fancyopts-based parser
r35224 options = dispatch._earlyparseopts(newui, args)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 dispatch._parseconfig(newui, options[b'config'])
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
# stolen from tortoisehg.util.copydynamicconfig()
for section, name, value in srcui.walkconfig():
source = srcui.configsource(section, name)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b':' in source or source == b'--config' or source.startswith(b'$'):
Jun Wu
chgserver: do not copy configs set by environment variables...
r31695 # path:line or command line, or environ
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 continue
newui.setconfig(section, name, value, source)
# load wd and repo config, copied from dispatch.py
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 cwd = options[b'cwd']
Yuya Nishihara
dispatch: add HGPLAIN=+strictflags to restrict early parsing of global options...
r35180 cwd = cwd and os.path.realpath(cwd) or None
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 rpath = options[b'repository']
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 path, newlui = dispatch._getlocal(newui, rpath, wd=cwd)
Yuya Nishihara
extensions: add "uipopulate" hook, called per instance, not per process...
r40760 extensions.populateui(newui)
Yuya Nishihara
commandserver: install logger to record server events through canonical API...
r40859 commandserver.setuplogging(newui, fp=cdebug)
Yuya Nishihara
extensions: add "uipopulate" hook, called per instance, not per process...
r40760 if newui is not newlui:
extensions.populateui(newlui)
Yuya Nishihara
commandserver: install logger to record server events through canonical API...
r40859 commandserver.setuplogging(newlui, fp=cdebug)
Yuya Nishihara
extensions: add "uipopulate" hook, called per instance, not per process...
r40760
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 return (newui, newlui)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 class channeledsystem(object):
"""Propagate ui.system() request in the following format:
payload length (unsigned int),
Jun Wu
chg: send type information via S channel (BC)...
r30726 type, '\0',
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 cmd, '\0',
cwd, '\0',
envkey, '=', val, '\0',
...
envkey, '=', val
Jun Wu
chg: send type information via S channel (BC)...
r30726 if type == 'system', waits for:
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
exitcode length (unsigned int),
exitcode (int)
Jun Wu
chgserver: make S channel support pager request...
r30739
if type == 'pager', repetitively waits for a command name ending with '\n'
and executes it defined by cmdtable, or exits the loop if the command name
is empty.
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 """
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 def __init__(self, in_, out, channel):
self.in_ = in_
self.out = out
self.channel = channel
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 def __call__(self, cmd, environ, cwd=None, type=b'system', cmdtable=None):
Manuel Jacob
cleanup: eliminate procutil.quotecommand()...
r45403 args = [type, cmd, os.path.abspath(cwd or b'.')]
Gregory Szorc
py3: finish porting iteritems() to pycompat and remove source transformer...
r43376 args.extend(b'%s=%s' % (k, v) for k, v in pycompat.iteritems(environ))
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 data = b'\0'.join(args)
self.out.write(struct.pack(b'>cI', self.channel, len(data)))
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 self.out.write(data)
self.out.flush()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if type == b'system':
Jun Wu
chgserver: check type passed to S channel...
r30727 length = self.in_.read(4)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 (length,) = struct.unpack(b'>I', length)
Jun Wu
chgserver: check type passed to S channel...
r30727 if length != 4:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.Abort(_(b'invalid response'))
(rc,) = struct.unpack(b'>i', self.in_.read(4))
Jun Wu
chgserver: check type passed to S channel...
r30727 return rc
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 elif type == b'pager':
Jun Wu
chgserver: make S channel support pager request...
r30739 while True:
cmd = self.in_.readline()[:-1]
if not cmd:
break
if cmdtable and cmd in cmdtable:
cmdtable[cmd]()
else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.Abort(_(b'unexpected command: %s') % cmd)
Jun Wu
chgserver: check type passed to S channel...
r30727 else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ProgrammingError(b'invalid S channel type: %s' % type)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 _iochannels = [
# server.ch, ui.fp, mode
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 (b'cin', b'fin', 'rb'),
(b'cout', b'fout', 'wb'),
(b'cerr', b'ferr', 'wb'),
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 ]
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 class chgcmdserver(commandserver.server):
Augie Fackler
formatting: blacken the codebase...
r43346 def __init__(
self, ui, repo, fin, fout, sock, prereposetups, hashstate, baseaddress
):
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 super(chgcmdserver, self).__init__(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _newchgui(ui, channeledsystem(fin, fout, b'S'), self.attachio),
Augie Fackler
formatting: blacken the codebase...
r43346 repo,
fin,
fout,
prereposetups,
)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 self.clientsock = sock
Yuya Nishihara
chgserver: add separate flag to remember if stdio fds are replaced...
r39774 self._ioattached = False
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 self._oldios = [] # original (self.ch, ui.fp, fd) before "attachio"
self.hashstate = hashstate
self.baseaddress = baseaddress
if hashstate is not None:
self.capabilities = self.capabilities.copy()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 self.capabilities[b'validate'] = chgcmdserver.validate
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
def cleanup(self):
super(chgcmdserver, self).cleanup()
# dispatch._runcatch() does not flush outputs if exception is not
# handled by dispatch._dispatch()
self.ui.flush()
self._restoreio()
Yuya Nishihara
chgserver: add separate flag to remember if stdio fds are replaced...
r39774 self._ioattached = False
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
def attachio(self):
"""Attach to client's stdio passed via unix domain socket; all
channels except cresult will no longer be used
"""
# tell client to sendmsg() with 1-byte payload, which makes it
# distinctive from "attachio\n" command consumed by client.read()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 self.clientsock.sendall(struct.pack(b'>cI', b'I', 1))
Yuya Nishihara
osutil: proxy through util (and platform) modules (API)...
r32203 clientfds = util.recvfds(self.clientsock.fileno())
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 self.ui.log(b'chgserver', b'received fds: %r\n', clientfds)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
ui = self.ui
ui.flush()
Yuya Nishihara
chgserver: add separate flag to remember if stdio fds are replaced...
r39774 self._saveio()
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 for fd, (cn, fn, mode) in zip(clientfds, _iochannels):
assert fd > 0
fp = getattr(ui, fn)
os.dup2(fd, fp.fileno())
os.close(fd)
Yuya Nishihara
chgserver: add separate flag to remember if stdio fds are replaced...
r39774 if self._ioattached:
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 continue
# reset buffering mode when client is first attached. as we want
# to see output immediately on pager, the mode stays unchanged
# when client re-attached. ferr is unchanged because it should
# be unbuffered no matter if it is a tty or not.
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if fn == b'ferr':
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 newfp = fp
Yuya Nishihara
chgserver: backport py3 buffered I/O workarounds from procutil...
r46452 elif pycompat.ispy3:
# On Python 3, the standard library doesn't offer line-buffered
# binary streams, so wrap/unwrap it.
if fp.isatty():
newfp = procutil.make_line_buffered(fp)
else:
newfp = procutil.unwrap_line_buffered(fp)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 else:
Yuya Nishihara
chgserver: backport py3 buffered I/O workarounds from procutil...
r46452 # Python 2 uses the I/O streams provided by the C library, so
# make it line-buffered explicitly. Otherwise the default would
# be decided on first write(), where fout could be a pager.
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 if fp.isatty():
bufsize = 1 # line buffered
else:
bufsize = -1 # system default
newfp = os.fdopen(fp.fileno(), mode, bufsize)
Yuya Nishihara
chgserver: backport py3 buffered I/O workarounds from procutil...
r46452 if newfp is not fp:
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 setattr(ui, fn, newfp)
setattr(self, cn, newfp)
Yuya Nishihara
chgserver: add separate flag to remember if stdio fds are replaced...
r39774 self._ioattached = True
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 self.cresult.write(struct.pack(b'>i', len(clientfds)))
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
def _saveio(self):
if self._oldios:
Yuya Nishihara
chgserver: add separate flag to remember if stdio fds are replaced...
r39774 return
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 ui = self.ui
for cn, fn, _mode in _iochannels:
ch = getattr(self, cn)
fp = getattr(ui, fn)
fd = os.dup(fp.fileno())
self._oldios.append((ch, fp, fd))
def _restoreio(self):
Yuya Nishihara
chgserver: discard buffered output before restoring fds (issue6207)...
r45745 if not self._oldios:
return
nullfd = os.open(os.devnull, os.O_WRONLY)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 ui = self.ui
Yuya Nishihara
chgserver: discard buffered output before restoring fds (issue6207)...
r45745 for (ch, fp, fd), (cn, fn, mode) in zip(self._oldios, _iochannels):
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 newfp = getattr(ui, fn)
Yuya Nishihara
chgserver: backport py3 buffered I/O workarounds from procutil...
r46452 # On Python 2, newfp and fp may be separate file objects associated
# with the same fd, so we must close newfp while it's associated
# with the client. Otherwise the new associated fd would be closed
# when newfp gets deleted. On Python 3, newfp is just a wrapper
# around fp even if newfp is not fp, so deleting newfp is safe.
if not (pycompat.ispy3 or newfp is fp):
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 newfp.close()
# restore original fd: fp is open again
Pulkit Goyal
chg: suppress OSError in _restoreio() and add some logging (issue6330)...
r45580 try:
Yuya Nishihara
chgserver: backport py3 buffered I/O workarounds from procutil...
r46452 if (pycompat.ispy3 or newfp is fp) and 'w' in mode:
Yuya Nishihara
chgserver: discard buffered output before restoring fds (issue6207)...
r45745 # Discard buffered data which couldn't be flushed because
# of EPIPE. The data should belong to the current session
# and should never persist.
os.dup2(nullfd, fp.fileno())
fp.flush()
Pulkit Goyal
chg: suppress OSError in _restoreio() and add some logging (issue6330)...
r45580 os.dup2(fd, fp.fileno())
except OSError as err:
# According to issue6330, running chg on heavy loaded systems
# can lead to EBUSY. [man dup2] indicates that, on Linux,
# EBUSY comes from a race condition between open() and dup2().
# However it's not clear why open() race occurred for
# newfd=stdin/out/err.
self.ui.log(
b'chgserver',
b'got %s while duplicating %s\n',
stringutil.forcebytestr(err),
fn,
)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 os.close(fd)
setattr(self, cn, ch)
setattr(ui, fn, fp)
Yuya Nishihara
chgserver: discard buffered output before restoring fds (issue6207)...
r45745 os.close(nullfd)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 del self._oldios[:]
def validate(self):
"""Reload the config and check if the server is up to date
Read a list of '\0' separated arguments.
Write a non-empty list of '\0' separated instruction strings or '\0'
if the list is empty.
An instruction string could be either:
- "unlink $path", the client should unlink the path to stop the
outdated server.
- "redirect $path", the client should attempt to connect to $path
first. If it does not work, start a new server. It implies
"reconnect".
- "exit $n", the client should exit directly with code n.
This may happen if we cannot parse the config.
- "reconnect", the client should close the connection and
reconnect.
If neither "reconnect" nor "redirect" is included in the instruction
list, the client can continue with this server after completing all
the instructions.
"""
args = self._readlist()
Pulkit Goyal
chgserver: catch RepoError while loading configuration...
r46616 errorraised = False
Martin von Zweigbergk
errors: respect ui.detailed-exit-code in chg...
r46759 detailed_exit_code = 255
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 try:
Yuya Nishihara
commandserver: install logger to record server events through canonical API...
r40859 self.ui, lui = _loadnewui(self.ui, args, self.cdebug)
Pulkit Goyal
chgserver: catch RepoError while loading configuration...
r46616 except error.RepoError as inst:
# RepoError can be raised while trying to read shared source
# configuration
self.ui.error(_(b"abort: %s\n") % stringutil.forcebytestr(inst))
if inst.hint:
self.ui.error(_(b"(%s)\n") % inst.hint)
errorraised = True
Yuya Nishihara
chgserver: catch Abort while parsing early args to shut down cleanly...
r40146 except error.Abort as inst:
Martin von Zweigbergk
errors: respect ui.detailed-exit-code in chg...
r46759 if isinstance(inst, error.InputError):
detailed_exit_code = 10
Pulkit Goyal
chgserver: respect detailed exit code in case of ConfigError...
r46761 elif isinstance(inst, error.ConfigError):
detailed_exit_code = 30
Martin von Zweigbergk
errors: format "abort: " text in a new Abort.format() method...
r46497 self.ui.error(inst.format())
Pulkit Goyal
chgserver: catch RepoError while loading configuration...
r46616 errorraised = True
if errorraised:
Yuya Nishihara
chgserver: catch Abort while parsing early args to shut down cleanly...
r40146 self.ui.flush()
Martin von Zweigbergk
errors: respect ui.detailed-exit-code in chg...
r46759 exit_code = 255
if self.ui.configbool(b'ui', b'detailed-exit-code'):
exit_code = detailed_exit_code
self.cresult.write(b'exit %d' % exit_code)
Yuya Nishihara
chgserver: catch Abort while parsing early args to shut down cleanly...
r40146 return
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 newhash = hashstate.fromui(lui, self.hashstate.mtimepaths)
insts = []
if newhash.mtimehash != self.hashstate.mtimehash:
addr = _hashaddress(self.baseaddress, self.hashstate.confighash)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 insts.append(b'unlink %s' % addr)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 # mtimehash is empty if one or more extensions fail to load.
# to be compatible with hg, still serve the client this time.
if self.hashstate.mtimehash:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 insts.append(b'reconnect')
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 if newhash.confighash != self.hashstate.confighash:
addr = _hashaddress(self.baseaddress, newhash.confighash)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 insts.append(b'redirect %s' % addr)
self.ui.log(b'chgserver', b'validate: %s\n', stringutil.pprint(insts))
self.cresult.write(b'\0'.join(insts) or b'\0')
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
def chdir(self):
"""Change current directory
Note that the behavior of --cwd option is bit different from this.
It does not affect --config parameter.
"""
path = self._readstr()
if not path:
return
Martin von Zweigbergk
py3: replace "%r" by"'%s'% for py3-compatible (and clearer) quoting in chg...
r44020 self.ui.log(b'chgserver', b"chdir to '%s'\n", path)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 os.chdir(path)
def setumask(self):
Yuya Nishihara
chgserver: add "setumask2" command which uses correct message frame...
r40144 """Change umask (DEPRECATED)"""
# BUG: this does not follow the message frame structure, but kept for
# backward compatibility with old chg clients for some time
self._setumask(self._read(4))
def setumask2(self):
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 """Change umask"""
Yuya Nishihara
chgserver: add "setumask2" command which uses correct message frame...
r40144 data = self._readstr()
if len(data) != 4:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise ValueError(b'invalid mask length in setumask2 request')
Yuya Nishihara
chgserver: add "setumask2" command which uses correct message frame...
r40144 self._setumask(data)
def _setumask(self, data):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 mask = struct.unpack(b'>I', data)[0]
self.ui.log(b'chgserver', b'setumask %r\n', mask)
Pulkit Goyal
chgserver: update the umask cache before each run...
r45119 util.setumask(mask)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
Jun Wu
chgserver: override runcommand...
r30644 def runcommand(self):
Yuya Nishihara
chgserver: restore pager fds attached within runcommand session...
r39775 # pager may be attached within the runcommand session, which should
# be detached at the end of the session. otherwise the pager wouldn't
# receive EOF.
globaloldios = self._oldios
self._oldios = []
try:
return super(chgcmdserver, self).runcommand()
finally:
self._restoreio()
self._oldios = globaloldios
Jun Wu
chgserver: override runcommand...
r30644
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 def setenv(self):
"""Clear and update os.environ
Note that not all variables can make an effect on the running process.
"""
l = self._readlist()
try:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 newenv = dict(s.split(b'=', 1) for s in l)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 except ValueError:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise ValueError(b'unexpected value in setenv request')
self.ui.log(b'chgserver', b'setenv: %r\n', sorted(newenv.keys()))
Kyle Lippincott
chg: fix chg to work with py3.7+ "coercing" the locale...
r44312
Pulkit Goyal
py3: replace os.environ with encoding.environ (part 2 of 5)
r30635 encoding.environ.clear()
encoding.environ.update(newenv)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
capabilities = commandserver.server.capabilities.copy()
Augie Fackler
formatting: blacken the codebase...
r43346 capabilities.update(
{
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'attachio': attachio,
b'chdir': chdir,
b'runcommand': runcommand,
b'setenv': setenv,
b'setumask': setumask,
b'setumask2': setumask2,
Augie Fackler
formatting: blacken the codebase...
r43346 }
)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if util.safehasattr(procutil, b'setprocname'):
Augie Fackler
formatting: blacken the codebase...
r43346
Jun Wu
chgserver: add the setprocname interface...
r30750 def setprocname(self):
"""Change process title"""
name = self._readstr()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 self.ui.log(b'chgserver', b'setprocname: %r\n', name)
Yuya Nishihara
procutil: bulk-replace function calls to point to new module
r37138 procutil.setprocname(name)
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 capabilities[b'setprocname'] = setprocname
Jun Wu
chgserver: add the setprocname interface...
r30750
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 def _tempaddress(address):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return b'%s.%d.tmp' % (address, os.getpid())
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 def _hashaddress(address, hashstr):
Jun Wu
chgserver: truncate base address at "." for hash address...
r30619 # if the basename of address contains '.', use only the left part. this
# makes it possible for the client to pass 'server.tmp$PID' and follow by
# an atomic rename to avoid locking when spawning new servers.
dirname, basename = os.path.split(address)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 basename = basename.split(b'.', 1)[0]
return b'%s-%s' % (os.path.join(dirname, basename), hashstr)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 class chgunixservicehandler(object):
"""Set of operations for chg services"""
pollinterval = 1 # [sec]
def __init__(self, ui):
self.ui = ui
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 self._idletimeout = ui.configint(b'chgserver', b'idletimeout')
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 self._lastactive = time.time()
def bindsocket(self, sock, address):
self._inithashstate(address)
self._checkextensions()
self._bind(sock)
self._createsymlink()
Jun Wu
commandserver: move printbanner logic to bindsocket...
r32233 # no "listening at" message should be printed to simulate hg behavior
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
def _inithashstate(self, address):
self._baseaddress = address
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if self.ui.configbool(b'chgserver', b'skiphash'):
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 self._hashstate = None
self._realaddress = address
return
self._hashstate = hashstate.fromui(self.ui)
self._realaddress = _hashaddress(address, self._hashstate.confighash)
def _checkextensions(self):
if not self._hashstate:
return
if extensions.notloaded():
# one or more extensions failed to load. mtimehash becomes
# meaningless because we do not know the paths of those extensions.
# set mtimehash to an illegal hash value to invalidate the server.
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 self._hashstate.mtimehash = b''
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
def _bind(self, sock):
# use a unique temp address so we can stat the file and do ownership
# check later
tempaddress = _tempaddress(self._realaddress)
util.bindunixsocket(sock, tempaddress)
self._socketstat = os.stat(tempaddress)
Jun Wu
commandserver: move "listen" responsibility from service to handler...
r32232 sock.listen(socket.SOMAXCONN)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 # rename will replace the old socket file if exists atomically. the
# old server will detect ownership change and exit.
util.rename(tempaddress, self._realaddress)
def _createsymlink(self):
if self._baseaddress == self._realaddress:
return
tempaddress = _tempaddress(self._baseaddress)
os.symlink(os.path.basename(self._realaddress), tempaddress)
util.rename(tempaddress, self._baseaddress)
def _issocketowner(self):
try:
Augie Fackler
cleanup: use stat_result[stat.ST_MTIME] instead of stat_result.st_mtime...
r36799 st = os.stat(self._realaddress)
Augie Fackler
formatting: blacken the codebase...
r43346 return (
st.st_ino == self._socketstat.st_ino
and st[stat.ST_MTIME] == self._socketstat[stat.ST_MTIME]
)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 except OSError:
return False
def unlinksocket(self, address):
if not self._issocketowner():
return
# it is possible to have a race condition here that we may
# remove another server's socket file. but that's okay
# since that server will detect and exit automatically and
# the client will start a new server on demand.
Ryan McElroy
chgserver: use tryunlink
r31545 util.tryunlink(self._realaddress)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
def shouldexit(self):
if not self._issocketowner():
Augie Fackler
formatting: blacken the codebase...
r43346 self.ui.log(
b'chgserver', b'%s is not owned, exiting.\n', self._realaddress
)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 return True
if time.time() - self._lastactive > self._idletimeout:
Yuya Nishihara
commandserver: turn server debug messages into logs...
r40863 self.ui.log(b'chgserver', b'being idle too long. exiting.\n')
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 return True
return False
def newconnection(self):
self._lastactive = time.time()
Yuya Nishihara
commandserver: pass around option to hook repo instance creation...
r40911 def createcmdserver(self, repo, conn, fin, fout, prereposetups):
Augie Fackler
formatting: blacken the codebase...
r43346 return chgcmdserver(
self.ui,
repo,
fin,
fout,
conn,
prereposetups,
self._hashstate,
self._baseaddress,
)
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
def chgunixservice(ui, repo, opts):
Jun Wu
demandimport: disable if chg is being used...
r33862 # CHGINTERNALMARK is set by chg client. It is an indication of things are
# started by chg so other code can do things accordingly, like disabling
# demandimport or detecting chg client started by chg client. When executed
# here, CHGINTERNALMARK is no longer useful and hence dropped to make
# environ cleaner.
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b'CHGINTERNALMARK' in encoding.environ:
del encoding.environ[b'CHGINTERNALMARK']
Kyle Lippincott
chg: force-set LC_CTYPE on server start to actual value from the environment...
r44733 # Python3.7+ "coerces" the LC_CTYPE environment variable to a UTF-8 one if
# it thinks the current value is "C". This breaks the hash computation and
# causes chg to restart loop.
if b'CHGORIG_LC_CTYPE' in encoding.environ:
encoding.environ[b'LC_CTYPE'] = encoding.environ[b'CHGORIG_LC_CTYPE']
del encoding.environ[b'CHGORIG_LC_CTYPE']
elif b'CHG_CLEAR_LC_CTYPE' in encoding.environ:
if b'LC_CTYPE' in encoding.environ:
del encoding.environ[b'LC_CTYPE']
del encoding.environ[b'CHG_CLEAR_LC_CTYPE']
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513
if repo:
# one chgserver can serve multiple repos. drop repo information
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ui.setconfig(b'bundle', b'mainreporoot', b'', b'repo')
Yuya Nishihara
chgserver: make it a core module and drop extension flags...
r30513 h = chgunixservicehandler(ui)
return commandserver.unixforkingservice(ui, repo=None, opts=opts, handler=h)