mercurial.py revision 12558
111784Sarthur.perais@inria.fr# Copyright (c) 2013, 2015-2017 ARM Limited
211784Sarthur.perais@inria.fr# All rights reserved.
311784Sarthur.perais@inria.fr#
411784Sarthur.perais@inria.fr# The license below extends only to copyright in the software and shall
511784Sarthur.perais@inria.fr# not be construed as granting a license to any other intellectual
611784Sarthur.perais@inria.fr# property including but not limited to intellectual property relating
711784Sarthur.perais@inria.fr# to a hardware implementation of the functionality of the software
811784Sarthur.perais@inria.fr# licensed hereunder.  You may use the software subject to the license
911784Sarthur.perais@inria.fr# terms below provided that you ensure that this notice is replicated
1011784Sarthur.perais@inria.fr# unmodified and in its entirety in all distributions of the software,
1111784Sarthur.perais@inria.fr# modified or unmodified, in source code or in binary form.
1211784Sarthur.perais@inria.fr#
1311784Sarthur.perais@inria.fr# Copyright (c) 2011 Advanced Micro Devices, Inc.
1411784Sarthur.perais@inria.fr# Copyright (c) 2009 The Hewlett-Packard Development Company
1511784Sarthur.perais@inria.fr# Copyright (c) 2004-2005 The Regents of The University of Michigan
1611784Sarthur.perais@inria.fr# All rights reserved.
1711784Sarthur.perais@inria.fr#
1811784Sarthur.perais@inria.fr# Redistribution and use in source and binary forms, with or without
1911784Sarthur.perais@inria.fr# modification, are permitted provided that the following conditions are
2011784Sarthur.perais@inria.fr# met: redistributions of source code must retain the above copyright
2111784Sarthur.perais@inria.fr# notice, this list of conditions and the following disclaimer;
2211784Sarthur.perais@inria.fr# redistributions in binary form must reproduce the above copyright
2311784Sarthur.perais@inria.fr# notice, this list of conditions and the following disclaimer in the
2411784Sarthur.perais@inria.fr# documentation and/or other materials provided with the distribution;
2511784Sarthur.perais@inria.fr# neither the name of the copyright holders nor the names of its
2611784Sarthur.perais@inria.fr# contributors may be used to endorse or promote products derived from
2711784Sarthur.perais@inria.fr# this software without specific prior written permission.
2811784Sarthur.perais@inria.fr#
2911784Sarthur.perais@inria.fr# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3011784Sarthur.perais@inria.fr# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3111784Sarthur.perais@inria.fr# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3211784Sarthur.perais@inria.fr# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3311784Sarthur.perais@inria.fr# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3411784Sarthur.perais@inria.fr# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3511784Sarthur.perais@inria.fr# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3611784Sarthur.perais@inria.fr# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3711784Sarthur.perais@inria.fr# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3811784Sarthur.perais@inria.fr# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3911784Sarthur.perais@inria.fr# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4011784Sarthur.perais@inria.fr
4111784Sarthur.perais@inria.frfrom __future__ import print_function
4211784Sarthur.perais@inria.frimport re
4311784Sarthur.perais@inria.frimport sys
4411784Sarthur.perais@inria.fr
4511784Sarthur.perais@inria.frimport gem5_scons.util
4611784Sarthur.perais@inria.fr
4711784Sarthur.perais@inria.frmercurial_style_message = """
4811784Sarthur.perais@inria.frYou're missing the gem5 style hook, which automatically checks your code
4911784Sarthur.perais@inria.fragainst the gem5 style rules on hg commit and qrefresh commands.
5011784Sarthur.perais@inria.frThis script will now install the hook in your .hg/hgrc file.
5111784Sarthur.perais@inria.frPress enter to continue, or ctrl-c to abort: """
5211784Sarthur.perais@inria.fr
5311784Sarthur.perais@inria.frmercurial_style_upgrade_message = """
5411784Sarthur.perais@inria.frYour Mercurial style hooks are not up-to-date. This script will now
5511784Sarthur.perais@inria.frtry to automatically update them. A backup of your hgrc will be saved
5611784Sarthur.perais@inria.frin .hg/hgrc.old.
5711784Sarthur.perais@inria.frPress enter to continue, or ctrl-c to abort: """
5811784Sarthur.perais@inria.fr
5911784Sarthur.perais@inria.frmercurial_style_hook_template = """
6011784Sarthur.perais@inria.fr# The following lines were automatically added by gem5/SConstruct
6111784Sarthur.perais@inria.fr# to provide the gem5 style-checking hooks
6211784Sarthur.perais@inria.fr[extensions]
6311784Sarthur.perais@inria.frhgstyle = %s/util/hgstyle.py
6411784Sarthur.perais@inria.fr
6511784Sarthur.perais@inria.fr[hooks]
6611784Sarthur.perais@inria.frpretxncommit.style = python:hgstyle.check_style
6711784Sarthur.perais@inria.frpre-qrefresh.style = python:hgstyle.check_style
6811784Sarthur.perais@inria.fr# End of SConstruct additions
6911784Sarthur.perais@inria.fr
7011784Sarthur.perais@inria.fr"""
7111784Sarthur.perais@inria.fr
7211784Sarthur.perais@inria.frmercurial_lib_not_found = """
7311784Sarthur.perais@inria.frMercurial libraries cannot be found, ignoring style hook.  If
7411784Sarthur.perais@inria.fryou are a gem5 developer, please fix this and run the style
7511784Sarthur.perais@inria.frhook. It is important.
7611784Sarthur.perais@inria.fr"""
7711784Sarthur.perais@inria.fr
7811784Sarthur.perais@inria.frdef install_style_hooks(env):
7911784Sarthur.perais@inria.fr    hgdir = env.Dir('#.hg')
8011784Sarthur.perais@inria.fr
8111784Sarthur.perais@inria.fr    style_hook = True
8211784Sarthur.perais@inria.fr    style_hooks = tuple()
8311784Sarthur.perais@inria.fr    hgrc = hgdir.File('hgrc')
8411784Sarthur.perais@inria.fr    hgrc_old = hgdir.File('hgrc.old')
8511784Sarthur.perais@inria.fr    try:
8611784Sarthur.perais@inria.fr        from mercurial import ui
8711784Sarthur.perais@inria.fr        ui = ui.ui()
8811784Sarthur.perais@inria.fr        ui.readconfig(hgrc.abspath)
8911784Sarthur.perais@inria.fr        style_hooks = (ui.config('hooks', 'pretxncommit.style', None),
9011784Sarthur.perais@inria.fr                       ui.config('hooks', 'pre-qrefresh.style', None))
9111784Sarthur.perais@inria.fr        style_hook = all(style_hooks)
9211784Sarthur.perais@inria.fr        style_extension = ui.config('extensions', 'style', None)
9311784Sarthur.perais@inria.fr    except ImportError:
9411784Sarthur.perais@inria.fr        print(mercurial_lib_not_found)
9511784Sarthur.perais@inria.fr
9611784Sarthur.perais@inria.fr    if "python:style.check_style" in style_hooks:
9711784Sarthur.perais@inria.fr        # Try to upgrade the style hooks
9811784Sarthur.perais@inria.fr        print(mercurial_style_upgrade_message)
9911784Sarthur.perais@inria.fr        # continue unless user does ctrl-c/ctrl-d etc.
10011784Sarthur.perais@inria.fr        try:
10111784Sarthur.perais@inria.fr            raw_input()
10211784Sarthur.perais@inria.fr        except:
10311784Sarthur.perais@inria.fr            print("Input exception, exiting scons.\n")
10411784Sarthur.perais@inria.fr            sys.exit(1)
10511784Sarthur.perais@inria.fr        shutil.copyfile(hgrc.abspath, hgrc_old.abspath)
10611784Sarthur.perais@inria.fr        re_style_hook = re.compile(r"^([^=#]+)\.style\s*=\s*([^#\s]+).*")
10711784Sarthur.perais@inria.fr        re_style_extension = re.compile("style\s*=\s*([^#\s]+).*")
10811784Sarthur.perais@inria.fr        old, new = open(hgrc_old.abspath, 'r'), open(hgrc.abspath, 'w')
10911784Sarthur.perais@inria.fr        for l in old:
11011784Sarthur.perais@inria.fr            m_hook = re_style_hook.match(l)
11111784Sarthur.perais@inria.fr            m_ext = re_style_extension.match(l)
11211784Sarthur.perais@inria.fr            if m_hook:
11311784Sarthur.perais@inria.fr                hook, check = m_hook.groups()
11411784Sarthur.perais@inria.fr                if check != "python:style.check_style":
11511784Sarthur.perais@inria.fr                    print("Warning: %s.style is using a non-default " \
11611784Sarthur.perais@inria.fr                        "checker: %s" % (hook, check))
11711784Sarthur.perais@inria.fr                if hook not in ("pretxncommit", "pre-qrefresh"):
11811784Sarthur.perais@inria.fr                    print("Warning: Updating unknown style hook: %s" % hook)
11911784Sarthur.perais@inria.fr
12011784Sarthur.perais@inria.fr                l = "%s.style = python:hgstyle.check_style\n" % hook
12111784Sarthur.perais@inria.fr            elif m_ext and m_ext.group(1) == style_extension:
12211784Sarthur.perais@inria.fr                l = "hgstyle = %s/util/hgstyle.py\n" % env.root.abspath
12311784Sarthur.perais@inria.fr
12411784Sarthur.perais@inria.fr            new.write(l)
12511784Sarthur.perais@inria.fr    elif not style_hook:
12611784Sarthur.perais@inria.fr        print(mercurial_style_message, end=' ')
12711784Sarthur.perais@inria.fr        # continue unless user does ctrl-c/ctrl-d etc.
12811784Sarthur.perais@inria.fr        try:
12911784Sarthur.perais@inria.fr            raw_input()
13011784Sarthur.perais@inria.fr        except:
13111784Sarthur.perais@inria.fr            print("Input exception, exiting scons.\n")
13211784Sarthur.perais@inria.fr            sys.exit(1)
13311784Sarthur.perais@inria.fr        hgrc_path = '%s/.hg/hgrc' % env.root.abspath
13411784Sarthur.perais@inria.fr        print("Adding style hook to", hgrc_path, "\n")
13511784Sarthur.perais@inria.fr        try:
13611784Sarthur.perais@inria.fr            with open(hgrc_path, 'a') as f:
13711784Sarthur.perais@inria.fr                f.write(mercurial_style_hook_template % env.root.abspath)
13811784Sarthur.perais@inria.fr        except:
13911784Sarthur.perais@inria.fr            print("Error updating", hgrc_path)
14011784Sarthur.perais@inria.fr            sys.exit(1)
14111784Sarthur.perais@inria.fr
14211784Sarthur.perais@inria.frdef generate(env):
14311784Sarthur.perais@inria.fr    if exists(env) and not gem5_scons.util.ignore_style():
14411784Sarthur.perais@inria.fr        install_style_hooks(env)
14511784Sarthur.perais@inria.fr
14611784Sarthur.perais@inria.frdef exists(env):
14711784Sarthur.perais@inria.fr    return env.Dir('#.hg').exists()
14811784Sarthur.perais@inria.fr