tests/check-perf-code.py
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Sun, 09 Oct 2016 01:03:19 +0900
changeset 30149 d8a2c536dd96
parent 30146 148ccd1d9f2f
child 32291 bd872f64a8ba
permissions -rwxr-xr-x
perf: replace ui.configint() by getint() for Mercurial earlier than 1.9 Before this patch, using ui.configint() prevents perf.py from measuring performance with Mercurial earlier than 1.9 (or fa2b596db182), because ui.configint() isn't available in such Mercurial, even though there are some code paths for Mercurial earlier than 1.9 in perf.py. For example, setting "_prereadsize" attribute in perfindex() and perfnodelookup() is effective only with hg earlier than 1.8 (or 61c9bc3da402). This patch replaces ui.configint() invocations by newly introduced getint(). This patch also adds check-perf-code.py an extra check entry to detect direct usage of ui.configint() in perf.py. BTW, this patch doesn't choose adding configint() method at runtime by replacing ui.__class__ like below, even though this is the recommended way to modern Mercurial extensions. def uisetup(ui): if not util.safehasattr(ui, 'configint'): class uiwrap(ui.__class__): def configint(self, section, name, ....): .... ui.__class__ = uiwrap Because changes to ui.__class__ by uisetup() of loaded extension have been propagated since 1.6.1 (or d8d0fc3988ca), the recommended way above doesn't work as expected with Mercurial earlier than it.

#!/usr/bin/env python
#
# check-perf-code - (historical) portability checker for contrib/perf.py

from __future__ import absolute_import

import os
import sys

# write static check patterns here
perfpypats = [
  [
    (r'(branchmap|repoview)\.subsettable',
     "use getbranchmapsubsettable() for early Mercurial"),
    (r'\.(vfs|svfs|opener|sopener)',
     "use getvfs()/getsvfs() for early Mercurial"),
    (r'ui\.configint',
     "use getint() instead of ui.configint() for early Mercurial"),
  ],
  # warnings
  [
  ]
]

def modulewhitelist(names):
    replacement = [('.py', ''), ('.c', ''), # trim suffix
                   ('mercurial%s' % (os.sep), ''), # trim "mercurial/" path
                  ]
    ignored = set(['__init__'])
    modules = {}

    # convert from file name to module name, and count # of appearances
    for name in names:
        name = name.strip()
        for old, new in replacement:
            name = name.replace(old, new)
        if name not in ignored:
            modules[name] = modules.get(name, 0) + 1

    # list up module names, which appear multiple times
    whitelist = []
    for name, count in modules.items():
        if count > 1:
            whitelist.append(name)

    return whitelist

if __name__ == "__main__":
    # in this case, it is assumed that result of "hg files" at
    # multiple revisions is given via stdin
    whitelist = modulewhitelist(sys.stdin)
    assert whitelist, "module whitelist is empty"

    # build up module whitelist check from file names given at runtime
    perfpypats[0].append(
        # this matching pattern assumes importing modules from
        # "mercurial" package in the current style below, for simplicity
        #
        #    from mercurial import (
        #        foo,
        #        bar,
        #        baz
        #    )
        ((r'from mercurial import [(][a-z0-9, \n#]*\n(?! *%s,|^[ #]*\n|[)])'
          % ',| *'.join(whitelist)),
         "import newer module separately in try clause for early Mercurial"
         ))

    # import contrib/check-code.py as checkcode
    assert 'RUNTESTDIR' in os.environ, "use check-perf-code.py in *.t script"
    contribpath = os.path.join(os.environ['RUNTESTDIR'], '..', 'contrib')
    sys.path.insert(0, contribpath)
    checkcode = __import__('check-code')

    # register perf.py specific entry with "checks" in check-code.py
    checkcode.checks.append(('perf.py', r'contrib/perf.py$', '',
                             checkcode.pyfilters, perfpypats))

    sys.exit(checkcode.main())