Home | History | Annotate | Download | only in bin
      1 #!/usr/bin/env python
      2 # Copyright 2016 The Chromium Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 import argparse
      7 import os
      8 import sys
      9 
     10 
     11 _CATAPULT_PATH = os.path.abspath(
     12     os.path.join(os.path.dirname(__file__),
     13                  os.path.pardir, os.path.pardir, os.path.pardir))
     14 
     15 
     16 _ESLINT_PATH = os.path.abspath(
     17     os.path.join(os.path.dirname(__file__), os.path.pardir))
     18 
     19 
     20 DIRECTORIES_TO_LINT = [
     21   os.path.join(_CATAPULT_PATH, 'dashboard', 'dashboard'),
     22   os.path.join(_CATAPULT_PATH, 'tracing', 'tracing')
     23 ]
     24 
     25 
     26 def _AddToPathIfNeeded(path):
     27   if path not in sys.path:
     28     sys.path.insert(0, path)
     29 
     30 
     31 if __name__ == '__main__':
     32   _AddToPathIfNeeded(_ESLINT_PATH)
     33   import eslint
     34 
     35   parser = argparse.ArgumentParser(
     36       description='Wrapper script to run eslint on Catapult code')
     37   parser.add_argument('--paths', '-p', default=None, nargs='+', metavar='PATH',
     38                       help='List of paths to lint')
     39   parser.add_argument('--all', default=None, action='store_true',
     40                       help='Runs eslint on all applicable Catapult code')
     41   parser.add_argument('--extra-args', default=None, type=str,
     42                       help='A string of extra arguments to pass to eslint')
     43 
     44   args = parser.parse_args(sys.argv[1:])
     45   if ((args.paths is not None and args.all is not None) or
     46       (args.paths is None and args.all is None)):
     47     print 'Either --paths or --all must be used, but not both.\n'
     48     parser.print_help()
     49     sys.exit(1)
     50 
     51   paths = DIRECTORIES_TO_LINT if args.all else args.paths
     52   success, output = eslint.RunEslint(paths, extra_args=args.extra_args)
     53   print output
     54   sys.exit(not success)
     55