hgstyle.py revision 11409:72f80dd8b194
113038Sgabeblack@google.com#! /usr/bin/env python
213038Sgabeblack@google.com# Copyright (c) 2014 ARM Limited
313038Sgabeblack@google.com# All rights reserved
413038Sgabeblack@google.com#
513038Sgabeblack@google.com# The license below extends only to copyright in the software and shall
613038Sgabeblack@google.com# not be construed as granting a license to any other intellectual
713038Sgabeblack@google.com# property including but not limited to intellectual property relating
813038Sgabeblack@google.com# to a hardware implementation of the functionality of the software
913038Sgabeblack@google.com# licensed hereunder.  You may use the software subject to the license
1013038Sgabeblack@google.com# terms below provided that you ensure that this notice is replicated
1113038Sgabeblack@google.com# unmodified and in its entirety in all distributions of the software,
1213038Sgabeblack@google.com# modified or unmodified, in source code or in binary form.
1313038Sgabeblack@google.com#
1413038Sgabeblack@google.com# Copyright (c) 2006 The Regents of The University of Michigan
1513038Sgabeblack@google.com# Copyright (c) 2007,2011 The Hewlett-Packard Development Company
1613038Sgabeblack@google.com# Copyright (c) 2016 Advanced Micro Devices, Inc.
1713038Sgabeblack@google.com# All rights reserved.
1813038Sgabeblack@google.com#
1913038Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
2013038Sgabeblack@google.com# modification, are permitted provided that the following conditions are
2113038Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
2213038Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
2313038Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
2413038Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
2513038Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
2613038Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
2713038Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
2813038Sgabeblack@google.com# this software without specific prior written permission.
2913038Sgabeblack@google.com#
3013038Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3113038Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3213038Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3313038Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3413038Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3513038Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3613038Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3713038Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3813038Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3913038Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4013038Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4113038Sgabeblack@google.com#
4213038Sgabeblack@google.com# Authors: Nathan Binkert
4313038Sgabeblack@google.com#          Steve Reinhardt
4413038Sgabeblack@google.com
4513038Sgabeblack@google.comimport sys
4613038Sgabeblack@google.comimport os
4713038Sgabeblack@google.comfrom os.path import join as joinpath
4813038Sgabeblack@google.com
4913038Sgabeblack@google.comcurrent_dir = os.path.dirname(__file__)
5013038Sgabeblack@google.comsys.path.insert(0, current_dir)
5113038Sgabeblack@google.com
5213038Sgabeblack@google.comfrom style.verifiers import all_verifiers
5313038Sgabeblack@google.comfrom style.style import MercurialUI, check_ignores
5413038Sgabeblack@google.comfrom style.region import *
55
56from mercurial import bdiff, mdiff, commands
57
58def modified_regions(old_data, new_data):
59    regions = Regions()
60    beg = None
61    for pbeg, pend, fbeg, fend in bdiff.blocks(old_data, new_data):
62        if beg is not None and beg != fbeg:
63            regions.append(beg, fbeg)
64        beg = fend
65    return regions
66
67def modregions(wctx, fname):
68    fctx = wctx.filectx(fname)
69    pctx = fctx.parents()
70
71    file_data = fctx.data()
72    lines = mdiff.splitnewlines(file_data)
73    if len(pctx) in (1, 2):
74        mod_regions = modified_regions(pctx[0].data(), file_data)
75        if len(pctx) == 2:
76            m2 = modified_regions(pctx[1].data(), file_data)
77            # only the lines that are new in both
78            mod_regions &= m2
79    else:
80        mod_regions = Regions()
81        mod_regions.append(0, len(lines))
82
83    return mod_regions
84
85
86def _modified_regions(repo, patterns, **kwargs):
87    opt_all = kwargs.get('all', False)
88    opt_no_ignore = kwargs.get('no_ignore', False)
89
90    # Import the match (repository file name matching helper)
91    # function. Different versions of Mercurial keep it in different
92    # modules and implement them differently.
93    try:
94        from mercurial import scmutil
95        m = scmutil.match(repo[None], patterns, kwargs)
96    except ImportError:
97        from mercurial import cmdutil
98        m = cmdutil.match(repo, patterns, kwargs)
99
100    modified, added, removed, deleted, unknown, ignore, clean = \
101        repo.status(match=m, clean=opt_all)
102
103    if not opt_all:
104        try:
105            wctx = repo.workingctx()
106        except:
107            from mercurial import context
108            wctx = context.workingctx(repo)
109
110        files = [ (fn, all_regions) for fn in added ] + \
111            [ (fn,  modregions(wctx, fn)) for fn in modified ]
112    else:
113        files = [ (fn, all_regions) for fn in added + modified + clean ]
114
115    for fname, mod_regions in files:
116        if opt_no_ignore or not check_ignores(fname):
117            yield fname, mod_regions
118
119
120def do_check_style(hgui, repo, *pats, **opts):
121    """check files for proper m5 style guidelines
122
123    Without an argument, checks all modified and added files for gem5
124    coding style violations. A list of files can be specified to limit
125    the checker to a subset of the repository. The style rules are
126    normally applied on a diff of the repository state (i.e., added
127    files are checked in their entirety while only modifications of
128    modified files are checked).
129
130    The --all option can be specified to include clean files and check
131    modified files in their entirety.
132
133    The --fix-<check>, --ignore-<check>, and --skip-<check> options
134    can be used to control individual style checks:
135
136    --fix-<check> will perform the check and automatically attempt to
137      fix sny style error (printing a warning if unsuccessful)
138
139    --ignore-<check> will perform the check but ignore any errors
140      found (other than printing a message for each)
141
142    --skip-<check> will skip performing the check entirely
143
144    If none of these options are given, all checks will be performed
145    and the user will be prompted on how to handle each error.
146
147    --fix-all, --ignore-all, and --skip-all are equivalent to specifying
148    --fix-<check>, --ignore-<check>, or --skip-<check> for all checks,
149    respectively.  However, option settings for specific checks take
150    precedence.  Thus --skip-all --fix-white can be used to skip every
151    check other than whitespace errors, which will be checked and
152    automatically fixed.
153
154    The -v/--verbose flag will display the offending line(s) as well
155    as their location.
156    """
157
158    ui = MercurialUI(hgui, verbose=hgui.verbose)
159
160    # instantiate varifier objects
161    verifiers = [v(ui, opts, base=repo.root) for v in all_verifiers]
162
163    for fname, mod_regions in _modified_regions(repo, pats, **opts):
164        for verifier in verifiers:
165            if verifier.apply(joinpath(repo.root, fname), mod_regions):
166                return True
167
168    return False
169
170def check_hook(hooktype):
171    if hooktype not in ('pretxncommit', 'pre-qrefresh'):
172        raise AttributeError, \
173              "This hook is not meant for %s" % hooktype
174
175# This function provides a hook that is called before transaction
176# commit and on qrefresh
177def check_style(ui, repo, hooktype, **kwargs):
178    check_hook(hooktype)
179    args = {}
180
181    try:
182        return do_check_style(ui, repo, **args)
183    except Exception, e:
184        import traceback
185        traceback.print_exc()
186        return True
187
188try:
189    from mercurial.i18n import _
190except ImportError:
191    def _(arg):
192        return arg
193
194_common_region_options = [
195    ('a', 'all', False,
196     _("include clean files and unmodified parts of modified files")),
197    ('', 'no-ignore', False, _("ignore the style ignore list")),
198    ]
199
200
201fix_opts = [('f', 'fix-all', False, _("fix all style errors"))] + \
202           [('', 'fix-' + v.opt_name, False,
203             _('fix errors in ' + v.test_name)) for v in all_verifiers]
204ignore_opts = [('', 'ignore-all', False, _("ignore all style errors"))] + \
205              [('', 'ignore-' + v.opt_name, False,
206                _('ignore errors in ' + v.test_name)) for v in all_verifiers]
207skip_opts = [('', 'skip-all', False, _("skip all style error checks"))] + \
208            [('', 'skip-' + v.opt_name, False,
209              _('skip checking for ' + v.test_name)) for v in all_verifiers]
210all_opts = fix_opts + ignore_opts + skip_opts
211
212
213cmdtable = {
214    '^m5style' : (
215        do_check_style, all_opts + _common_region_options + commands.walkopts,
216        _('hg m5style [-a] [FILE]...')),
217}
218
219if __name__ == '__main__':
220    print >> sys.stderr, "This file cannot be used from the command line. Use"
221    print >> sys.stderr, "style.py instead."
222    sys.exit(1)
223