mercurial.py revision 12244
1# Copyright (c) 2013, 2015-2017 ARM Limited
2# All rights reserved.
3#
4# The license below extends only to copyright in the software and shall
5# not be construed as granting a license to any other intellectual
6# property including but not limited to intellectual property relating
7# to a hardware implementation of the functionality of the software
8# licensed hereunder.  You may use the software subject to the license
9# terms below provided that you ensure that this notice is replicated
10# unmodified and in its entirety in all distributions of the software,
11# modified or unmodified, in source code or in binary form.
12#
13# Copyright (c) 2011 Advanced Micro Devices, Inc.
14# Copyright (c) 2009 The Hewlett-Packard Development Company
15# Copyright (c) 2004-2005 The Regents of The University of Michigan
16# All rights reserved.
17#
18# Redistribution and use in source and binary forms, with or without
19# modification, are permitted provided that the following conditions are
20# met: redistributions of source code must retain the above copyright
21# notice, this list of conditions and the following disclaimer;
22# redistributions in binary form must reproduce the above copyright
23# notice, this list of conditions and the following disclaimer in the
24# documentation and/or other materials provided with the distribution;
25# neither the name of the copyright holders nor the names of its
26# contributors may be used to endorse or promote products derived from
27# this software without specific prior written permission.
28#
29# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41import re
42import sys
43
44import gem5_scons.util
45
46mercurial_style_message = """
47You're missing the gem5 style hook, which automatically checks your code
48against the gem5 style rules on hg commit and qrefresh commands.
49This script will now install the hook in your .hg/hgrc file.
50Press enter to continue, or ctrl-c to abort: """
51
52mercurial_style_upgrade_message = """
53Your Mercurial style hooks are not up-to-date. This script will now
54try to automatically update them. A backup of your hgrc will be saved
55in .hg/hgrc.old.
56Press enter to continue, or ctrl-c to abort: """
57
58mercurial_style_hook_template = """
59# The following lines were automatically added by gem5/SConstruct
60# to provide the gem5 style-checking hooks
61[extensions]
62hgstyle = %s/util/hgstyle.py
63
64[hooks]
65pretxncommit.style = python:hgstyle.check_style
66pre-qrefresh.style = python:hgstyle.check_style
67# End of SConstruct additions
68
69"""
70
71mercurial_lib_not_found = """
72Mercurial libraries cannot be found, ignoring style hook.  If
73you are a gem5 developer, please fix this and run the style
74hook. It is important.
75"""
76
77def install_style_hooks(env):
78    hgdir = env.Dir('#.hg')
79
80    style_hook = True
81    style_hooks = tuple()
82    hgrc = hgdir.File('hgrc')
83    hgrc_old = hgdir.File('hgrc.old')
84    try:
85        from mercurial import ui
86        ui = ui.ui()
87        ui.readconfig(hgrc.abspath)
88        style_hooks = (ui.config('hooks', 'pretxncommit.style', None),
89                       ui.config('hooks', 'pre-qrefresh.style', None))
90        style_hook = all(style_hooks)
91        style_extension = ui.config('extensions', 'style', None)
92    except ImportError:
93        print mercurial_lib_not_found
94
95    if "python:style.check_style" in style_hooks:
96        # Try to upgrade the style hooks
97        print mercurial_style_upgrade_message
98        # continue unless user does ctrl-c/ctrl-d etc.
99        try:
100            raw_input()
101        except:
102            print "Input exception, exiting scons.\n"
103            sys.exit(1)
104        shutil.copyfile(hgrc.abspath, hgrc_old.abspath)
105        re_style_hook = re.compile(r"^([^=#]+)\.style\s*=\s*([^#\s]+).*")
106        re_style_extension = re.compile("style\s*=\s*([^#\s]+).*")
107        old, new = open(hgrc_old.abspath, 'r'), open(hgrc.abspath, 'w')
108        for l in old:
109            m_hook = re_style_hook.match(l)
110            m_ext = re_style_extension.match(l)
111            if m_hook:
112                hook, check = m_hook.groups()
113                if check != "python:style.check_style":
114                    print "Warning: %s.style is using a non-default " \
115                        "checker: %s" % (hook, check)
116                if hook not in ("pretxncommit", "pre-qrefresh"):
117                    print "Warning: Updating unknown style hook: %s" % hook
118
119                l = "%s.style = python:hgstyle.check_style\n" % hook
120            elif m_ext and m_ext.group(1) == style_extension:
121                l = "hgstyle = %s/util/hgstyle.py\n" % env.root.abspath
122
123            new.write(l)
124    elif not style_hook:
125        print mercurial_style_message,
126        # continue unless user does ctrl-c/ctrl-d etc.
127        try:
128            raw_input()
129        except:
130            print "Input exception, exiting scons.\n"
131            sys.exit(1)
132        hgrc_path = '%s/.hg/hgrc' % env.root.abspath
133        print "Adding style hook to", hgrc_path, "\n"
134        try:
135            with open(hgrc_path, 'a') as f:
136                f.write(mercurial_style_hook_template % env.root.abspath)
137        except:
138            print "Error updating", hgrc_path
139            sys.exit(1)
140
141def generate(env):
142    if exists(env) and not gem5_scons.util.ignore_style():
143        install_style_hooks(env)
144
145def exists(env):
146    return env.Dir('#.hg').exists()
147