contrib/simplemerge
author Pierre-Yves David <pierre-yves.david@octobus.net>
Thu, 08 Jun 2017 01:38:48 +0100
changeset 32783 4483696dacee
parent 30576 541949a10a68
child 33895 aed91971d88c
permissions -rwxr-xr-x
profile: upgrade the "profile" context manager to a full class So far we have been able to use a simple decorator for this. However using the current context manager makes the scope of the profiling in dispatch constrainted and the time frame to decide to enable profiling quite limited (using "maybeprofile") This is the first step toward the ability to enable the profiling from within the profiling scope. eg:: with maybeprofiling(ui) as profiler: ... bar.foo(): ... if options['profile']: profiler.start() ... fooz() ... My target usecase is adding support for "--profile" to alias definitions with effect. These are to be used with "profiling.output=blackbox" to gather data about operation that get slow from time to time (eg: pull being minutes instead of seconds from time to time). Of course, in such case, the scope of the profiling would be smaller since profiler would be started after running extensions 'reposetup' (and other potentially costly logic), but these are not relevant for my target usecase (multiple second commits, multiple tens of seconds pull). Currently adding '--profile' to a command through alias requires to re-spin a Mercurial binary (using "!$HG" in alias), which as a significant performance impact, especially in context where startup performance is being worked on... An alternative approach would be to stop using the context manager in dispatch and move back to a try/finally setup.

#!/usr/bin/env python

from mercurial import demandimport
demandimport.enable()

import getopt
import sys
from mercurial.i18n import _
from mercurial import error, simplemerge, fancyopts, util, ui

options = [('L', 'label', [], _('labels to use on conflict markers')),
           ('a', 'text', None, _('treat all files as text')),
           ('p', 'print', None,
            _('print results instead of overwriting LOCAL')),
           ('', 'no-minimal', None, _('no effect (DEPRECATED)')),
           ('h', 'help', None, _('display help and exit')),
           ('q', 'quiet', None, _('suppress output'))]

usage = _('''simplemerge [OPTS] LOCAL BASE OTHER

    Simple three-way file merge utility with a minimal feature set.

    Apply to LOCAL the changes necessary to go from BASE to OTHER.

    By default, LOCAL is overwritten with the results of this operation.
''')

class ParseError(Exception):
    """Exception raised on errors in parsing the command line."""

def showhelp():
    sys.stdout.write(usage)
    sys.stdout.write('\noptions:\n')

    out_opts = []
    for shortopt, longopt, default, desc in options:
        out_opts.append(('%2s%s' % (shortopt and '-%s' % shortopt,
                                    longopt and ' --%s' % longopt),
                         '%s' % desc))
    opts_len = max([len(opt[0]) for opt in out_opts])
    for first, second in out_opts:
        sys.stdout.write(' %-*s  %s\n' % (opts_len, first, second))

try:
    for fp in (sys.stdin, sys.stdout, sys.stderr):
        util.setbinary(fp)

    opts = {}
    try:
        args = fancyopts.fancyopts(sys.argv[1:], options, opts)
    except getopt.GetoptError as e:
        raise ParseError(e)
    if opts['help']:
        showhelp()
        sys.exit(0)
    if len(args) != 3:
            raise ParseError(_('wrong number of arguments'))
    sys.exit(simplemerge.simplemerge(ui.ui.load(), *args, **opts))
except ParseError as e:
    sys.stdout.write("%s: %s\n" % (sys.argv[0], e))
    showhelp()
    sys.exit(1)
except error.Abort as e:
    sys.stderr.write("abort: %s\n" % e)
    sys.exit(255)
except KeyboardInterrupt:
    sys.exit(255)