style.py revision 11409
111409Sandreas.sandberg@arm.com#! /usr/bin/env python 211409Sandreas.sandberg@arm.com# 311409Sandreas.sandberg@arm.com# Copyright (c) 2016 ARM Limited 411409Sandreas.sandberg@arm.com# All rights reserved 511409Sandreas.sandberg@arm.com# 611409Sandreas.sandberg@arm.com# The license below extends only to copyright in the software and shall 711409Sandreas.sandberg@arm.com# not be construed as granting a license to any other intellectual 811409Sandreas.sandberg@arm.com# property including but not limited to intellectual property relating 911409Sandreas.sandberg@arm.com# to a hardware implementation of the functionality of the software 1011409Sandreas.sandberg@arm.com# licensed hereunder. You may use the software subject to the license 1111409Sandreas.sandberg@arm.com# terms below provided that you ensure that this notice is replicated 1211409Sandreas.sandberg@arm.com# unmodified and in its entirety in all distributions of the software, 1311409Sandreas.sandberg@arm.com# modified or unmodified, in source code or in binary form. 1411409Sandreas.sandberg@arm.com# 1511409Sandreas.sandberg@arm.com# Redistribution and use in source and binary forms, with or without 1611409Sandreas.sandberg@arm.com# modification, are permitted provided that the following conditions are 1711409Sandreas.sandberg@arm.com# met: redistributions of source code must retain the above copyright 1811409Sandreas.sandberg@arm.com# notice, this list of conditions and the following disclaimer; 1911409Sandreas.sandberg@arm.com# redistributions in binary form must reproduce the above copyright 2011409Sandreas.sandberg@arm.com# notice, this list of conditions and the following disclaimer in the 2111409Sandreas.sandberg@arm.com# documentation and/or other materials provided with the distribution; 2211409Sandreas.sandberg@arm.com# neither the name of the copyright holders nor the names of its 2311409Sandreas.sandberg@arm.com# contributors may be used to endorse or promote products derived from 2411409Sandreas.sandberg@arm.com# this software without specific prior written permission. 2511409Sandreas.sandberg@arm.com# 2611409Sandreas.sandberg@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2711409Sandreas.sandberg@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2811409Sandreas.sandberg@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2911409Sandreas.sandberg@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3011409Sandreas.sandberg@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3111409Sandreas.sandberg@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3211409Sandreas.sandberg@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3311409Sandreas.sandberg@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3411409Sandreas.sandberg@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3511409Sandreas.sandberg@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3611409Sandreas.sandberg@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3711409Sandreas.sandberg@arm.com# 3811409Sandreas.sandberg@arm.com# Authors: Andreas Sandberg 3911409Sandreas.sandberg@arm.com 4011409Sandreas.sandberg@arm.comimport os 4111409Sandreas.sandberg@arm.comimport sys 4211409Sandreas.sandberg@arm.com 4311409Sandreas.sandberg@arm.comfrom style.file_types import lang_type 4411409Sandreas.sandberg@arm.comfrom style.verifiers import all_verifiers 4511409Sandreas.sandberg@arm.comfrom style.region import all_regions 4611409Sandreas.sandberg@arm.com 4711409Sandreas.sandberg@arm.comfrom style.style import StdioUI 4811409Sandreas.sandberg@arm.comfrom style import repo 4911409Sandreas.sandberg@arm.com 5011409Sandreas.sandberg@arm.comdef verify(filename, regions=all_regions, verbose=False): 5111409Sandreas.sandberg@arm.com ui = StdioUI() 5211409Sandreas.sandberg@arm.com opts = {} 5311409Sandreas.sandberg@arm.com base = os.path.join(os.path.dirname(__file__), "..") 5411409Sandreas.sandberg@arm.com verifiers = [ v(ui, opts, base=base) for v in all_verifiers ] 5511409Sandreas.sandberg@arm.com 5611409Sandreas.sandberg@arm.com if verbose: 5711409Sandreas.sandberg@arm.com print "Verifying %s[%s]..." % (filename, regions) 5811409Sandreas.sandberg@arm.com for verifier in verifiers: 5911409Sandreas.sandberg@arm.com if verbose: 6011409Sandreas.sandberg@arm.com print "Applying %s (%s)" % ( 6111409Sandreas.sandberg@arm.com verifier.test_name, verifier.__class__.__name__) 6211409Sandreas.sandberg@arm.com if verifier.apply(filename, regions=regions): 6311409Sandreas.sandberg@arm.com return False 6411409Sandreas.sandberg@arm.com return True 6511409Sandreas.sandberg@arm.com 6611409Sandreas.sandberg@arm.comdef detect_repo(): 6711409Sandreas.sandberg@arm.com repo_classes = repo.detect_repo() 6811409Sandreas.sandberg@arm.com if not repo_classes: 6911409Sandreas.sandberg@arm.com print >> sys.stderr, "Error: Failed to detect repository type, no " \ 7011409Sandreas.sandberg@arm.com "known repository type found." 7111409Sandreas.sandberg@arm.com sys.exit(1) 7211409Sandreas.sandberg@arm.com elif len(repo_classes) > 1: 7311409Sandreas.sandberg@arm.com print >> sys.stderr, "Error: Detected multiple repository types." 7411409Sandreas.sandberg@arm.com sys.exit(1) 7511409Sandreas.sandberg@arm.com else: 7611409Sandreas.sandberg@arm.com return repo_classes[0]() 7711409Sandreas.sandberg@arm.com 7811409Sandreas.sandberg@arm.comrepo_types = { 7911409Sandreas.sandberg@arm.com "auto" : detect_repo, 8011409Sandreas.sandberg@arm.com "none" : lambda : None, 8111409Sandreas.sandberg@arm.com "git" : repo.GitRepo, 8211409Sandreas.sandberg@arm.com "hg" : repo.MercurialRepo, 8311409Sandreas.sandberg@arm.com} 8411409Sandreas.sandberg@arm.com 8511409Sandreas.sandberg@arm.comif __name__ == '__main__': 8611409Sandreas.sandberg@arm.com import argparse 8711409Sandreas.sandberg@arm.com 8811409Sandreas.sandberg@arm.com parser = argparse.ArgumentParser( 8911409Sandreas.sandberg@arm.com description="Check a file for gem5 style violations", 9011409Sandreas.sandberg@arm.com epilog="""If no files are specified, the style checker tries to 9111409Sandreas.sandberg@arm.com determine the list of modified and added files from the version 9211409Sandreas.sandberg@arm.com control system and checks those.""" 9311409Sandreas.sandberg@arm.com ) 9411409Sandreas.sandberg@arm.com 9511409Sandreas.sandberg@arm.com parser.add_argument("--verbose", "-v", action="count", 9611409Sandreas.sandberg@arm.com help="Produce verbose output") 9711409Sandreas.sandberg@arm.com 9811409Sandreas.sandberg@arm.com parser.add_argument("--modifications", "-m", action="store_true", 9911409Sandreas.sandberg@arm.com help="""Apply the style checker to modified regions 10011409Sandreas.sandberg@arm.com instead of whole files""") 10111409Sandreas.sandberg@arm.com 10211409Sandreas.sandberg@arm.com parser.add_argument("--repo-type", choices=repo_types, default="auto", 10311409Sandreas.sandberg@arm.com help="Repository type to use to detect changes") 10411409Sandreas.sandberg@arm.com 10511409Sandreas.sandberg@arm.com parser.add_argument("files", metavar="FILE", nargs="*", 10611409Sandreas.sandberg@arm.com type=str, 10711409Sandreas.sandberg@arm.com help="Source file(s) to inspect") 10811409Sandreas.sandberg@arm.com 10911409Sandreas.sandberg@arm.com args = parser.parse_args() 11011409Sandreas.sandberg@arm.com 11111409Sandreas.sandberg@arm.com repo = repo_types[args.repo_type]() 11211409Sandreas.sandberg@arm.com 11311409Sandreas.sandberg@arm.com files = args.files 11411409Sandreas.sandberg@arm.com if not files and repo: 11511409Sandreas.sandberg@arm.com added, modified = repo.staged_files() 11611409Sandreas.sandberg@arm.com files = [ repo.file_path(f) for f in added + modified ] 11711409Sandreas.sandberg@arm.com 11811409Sandreas.sandberg@arm.com for filename in files: 11911409Sandreas.sandberg@arm.com if args.modifications and repo and repo.in_repo(filename): 12011409Sandreas.sandberg@arm.com regions = repo.modified_regions(filename) 12111409Sandreas.sandberg@arm.com else: 12211409Sandreas.sandberg@arm.com regions = all_regions 12311409Sandreas.sandberg@arm.com 12411409Sandreas.sandberg@arm.com if not verify(filename, regions=regions, 12511409Sandreas.sandberg@arm.com verbose=args.verbose): 12611409Sandreas.sandberg@arm.com sys.exit(1) 127