Commit Graph

30 Commits

Author SHA1 Message Date
Jun Wu
e47f7dc2fa codemod: register core configitems using a script
This is done by a script [2] using RedBaron [1], a tool designed for doing
code refactoring. All "default" values are decided by the script and are
strongly consistent with the existing code.

There are 2 changes done manually to fix tests:

  [warn] mercurial/exchange.py: experimental.bundle2-output-capture: default needs manual removal
  [warn] mercurial/localrepo.py: experimental.hook-track-tags: default needs manual removal

Since RedBaron is not confident about how to indent things [2].

[1]: https://github.com/PyCQA/redbaron
[2]: https://github.com/PyCQA/redbaron/issues/100
[3]:

#!/usr/bin/env python
# codemod_configitems.py - codemod tool to fill configitems
#
# Copyright 2017 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import, print_function

import os
import sys

import redbaron

def readpath(path):
    with open(path) as f:
        return f.read()

def writepath(path, content):
    with open(path, 'w') as f:
        f.write(content)

_configmethods = {'config', 'configbool', 'configint', 'configbytes',
                  'configlist', 'configdate'}

def extractstring(rnode):
    """get the string from a RedBaron string or call_argument node"""
    while rnode.type != 'string':
        rnode = rnode.value
    return rnode.value[1:-1]  # unquote, "'str'" -> "str"

def uiconfigitems(red):
    """match *.ui.config* pattern, yield (node, method, args, section, name)"""
    for node in red.find_all('atomtrailers'):
        entry = None
        try:
            obj = node[-3].value
            method = node[-2].value
            args = node[-1]
            section = args[0].value
            name = args[1].value
            if (obj in ('ui', 'self') and method in _configmethods
                and section.type == 'string' and name.type == 'string'):
                entry = (node, method, args, extractstring(section),
                         extractstring(name))
        except Exception:
            pass
        else:
            if entry:
                yield entry

def coreconfigitems(red):
    """match coreconfigitem(...) pattern, yield (node, args, section, name)"""
    for node in red.find_all('atomtrailers'):
        entry = None
        try:
            args = node[1]
            section = args[0].value
            name = args[1].value
            if (node[0].value == 'coreconfigitem' and section.type == 'string'
                and name.type == 'string'):
                entry = (node, args, extractstring(section),
                         extractstring(name))
        except Exception:
            pass
        else:
            if entry:
                yield entry

def registercoreconfig(cfgred, section, name, defaultrepr):
    """insert coreconfigitem to cfgred AST

    section and name are plain string, defaultrepr is a string
    """
    # find a place to insert the "coreconfigitem" item
    entries = list(coreconfigitems(cfgred))
    for node, args, nodesection, nodename in reversed(entries):
        if (nodesection, nodename) < (section, name):
            # insert after this entry
            node.insert_after(
                'coreconfigitem(%r, %r,\n'
                '    default=%s,\n'
                ')' % (section, name, defaultrepr))
            return

def main(argv):
    if not argv:
        print('Usage: codemod_configitems.py FILES\n'
              'For example, FILES could be "{hgext,mercurial}/*/**.py"')
    dirname = os.path.dirname
    reporoot = dirname(dirname(dirname(os.path.abspath(__file__))))

    # register configitems to this destination
    cfgpath = os.path.join(reporoot, 'mercurial', 'configitems.py')
    cfgred = redbaron.RedBaron(readpath(cfgpath))

    # state about what to do
    registered = set((s, n) for n, a, s, n in coreconfigitems(cfgred))
    toregister = {} # {(section, name): defaultrepr}
    coreconfigs = set() # {(section, name)}, whether it's used in core

    # first loop: scan all files before taking any action
    for i, path in enumerate(argv):
        print('(%d/%d) scanning %s' % (i + 1, len(argv), path))
        iscore = ('mercurial' in path) and ('hgext' not in path)
        red = redbaron.RedBaron(readpath(path))
        # find all repo.ui.config* and ui.config* calls, and collect their
        # section, name and default value information.
        for node, method, args, section, name in uiconfigitems(red):
            if section == 'web':
                # [web] section has some weirdness, ignore them for now
                continue
            defaultrepr = None
            key = (section, name)
            if len(args) == 2:
                if key in registered:
                    continue
                if method == 'configlist':
                    defaultrepr = 'list'
                elif method == 'configbool':
                    defaultrepr = 'False'
                else:
                    defaultrepr = 'None'
            elif len(args) >= 3 and (args[2].target is None or
                                     args[2].target.value == 'default'):
                # try to understand the "default" value
                dnode = args[2].value
                if dnode.type == 'name':
                    if dnode.value in {'None', 'True', 'False'}:
                        defaultrepr = dnode.value
                elif dnode.type == 'string':
                    defaultrepr = repr(dnode.value[1:-1])
                elif dnode.type in ('int', 'float'):
                    defaultrepr = dnode.value
            # inconsistent default
            if key in toregister and toregister[key] != defaultrepr:
                defaultrepr = None
            # interesting to rewrite
            if key not in registered:
                if defaultrepr is None:
                    print('[note] %s: %s.%s: unsupported default'
                          % (path, section, name))
                    registered.add(key) # skip checking it again
                else:
                    toregister[key] = defaultrepr
                    if iscore:
                        coreconfigs.add(key)

    # second loop: rewrite files given "toregister" result
    for path in argv:
        # reconstruct redbaron - trade CPU for memory
        red = redbaron.RedBaron(readpath(path))
        changed = False
        for node, method, args, section, name in uiconfigitems(red):
            key = (section, name)
            defaultrepr = toregister.get(key)
            if defaultrepr is None or key not in coreconfigs:
                continue
            if len(args) >= 3 and (args[2].target is None or
                                   args[2].target.value == 'default'):
                try:
                    del args[2]
                    changed = True
                except Exception:
                    # redbaron fails to do the rewrite due to indentation
                    # see https://github.com/PyCQA/redbaron/issues/100
                    print('[warn] %s: %s.%s: default needs manual removal'
                          % (path, section, name))
            if key not in registered:
                print('registering %s.%s' % (section, name))
                registercoreconfig(cfgred, section, name, defaultrepr)
                registered.add(key)
        if changed:
            print('updating %s' % path)
            writepath(path, red.dumps())

    if toregister:
        print('updating configitems.py')
        writepath(cfgpath, cfgred.dumps())

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
2017-07-14 14:22:40 -07:00
Jun Wu
dcb05ed412 chgserver: more explicit about sensitive environ variables
Environment variables like HGUSER, HGEDITOR, HGEDITFROM should not trigger
a new chgserver. This patch uses a whitelist for environ variables starting
with "HG" to reduce the number of servers.

I have went through `grep -o "[\"']HG[A-Z_0-9]*['\"]" -hR . | sort -u` so
the list should be up-to-date.
2017-05-10 11:55:22 -07:00
Jun Wu
15584f0091 commandserver: move printbanner logic to bindsocket
bindsocket now handles listen automatically. "printbanner" seems to be just
a part of "bindsocket". This simplifies the interface a bit.
2017-04-30 11:21:05 -07:00
Jun Wu
d3cc1e50f4 commandserver: move "listen" responsibility from service to handler
This enables chg to replace a server socket in an atomic way:

  1. bind to a temp address
  2. listen
  3. rename

Currently 3 happens before 2 so a client may see the socket file but fails
to connect to it.
2017-04-30 11:08:27 -07:00
Yuya Nishihara
cbe21a1cc9 osutil: proxy through util (and platform) modules (API)
See the previous commit for why. Marked as API change since osutil.listdir()
seems widely used in third-party extensions.

The win32mbcs extension is updated to wrap both util. and windows. aliases.
2017-04-26 22:26:28 +09:00
Jun Wu
dcf42da6e9 pager: set some environment variables if they're not set
Git did this already [1] [2]. We want this behavior too [3].

This provides a better default user experience (like, supporting colors) if
users have things like "PAGER=less" set, which is not uncommon.

The environment variables are provided by a method so extensions can
override them on demand.

[1]: 6a5ff7acb5/pager.c (L87)
[2]: 6a5ff7acb5/Makefile (L1545)
[3]: https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-March/094780.html
2017-04-13 08:27:19 -07:00
Jun Wu
49dddd702d chgserver: do not copy configs set by environment variables
Config set by environment variables have a source like "$ENVNAME". They
should not be copied because they will be recalculated by
rcutil.rccomponents.
2017-03-28 08:40:12 -07:00
Matt Harbison
ca66dceee3 ui: defer setting pager related properties until the pager has spawned
When --pager=on is given, dispatch.py spawns a pager before setting up color.
If the pager failed to launch, ui.pageractive was left set to True, so color
configured itself based on 'color.pagermode'.  A typical MSYS setting would be
'color.mode=auto, color.pagermode=ansi'.  In the failure case, this would print
a warning, disable the pager, and then print the raw ANSI codes to the terminal.

Care needs to be taken, because it appears that leaving ui.pageractive=True was
the only thing that prevented an attempt at running the pager again from inside
the command.  This results in a double warning message, so pager is simply
disabled on failure.

The ui config settings didn't need to be moved to fix this, but it seemed like
the right thing to do for consistency.
2017-03-25 21:12:00 -04:00
Ryan McElroy
d5ac75bcbf chgserver: use tryunlink 2017-03-21 06:50:28 -07:00
Yuya Nishihara
8072a1daee chg: deduplicate error handling of ui.system()
This moves 'onerr' handling from low-level util.system() to higher level,
which seems better API separation.
2017-02-19 01:16:45 +09:00
Yuya Nishihara
9a31b02cc4 chg: refactor ui.system() to be partly overridden
Since a33895509ccb changed the signature of ui.system(), chgui.system()
should have been updated. This patch factors out the util.system() call
so that chg can override how a shell command is executed.
2017-02-19 01:00:10 +09:00
Jun Wu
0cd5475f03 chgserver: move comments in config example
"#" must be the first character of a line to mark the text as comments.
So let's change the docstring.
2017-02-16 23:10:47 -08:00
Pulkit Goyal
07314d0686 py3: convert the mode argument of os.fdopen to unicodes (1 of 2)
os.fdopen() does not accepts bytes as its second argument which represent the
mode in which the file is to be opened. This patch makes sure unicodes are
passed in py3 by using pycompat.sysstr().
2017-02-13 20:06:38 +05:30
Jun Wu
f7a8f527b8 chgserver: add the setprocname interface
This allows clients to change its process title freely.
2017-01-11 07:36:48 +08:00
Jun Wu
b61b02a865 chg: remove getpager support
We have enough bits to switch to the new chg pager code path in runcommand.
So just remove the legacy getpager support.

This is a red-only patch, and will break chg's pager support temporarily.
2017-01-10 06:59:39 +08:00
Jun Wu
2fc8d9fe86 chgserver: implement chgui._runpager
This patch implements chgui._runpager in a relatively simple way. A more
clean way is to move the core logic of "attachio" to "ui", which will be
done later after chg runs uisetup per request.
2017-01-10 06:59:31 +08:00
Jun Wu
ed9bebc440 chgserver: make S channel support pager request
This patch adds the "pager" support for the S channel. The pager API allows
running some subcommands, namely attachio, and waiting for the client to be
properly synchronized.
2017-01-10 06:59:21 +08:00
Jun Wu
7085592213 chgserver: use util.shellenviron
This avoids code duplication.
2017-01-10 06:58:51 +08:00
Jun Wu
56484854f9 chgserver: check type passed to S channel
It currently only supports the "system" type. Add an explicit check.
2017-01-06 16:12:25 +00:00
Jun Wu
734e02b02d chg: send type information via S channel (BC)
Previously S channel is only used to send system commands. It will also be
used to send pager commands. So add a type parameter.

This breaks older chg clients. But chg and hg should always come from a
single commit and be packed into a single package. Supporting running
inconsistent versions of chg and hg seems to be unnecessarily complicated
with little benefit. So just make the change and assume people won't use
inconsistent chg with hg.
2017-01-06 16:11:03 +00:00
Pulkit Goyal
007bf9e678 py3: replace sys.executable with pycompat.sysexecutable
sys.executable returns unicodes on Python 3. This patch replaces occurences of
sys.executable with pycompat.sysexecutable.
2016-12-20 00:20:07 +05:30
Yuya Nishihara
55f132e688 chgserver: backout changeset 89972695d644 (per discussion)
On Wed, 21 Dec 2016 15:39:05 +0000, Jun Wu wrote:
> Actually, patch 1 is unnecessary if we go with the "ui._runpager" approach.
> Maybe someone can drop it without adding too many markers.
2016-12-22 01:09:45 +09:00
Jun Wu
f0bfce6778 chgserver: override runcommand
Next patches will customize chgserver's runcommand. So let's override it.
The docstring is temporarily missing and will be filled later.
2016-12-16 14:48:37 +00:00
Jun Wu
c85c98112c chgserver: store csystem separately
Previously, the "system" channel is inside the ui object. In the future, chg
will let dispatch to create a new ui object from scratch, to maximize
compatibility. And chgserver will use a "uisetup" like an extension to wrap
ui.system. To be able to do that cleanly, the system channel needs to be
accessed directly.
2016-12-16 14:46:34 +00:00
Pulkit Goyal
1a4248666b py3: replace os.environ with encoding.environ (part 2 of 5) 2016-12-18 01:46:39 +05:30
Jun Wu
5508cfe721 chgserver: truncate base address at "." for hash address
Previously, the hash address is just appending "-$HASH" to base address.
This patch makes it truncate the basename address at "." before appending
"-$HASH".

This makes it possible to spawn new servers in a racy situation and the
client could be sure the server it connects is the new server just spawned.

This is a step towards removing the lock.

One of the functionalities of the lock is to make sure the connect will
connect to a server it just created:

  1. start server --address foo
  2. connect to foo # wish "foo" is the server just started

With this change, the client could do:

  1. start server --address foo.tmp$PID
  2. connect to foo.tmp$PID # is the server just started
     (note: if it is not, it does not affect correctness - linux pid
      namespace is not a concern here)
  3. rename foo.tmp$PID to foo

Another functionality of the lock is to avoid starting multiple servers with
a same confighash in parallel. But that also prevents starting multiple
servers with different confighashes in parallel.
2016-12-19 22:07:41 +00:00
Yuya Nishihara
26dd8d740c ui: do not translate empty configsource() to 'none' (API)
It should be processed when displaying data, so we can get "source": "" in
JSON output.
2016-10-23 17:47:00 +09:00
Jun Wu
44c0d5d616 chg: ignore HG_* in confighash
The environment variables `HG_*` are usually used by hooks. Unlike `HGPLAIN`
etc, they do not actually affect hg's behavior. So do not include them in
confighash.

This would avoid spawning an unbound number of chg server processes if
commit hook calls hg frequently.
2016-12-14 02:17:59 +00:00
Jun Wu
5b83a79f69 chgserver: call "load" for new ui objects
After 81ed7b0f8a46, we need to call "ui.load" explicitly to load config
files.
2016-12-05 21:36:35 +00:00
Yuya Nishihara
30fe4722fb chgserver: make it a core module and drop extension flags
It was an extension just because there were several dependency cycles I
needed to address.

I don't add 'chgserver' to extensions._builtin since chgserver is considered
an internal extension so nobody should enable it by their config.
2016-10-15 14:30:16 +09:00