1#!/usr/bin/python 2# -*- coding:utf-8 -*- 3# Copyright 2016 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17"""Wrapper to run pylint with the right settings.""" 18 19from __future__ import print_function 20 21import argparse 22import errno 23import os 24import sys 25 26 27DEFAULT_PYLINTRC_PATH = os.path.join( 28 os.path.dirname(os.path.realpath(__file__)), 'pylintrc') 29 30 31def get_parser(): 32 """Return a command line parser.""" 33 parser = argparse.ArgumentParser(description=__doc__) 34 parser.add_argument('--init-hook', help='Init hook commands to run.') 35 parser.add_argument('--executable-path', 36 help='The path of the pylint executable.', 37 default='pylint') 38 parser.add_argument('--no-rcfile', 39 help='Specify to use the executable\'s default ' 40 'configuration.', 41 action='store_true') 42 parser.add_argument('files', nargs='+') 43 return parser 44 45 46def main(argv): 47 """The main entry.""" 48 parser = get_parser() 49 opts, unknown = parser.parse_known_args(argv) 50 51 cmd = [opts.executable_path] 52 if not opts.no_rcfile: 53 # We assume pylint is running in the top directory of the project, 54 # so load the pylintrc file from there if it's available. 55 pylintrc = os.path.abspath('pylintrc') 56 if not os.path.exists(pylintrc): 57 pylintrc = DEFAULT_PYLINTRC_PATH 58 # If we pass a non-existent rcfile to pylint, it'll happily ignore 59 # it. 60 assert os.path.exists(pylintrc), 'Could not find %s' % pylintrc 61 cmd += ['--rcfile', pylintrc] 62 63 cmd += unknown + opts.files 64 65 if opts.init_hook: 66 cmd += ['--init-hook', opts.init_hook] 67 68 try: 69 os.execvp(cmd[0], cmd) 70 except OSError as e: 71 if e.errno == errno.ENOENT: 72 print('%s: unable to run `%s`: %s' % (__file__, cmd[0], e), 73 file=sys.stderr) 74 print('%s: Try installing pylint: sudo apt-get install %s' % 75 (__file__, os.path.basename(cmd[0])), file=sys.stderr) 76 return 1 77 else: 78 raise 79 80 81if __name__ == '__main__': 82 sys.exit(main(sys.argv[1:])) 83