sapling/hgext
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
..
convert bookmark: use 'applychanges' in the convert extension 2017-07-10 17:30:20 +02:00
fsmonitor fsmonitor: execute setup procedures only if dirstate is already instantiated 2017-07-10 23:09:52 +09:00
highlight highlight: put pygments import inside demandimport.deactivated 2017-06-18 23:05:54 -04:00
largefiles codemod: register core configitems using a script 2017-07-14 14:22:40 -07:00
zeroconf zeroconf: blindly forward extra argument to the core config method 2017-07-01 21:57:17 +02:00
__init__.py hgext: officially turn 'hgext' into a namespace package 2016-02-27 12:56:26 +01:00
acl.py configitem: create a new list of each 'acl.sources' access 2017-07-02 23:10:33 +02:00
amend.py amend: new extension providing the amend command 2017-07-11 20:53:55 -07:00
automv.py configitems: register the 'automv.similarity' config 2017-06-30 03:27:24 +02:00
blackbox.py repovfs: add a ward to check if locks are properly taken 2017-07-11 12:38:17 +02:00
bugzilla.py bugzilla: move the default regexp for fix in the config declaration 2017-07-14 16:17:37 +02:00
censor.py revlog: rename constants (API) 2017-05-17 19:52:18 -07:00
children.py commands: move templates of common command options to cmdutil (API) 2017-05-14 16:19:47 +09:00
churn.py churn: use the non-deprecated template option in the examples 2017-05-08 23:05:01 -04:00
clonebundles.py clonebundles: fix missing newline character 2017-05-24 22:59:59 -04:00
color.py color: fix grammar in help text 2017-03-25 13:29:23 -04:00
eol.py eol: import 'error' as 'errormod' 2017-06-23 13:24:45 +02:00
extdiff.py cmdutil: rename template param to export to fntemplate 2017-05-20 20:15:05 -04:00
factotum.py configitems: register the 'factotum.service' config 2017-06-30 03:42:15 +02:00
fetch.py fetch: remove shorthand of --edit colliding against -e/-ssh in remoteopts (BC) 2017-06-24 02:39:13 +09:00
gpg.py commands: move templates of common command options to cmdutil (API) 2017-05-14 16:19:47 +09:00
graphlog.py commands: move templates of common command options to cmdutil (API) 2017-05-14 16:19:47 +09:00
hgk.py hgk: don't use mutable default argument value 2017-03-14 23:48:25 -07:00
histedit.py bookmark: use 'applychanges' when updating bookmark in histedit 2017-07-10 17:28:53 +02:00
journal.py codemod: register core configitems using a script 2017-07-14 14:22:40 -07:00
keyword.py keyword: wrap functions only once at loading keyword extension 2017-06-26 03:47:11 +09:00
logtoprocess.py logtoprocess: use lowercase for docstring title 2017-03-23 21:16:55 -07:00
mq.py bookmark: use 'applychanges' in the mq extension 2017-07-10 17:44:25 +02:00
notify.py cmdutil: pass templatespec tuple directly to changeset_templater (API) 2017-04-22 19:02:47 +09:00
pager.py pager: if old pager extensions is enabled, respect pager.attend 2017-03-13 21:43:17 -07:00
patchbomb.py repovfs: add a ward to check if locks are properly taken 2017-07-11 12:38:17 +02:00
purge.py commands: move templates of common command options to cmdutil (API) 2017-05-14 16:19:47 +09:00
rebase.py rebase: remove "if True" 2017-07-07 19:03:03 -07:00
record.py commands: move templates of common command options to cmdutil (API) 2017-05-14 16:19:47 +09:00
releasenotes.py releasenotes: improve parsing around bullet points 2017-06-23 17:15:53 +02:00
relink.py extensions: change magic "shipped with hg" string 2016-08-23 11:26:08 -04:00
schemes.py schemes: use br'' literal to define bytes regexp 2017-03-09 19:41:40 -08:00
share.py share: use dict literal instead of dict(key=value) 2017-06-24 13:20:30 +09:00
shelve.py dirstate: update backup functions to take full backup filename 2017-07-12 15:24:07 -07:00
show.py show: document why accidentally quadratic is (probably) acceptable 2017-07-03 21:26:39 -07:00
sparse.py sparse: override dirstate.walk() instead of dirstate._ignore 2017-07-11 10:46:35 -07:00
strip.py bookmark: use 'applychanges' when stripping 2017-07-10 17:37:48 +02:00
transplant.py transplant: directly use repo.vfs.join 2017-03-08 16:52:49 -08:00
win32mbcs.py win32mbcs: avoid unintentional failure at colorization 2017-05-31 23:44:33 +09:00
win32text.py win32text: directly use repo.vfs.join 2017-03-08 16:52:57 -08:00