diff -r 2372284d9457 -r 687b865b95ad mercurial/chgserver.py --- a/mercurial/chgserver.py Sun Oct 06 09:45:02 2019 -0400 +++ b/mercurial/chgserver.py Sun Oct 06 09:48:39 2019 -0400 @@ -75,14 +75,14 @@ # sensitive config sections affecting confighash _configsections = [ - 'alias', # affects global state commands.table - 'eol', # uses setconfig('eol', ...) - 'extdiff', # uisetup will register new commands - 'extensions', + b'alias', # affects global state commands.table + b'eol', # uses setconfig('eol', ...) + b'extdiff', # uisetup will register new commands + b'extensions', ] _configsectionitems = [ - ('commands', 'show.aliasprefix'), # show.py reads it in extsetup + (b'commands', b'show.aliasprefix'), # show.py reads it in extsetup ] # sensitive environment variables affecting confighash @@ -121,8 +121,8 @@ sectionitems.append(ui.config(section, item)) sectionhash = _hashlist(sectionitems) # If $CHGHG is set, the change to $HG should not trigger a new chg server - if 'CHGHG' in encoding.environ: - ignored = {'HG'} + if b'CHGHG' in encoding.environ: + ignored = {b'HG'} else: ignored = set() envitems = [ @@ -204,8 +204,8 @@ confighash = _confighash(ui) mtimehash = _mtimehash(mtimepaths) ui.log( - 'cmdserver', - 'confighash = %s mtimehash = %s\n', + b'cmdserver', + b'confighash = %s mtimehash = %s\n', confighash, mtimehash, ) @@ -229,7 +229,7 @@ # will behave differently (i.e. write to stdout). if ( out is not self.fout - or not util.safehasattr(self.fout, 'fileno') + or not util.safehasattr(self.fout, b'fileno') or self.fout.fileno() != procutil.stdout.fileno() or self._finoutredirected ): @@ -241,8 +241,8 @@ self._csystem( cmd, procutil.shellenviron(env), - type='pager', - cmdtable={'attachio': attachio}, + type=b'pager', + cmdtable={b'attachio': attachio}, ) return True @@ -253,27 +253,27 @@ from . import dispatch # avoid cycle newui = srcui.__class__.load() - for a in ['fin', 'fout', 'ferr', 'environ']: + for a in [b'fin', b'fout', b'ferr', b'environ']: setattr(newui, a, getattr(srcui, a)) - if util.safehasattr(srcui, '_csystem'): + if util.safehasattr(srcui, b'_csystem'): newui._csystem = srcui._csystem # command line args options = dispatch._earlyparseopts(newui, args) - dispatch._parseconfig(newui, options['config']) + dispatch._parseconfig(newui, options[b'config']) # stolen from tortoisehg.util.copydynamicconfig() for section, name, value in srcui.walkconfig(): source = srcui.configsource(section, name) - if ':' in source or source == '--config' or source.startswith('$'): + if b':' in source or source == b'--config' or source.startswith(b'$'): # path:line or command line, or environ continue newui.setconfig(section, name, value, source) # load wd and repo config, copied from dispatch.py - cwd = options['cwd'] + cwd = options[b'cwd'] cwd = cwd and os.path.realpath(cwd) or None - rpath = options['repository'] + rpath = options[b'repository'] path, newlui = dispatch._getlocal(newui, rpath, wd=cwd) extensions.populateui(newui) @@ -311,22 +311,22 @@ self.out = out self.channel = channel - def __call__(self, cmd, environ, cwd=None, type='system', cmdtable=None): - args = [type, procutil.quotecommand(cmd), os.path.abspath(cwd or '.')] - args.extend('%s=%s' % (k, v) for k, v in environ.iteritems()) - data = '\0'.join(args) - self.out.write(struct.pack('>cI', self.channel, len(data))) + def __call__(self, cmd, environ, cwd=None, type=b'system', cmdtable=None): + args = [type, procutil.quotecommand(cmd), os.path.abspath(cwd or b'.')] + args.extend(b'%s=%s' % (k, v) for k, v in environ.iteritems()) + data = b'\0'.join(args) + self.out.write(struct.pack(b'>cI', self.channel, len(data))) self.out.write(data) self.out.flush() - if type == 'system': + if type == b'system': length = self.in_.read(4) - (length,) = struct.unpack('>I', length) + (length,) = struct.unpack(b'>I', length) if length != 4: - raise error.Abort(_('invalid response')) - (rc,) = struct.unpack('>i', self.in_.read(4)) + raise error.Abort(_(b'invalid response')) + (rc,) = struct.unpack(b'>i', self.in_.read(4)) return rc - elif type == 'pager': + elif type == b'pager': while True: cmd = self.in_.readline()[:-1] if not cmd: @@ -334,16 +334,16 @@ if cmdtable and cmd in cmdtable: cmdtable[cmd]() else: - raise error.Abort(_('unexpected command: %s') % cmd) + raise error.Abort(_(b'unexpected command: %s') % cmd) else: - raise error.ProgrammingError('invalid S channel type: %s' % type) + raise error.ProgrammingError(b'invalid S channel type: %s' % type) _iochannels = [ # server.ch, ui.fp, mode - ('cin', 'fin', r'rb'), - ('cout', 'fout', r'wb'), - ('cerr', 'ferr', r'wb'), + (b'cin', b'fin', r'rb'), + (b'cout', b'fout', r'wb'), + (b'cerr', b'ferr', r'wb'), ] @@ -352,7 +352,7 @@ self, ui, repo, fin, fout, sock, prereposetups, hashstate, baseaddress ): super(chgcmdserver, self).__init__( - _newchgui(ui, channeledsystem(fin, fout, 'S'), self.attachio), + _newchgui(ui, channeledsystem(fin, fout, b'S'), self.attachio), repo, fin, fout, @@ -365,7 +365,7 @@ self.baseaddress = baseaddress if hashstate is not None: self.capabilities = self.capabilities.copy() - self.capabilities['validate'] = chgcmdserver.validate + self.capabilities[b'validate'] = chgcmdserver.validate def cleanup(self): super(chgcmdserver, self).cleanup() @@ -381,9 +381,9 @@ """ # tell client to sendmsg() with 1-byte payload, which makes it # distinctive from "attachio\n" command consumed by client.read() - self.clientsock.sendall(struct.pack('>cI', 'I', 1)) + self.clientsock.sendall(struct.pack(b'>cI', b'I', 1)) clientfds = util.recvfds(self.clientsock.fileno()) - self.ui.log('chgserver', 'received fds: %r\n', clientfds) + self.ui.log(b'chgserver', b'received fds: %r\n', clientfds) ui = self.ui ui.flush() @@ -399,7 +399,7 @@ # 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. - if fn == 'ferr': + if fn == b'ferr': newfp = fp else: # make it line buffered explicitly because the default is @@ -413,7 +413,7 @@ setattr(self, cn, newfp) self._ioattached = True - self.cresult.write(struct.pack('>i', len(clientfds))) + self.cresult.write(struct.pack(b'>i', len(clientfds))) def _saveio(self): if self._oldios: @@ -468,29 +468,29 @@ except error.ParseError as inst: dispatch._formatparse(self.ui.warn, inst) self.ui.flush() - self.cresult.write('exit 255') + self.cresult.write(b'exit 255') return except error.Abort as inst: - self.ui.error(_("abort: %s\n") % inst) + self.ui.error(_(b"abort: %s\n") % inst) if inst.hint: - self.ui.error(_("(%s)\n") % inst.hint) + self.ui.error(_(b"(%s)\n") % inst.hint) self.ui.flush() - self.cresult.write('exit 255') + self.cresult.write(b'exit 255') return newhash = hashstate.fromui(lui, self.hashstate.mtimepaths) insts = [] if newhash.mtimehash != self.hashstate.mtimehash: addr = _hashaddress(self.baseaddress, self.hashstate.confighash) - insts.append('unlink %s' % addr) + insts.append(b'unlink %s' % addr) # 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: - insts.append('reconnect') + insts.append(b'reconnect') if newhash.confighash != self.hashstate.confighash: addr = _hashaddress(self.baseaddress, newhash.confighash) - insts.append('redirect %s' % addr) - self.ui.log('chgserver', 'validate: %s\n', stringutil.pprint(insts)) - self.cresult.write('\0'.join(insts) or '\0') + 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') def chdir(self): """Change current directory @@ -501,7 +501,7 @@ path = self._readstr() if not path: return - self.ui.log('chgserver', 'chdir to %r\n', path) + self.ui.log(b'chgserver', b'chdir to %r\n', path) os.chdir(path) def setumask(self): @@ -514,12 +514,12 @@ """Change umask""" data = self._readstr() if len(data) != 4: - raise ValueError('invalid mask length in setumask2 request') + raise ValueError(b'invalid mask length in setumask2 request') self._setumask(data) def _setumask(self, data): - mask = struct.unpack('>I', data)[0] - self.ui.log('chgserver', 'setumask %r\n', mask) + mask = struct.unpack(b'>I', data)[0] + self.ui.log(b'chgserver', b'setumask %r\n', mask) os.umask(mask) def runcommand(self): @@ -541,38 +541,38 @@ """ l = self._readlist() try: - newenv = dict(s.split('=', 1) for s in l) + newenv = dict(s.split(b'=', 1) for s in l) except ValueError: - raise ValueError('unexpected value in setenv request') - self.ui.log('chgserver', 'setenv: %r\n', sorted(newenv.keys())) + raise ValueError(b'unexpected value in setenv request') + self.ui.log(b'chgserver', b'setenv: %r\n', sorted(newenv.keys())) encoding.environ.clear() encoding.environ.update(newenv) capabilities = commandserver.server.capabilities.copy() capabilities.update( { - 'attachio': attachio, - 'chdir': chdir, - 'runcommand': runcommand, - 'setenv': setenv, - 'setumask': setumask, - 'setumask2': setumask2, + b'attachio': attachio, + b'chdir': chdir, + b'runcommand': runcommand, + b'setenv': setenv, + b'setumask': setumask, + b'setumask2': setumask2, } ) - if util.safehasattr(procutil, 'setprocname'): + if util.safehasattr(procutil, b'setprocname'): def setprocname(self): """Change process title""" name = self._readstr() - self.ui.log('chgserver', 'setprocname: %r\n', name) + self.ui.log(b'chgserver', b'setprocname: %r\n', name) procutil.setprocname(name) - capabilities['setprocname'] = setprocname + capabilities[b'setprocname'] = setprocname def _tempaddress(address): - return '%s.%d.tmp' % (address, os.getpid()) + return b'%s.%d.tmp' % (address, os.getpid()) def _hashaddress(address, hashstr): @@ -580,8 +580,8 @@ # 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) - basename = basename.split('.', 1)[0] - return '%s-%s' % (os.path.join(dirname, basename), hashstr) + basename = basename.split(b'.', 1)[0] + return b'%s-%s' % (os.path.join(dirname, basename), hashstr) class chgunixservicehandler(object): @@ -591,7 +591,7 @@ def __init__(self, ui): self.ui = ui - self._idletimeout = ui.configint('chgserver', 'idletimeout') + self._idletimeout = ui.configint(b'chgserver', b'idletimeout') self._lastactive = time.time() def bindsocket(self, sock, address): @@ -603,7 +603,7 @@ def _inithashstate(self, address): self._baseaddress = address - if self.ui.configbool('chgserver', 'skiphash'): + if self.ui.configbool(b'chgserver', b'skiphash'): self._hashstate = None self._realaddress = address return @@ -617,7 +617,7 @@ # 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. - self._hashstate.mtimehash = '' + self._hashstate.mtimehash = b'' def _bind(self, sock): # use a unique temp address so we can stat the file and do ownership @@ -689,11 +689,11 @@ # demandimport or detecting chg client started by chg client. When executed # here, CHGINTERNALMARK is no longer useful and hence dropped to make # environ cleaner. - if 'CHGINTERNALMARK' in encoding.environ: - del encoding.environ['CHGINTERNALMARK'] + if b'CHGINTERNALMARK' in encoding.environ: + del encoding.environ[b'CHGINTERNALMARK'] if repo: # one chgserver can serve multiple repos. drop repo information - ui.setconfig('bundle', 'mainreporoot', '', 'repo') + ui.setconfig(b'bundle', b'mainreporoot', b'', b'repo') h = chgunixservicehandler(ui) return commandserver.unixforkingservice(ui, repo=None, opts=opts, handler=h)