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
41from __future__ import print_function
42import re
43import sys
44
45import gem5_scons.util
46
47mercurial_style_message = """
48You're missing the gem5 style hook, which automatically checks your code
49against the gem5 style rules on hg commit and qrefresh commands.
50This script will now install the hook in your .hg/hgrc file.
51Press enter to continue, or ctrl-c to abort: """
52
53mercurial_style_upgrade_message = """
54Your Mercurial style hooks are not up-to-date. This script will now
55try to automatically update them. A backup of your hgrc will be saved
56in .hg/hgrc.old.
57Press enter to continue, or ctrl-c to abort: """
58
59mercurial_style_hook_template = """
60# The following lines were automatically added by gem5/SConstruct
61# to provide the gem5 style-checking hooks
62[extensions]
63hgstyle = %s/util/hgstyle.py
64
65[hooks]
66pretxncommit.style = python:hgstyle.check_style
67pre-qrefresh.style = python:hgstyle.check_style
68# End of SConstruct additions
69
70"""
71
72mercurial_lib_not_found = """
73Mercurial libraries cannot be found, ignoring style hook.  If
74you are a gem5 developer, please fix this and run the style
75hook. It is important.
76"""
77
78def install_style_hooks(env):
79    hgdir = env.Dir('#.hg')
80
81    style_hook = True
82    style_hooks = tuple()
83    hgrc = hgdir.File('hgrc')
84    hgrc_old = hgdir.File('hgrc.old')
85    try:
86        from mercurial import ui
87        ui = ui.ui()
88        ui.readconfig(hgrc.abspath)
89        style_hooks = (ui.config('hooks', 'pretxncommit.style', None),
90                       ui.config('hooks', 'pre-qrefresh.style', None))
91        style_hook = all(style_hooks)
92        style_extension = ui.config('extensions', 'style', None)
93    except ImportError:
94        print(mercurial_lib_not_found)
95
96    if "python:style.check_style" in style_hooks:
97        # Try to upgrade the style hooks
98        print(mercurial_style_upgrade_message)
99        # continue unless user does ctrl-c/ctrl-d etc.
100        try:
101            raw_input()
102        except:
103            print("Input exception, exiting scons.\n")
104            sys.exit(1)
105        shutil.copyfile(hgrc.abspath, hgrc_old.abspath)
106        re_style_hook = re.compile(r"^([^=#]+)\.style\s*=\s*([^#\s]+).*")
107        re_style_extension = re.compile("style\s*=\s*([^#\s]+).*")
108        old, new = open(hgrc_old.abspath, 'r'), open(hgrc.abspath, 'w')
109        for l in old:
110            m_hook = re_style_hook.match(l)
111            m_ext = re_style_extension.match(l)
112            if m_hook:
113                hook, check = m_hook.groups()
114                if check != "python:style.check_style":
115                    print("Warning: %s.style is using a non-default " \
116                        "checker: %s" % (hook, check))
117                if hook not in ("pretxncommit", "pre-qrefresh"):
118                    print("Warning: Updating unknown style hook: %s" % hook)
119
120                l = "%s.style = python:hgstyle.check_style\n" % hook
121            elif m_ext and m_ext.group(1) == style_extension:
122                l = "hgstyle = %s/util/hgstyle.py\n" % env.root.abspath
123
124            new.write(l)
125    elif not style_hook:
126        print(mercurial_style_message, end=' ')
127        # continue unless user does ctrl-c/ctrl-d etc.
128        try:
129            raw_input()
130        except:
131            print("Input exception, exiting scons.\n")
132            sys.exit(1)
133        hgrc_path = '%s/.hg/hgrc' % env.root.abspath
134        print("Adding style hook to", hgrc_path, "\n")
135        try:
136            with open(hgrc_path, 'a') as f:
137                f.write(mercurial_style_hook_template % env.root.abspath)
138        except:
139            print("Error updating", hgrc_path)
140            sys.exit(1)
141
142def generate(env):
143    if exists(env) and not gem5_scons.util.ignore_style():
144        install_style_hooks(env)
145
146def exists(env):
147    return env.Dir('#.hg').exists()
148