113540Sandrea.mondelli@ucf.edu#!/usr/bin/env python2.7
211407Sandreas.sandberg@arm.com#
311407Sandreas.sandberg@arm.com# Copyright (c) 2016 ARM Limited
411407Sandreas.sandberg@arm.com# All rights reserved
511407Sandreas.sandberg@arm.com#
611407Sandreas.sandberg@arm.com# The license below extends only to copyright in the software and shall
711407Sandreas.sandberg@arm.com# not be construed as granting a license to any other intellectual
811407Sandreas.sandberg@arm.com# property including but not limited to intellectual property relating
911407Sandreas.sandberg@arm.com# to a hardware implementation of the functionality of the software
1011407Sandreas.sandberg@arm.com# licensed hereunder.  You may use the software subject to the license
1111407Sandreas.sandberg@arm.com# terms below provided that you ensure that this notice is replicated
1211407Sandreas.sandberg@arm.com# unmodified and in its entirety in all distributions of the software,
1311407Sandreas.sandberg@arm.com# modified or unmodified, in source code or in binary form.
1411407Sandreas.sandberg@arm.com#
1511407Sandreas.sandberg@arm.com# Redistribution and use in source and binary forms, with or without
1611407Sandreas.sandberg@arm.com# modification, are permitted provided that the following conditions are
1711407Sandreas.sandberg@arm.com# met: redistributions of source code must retain the above copyright
1811407Sandreas.sandberg@arm.com# notice, this list of conditions and the following disclaimer;
1911407Sandreas.sandberg@arm.com# redistributions in binary form must reproduce the above copyright
2011407Sandreas.sandberg@arm.com# notice, this list of conditions and the following disclaimer in the
2111407Sandreas.sandberg@arm.com# documentation and/or other materials provided with the distribution;
2211407Sandreas.sandberg@arm.com# neither the name of the copyright holders nor the names of its
2311407Sandreas.sandberg@arm.com# contributors may be used to endorse or promote products derived from
2411407Sandreas.sandberg@arm.com# this software without specific prior written permission.
2511407Sandreas.sandberg@arm.com#
2611407Sandreas.sandberg@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2711407Sandreas.sandberg@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2811407Sandreas.sandberg@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2911407Sandreas.sandberg@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3011407Sandreas.sandberg@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3111407Sandreas.sandberg@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3211407Sandreas.sandberg@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3311407Sandreas.sandberg@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3411407Sandreas.sandberg@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3511407Sandreas.sandberg@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3611407Sandreas.sandberg@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3711407Sandreas.sandberg@arm.com#
3811407Sandreas.sandberg@arm.com# Authors: Andreas Sandberg
3911407Sandreas.sandberg@arm.com
4011716Srekai.gonzalezalberquilla@arm.comfrom tempfile import TemporaryFile
4111407Sandreas.sandberg@arm.comimport os
4211716Srekai.gonzalezalberquilla@arm.comimport subprocess
4311407Sandreas.sandberg@arm.comimport sys
4411407Sandreas.sandberg@arm.com
4511407Sandreas.sandberg@arm.comfrom style.repo import GitRepo
4611407Sandreas.sandberg@arm.comfrom style.verifiers import all_verifiers, all_regions
4711467SCurtis.Dunham@arm.comfrom style.style import StdioUI, check_ignores
4811407Sandreas.sandberg@arm.com
4911407Sandreas.sandberg@arm.comimport argparse
5011407Sandreas.sandberg@arm.com
5111407Sandreas.sandberg@arm.comparser = argparse.ArgumentParser(
5211407Sandreas.sandberg@arm.com    description="gem5 git style checker hook")
5311407Sandreas.sandberg@arm.com
5411407Sandreas.sandberg@arm.comparser.add_argument("--verbose", "-v", action="store_true",
5511407Sandreas.sandberg@arm.com                    help="Produce verbose output")
5611407Sandreas.sandberg@arm.com
5711407Sandreas.sandberg@arm.comargs = parser.parse_args()
5811407Sandreas.sandberg@arm.com
5911407Sandreas.sandberg@arm.comgit = GitRepo()
6011407Sandreas.sandberg@arm.com
6111407Sandreas.sandberg@arm.comopts = {}
6211407Sandreas.sandberg@arm.comrepo_base = git.repo_base()
6311407Sandreas.sandberg@arm.comui = StdioUI()
6411407Sandreas.sandberg@arm.com
6511407Sandreas.sandberg@arm.comos.chdir(repo_base)
6611407Sandreas.sandberg@arm.comfailing_files = set()
6711716Srekai.gonzalezalberquilla@arm.comstaged_mismatch = set()
6811407Sandreas.sandberg@arm.com
6911407Sandreas.sandberg@arm.comfor status, fname in git.status(filter="MA", cached=True):
7011407Sandreas.sandberg@arm.com    if args.verbose:
7111407Sandreas.sandberg@arm.com        print "Checking %s..." % fname
7211467SCurtis.Dunham@arm.com    if check_ignores(fname):
7311467SCurtis.Dunham@arm.com        continue
7411407Sandreas.sandberg@arm.com    if status == "M":
7511465Sandreas.sandberg@arm.com        regions = git.staged_regions(fname)
7611407Sandreas.sandberg@arm.com    else:
7711407Sandreas.sandberg@arm.com        regions = all_regions
7811407Sandreas.sandberg@arm.com
7911716Srekai.gonzalezalberquilla@arm.com    # Show they appropriate object and dump it to a file
8011716Srekai.gonzalezalberquilla@arm.com    status = git.file_from_index(fname)
8111716Srekai.gonzalezalberquilla@arm.com    f = TemporaryFile()
8211716Srekai.gonzalezalberquilla@arm.com    f.write(status)
8311716Srekai.gonzalezalberquilla@arm.com
8411407Sandreas.sandberg@arm.com    verifiers = [ v(ui, opts, base=repo_base) for v in all_verifiers ]
8511407Sandreas.sandberg@arm.com    for v in verifiers:
8611716Srekai.gonzalezalberquilla@arm.com        f.seek(0)
8711716Srekai.gonzalezalberquilla@arm.com        # It is prefered that the first check is silent as it is in the
8811716Srekai.gonzalezalberquilla@arm.com        # staged file. If the check fails, then we will do it non-silently
8911716Srekai.gonzalezalberquilla@arm.com        # on the current file, reporting meaningful shortcomings
9011716Srekai.gonzalezalberquilla@arm.com        if not v.skip(fname) and v.check(fname, regions, fobj=f, silent=True):
9111407Sandreas.sandberg@arm.com            failing_files.add(fname)
9211716Srekai.gonzalezalberquilla@arm.com            if not v.check(fname, regions):
9311716Srekai.gonzalezalberquilla@arm.com                staged_mismatch.add(fname)
9411716Srekai.gonzalezalberquilla@arm.com    f.close()
9511407Sandreas.sandberg@arm.com
9611407Sandreas.sandberg@arm.comif failing_files:
9711716Srekai.gonzalezalberquilla@arm.com    if len(failing_files) > len(staged_mismatch):
9811716Srekai.gonzalezalberquilla@arm.com        print >> sys.stderr
9911716Srekai.gonzalezalberquilla@arm.com        print >> sys.stderr, "Style checker failed for the following files:"
10011716Srekai.gonzalezalberquilla@arm.com        for f in failing_files:
10111716Srekai.gonzalezalberquilla@arm.com            if f not in staged_mismatch:
10211716Srekai.gonzalezalberquilla@arm.com                print >> sys.stderr, "\t%s" % f
10311716Srekai.gonzalezalberquilla@arm.com        print >> sys.stderr
10411716Srekai.gonzalezalberquilla@arm.com        print >> sys.stderr, \
10511407Sandreas.sandberg@arm.com        "Please run the style checker manually to fix the offending files.\n" \
10611407Sandreas.sandberg@arm.com        "To check your modifications, run: util/style.py -m"
10711716Srekai.gonzalezalberquilla@arm.com
10811716Srekai.gonzalezalberquilla@arm.com    print >> sys.stderr
10911716Srekai.gonzalezalberquilla@arm.com    if staged_mismatch:
11011716Srekai.gonzalezalberquilla@arm.com        print >> sys.stderr, \
11111716Srekai.gonzalezalberquilla@arm.com        "It looks like you have forgotten to stage your fixes for commit in\n"\
11211716Srekai.gonzalezalberquilla@arm.com        "the following files: "
11311716Srekai.gonzalezalberquilla@arm.com        for f in staged_mismatch:
11411716Srekai.gonzalezalberquilla@arm.com            print >> sys.stderr, "\t%s" % f
11511716Srekai.gonzalezalberquilla@arm.com        print >> sys.stderr, "Please `git --add' them"
11611407Sandreas.sandberg@arm.com    sys.exit(1)
117