| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Makes sure that injected JavaScript is gjslint clean.""" |
| 6 |
| 7 # Regular expression patterns for JavaScript source files. |
| 8 INCLUDE_JS_FILES_ONLY = ( |
| 9 r'.*\.js$', |
| 10 ) |
| 11 |
| 12 # List of standard locations where gjslint may be installed. |
| 13 GJSLINT_PATHS = ( |
| 14 '/usr/local/bin/gjslint', |
| 15 '/usr/bin/gjslint', |
| 16 ) |
| 17 |
| 18 def CheckChangeJsLintsClean(input_api, output_api): |
| 19 """Checks for JavaScript lint errors.""" |
| 20 # Gets the list of modified files. |
| 21 source_file_filter = lambda x: input_api.FilterSourceFile( |
| 22 x, white_list=INCLUDE_JS_FILES_ONLY) |
| 23 files = [f.AbsoluteLocalPath() |
| 24 for f in input_api.AffectedSourceFiles(source_file_filter)] |
| 25 results = [] |
| 26 |
| 27 # Run gjslint only if there are JavaScript files changed. |
| 28 if files: |
| 29 # Finds out where gjslint is installed, and warns user |
| 30 # gently about this missing utility. Then treats it as |
| 31 # if there are no JavaScript lint errors. |
| 32 gjslint_path = None |
| 33 for path in GJSLINT_PATHS: |
| 34 if input_api.os_path.exists(path): |
| 35 gjslint_path = path |
| 36 break |
| 37 |
| 38 if not gjslint_path: |
| 39 # Early return if gjslint is not available. |
| 40 return [ output_api.PresubmitNotifyResult( |
| 41 '** WARNING ** gjslint is not installed in your environment.', |
| 42 GJSLINT_PATHS, |
| 43 ('Please see http://code.google.com/' |
| 44 'closure/utilities/docs/linter_howto.html ' |
| 45 'for installation instructions.\n' |
| 46 'You are strongly encouraged to install gjslint ' |
| 47 'because a kitten dies every time you submit code ' |
| 48 'without gjslint :)')) ] |
| 49 |
| 50 # Found gjslint, run it over modified JavaScript files. |
| 51 for file_name in files: |
| 52 lint_error = RunJsLint(input_api, output_api, gjslint_path, file_name) |
| 53 if lint_error is not None: |
| 54 results.append(lint_error) |
| 55 |
| 56 return results |
| 57 |
| 58 def RunJsLint(input_api, output_api, gjslint_path, file_name): |
| 59 """Runs gjslint on a file. |
| 60 |
| 61 Returns: None if there are no lint errors, or an object of type |
| 62 output_api.Presubmit{Error,PromptWarning} if lint errors are found. |
| 63 """ |
| 64 # Do not hinder users from uploading incomplete patches. |
| 65 if input_api.is_committing: |
| 66 message_type = output_api.PresubmitError |
| 67 else: |
| 68 message_type = output_api.PresubmitPromptWarning |
| 69 |
| 70 result = None |
| 71 cmd = [ gjslint_path, file_name ]; |
| 72 try: |
| 73 if input_api.verbose: |
| 74 input_api.subprocess.check_call(cmd, cwd=input_api.PresubmitLocalPath()) |
| 75 else: |
| 76 input_api.subprocess.check_output( |
| 77 cmd, |
| 78 stderr=input_api.subprocess.STDOUT, |
| 79 cwd=input_api.PresubmitLocalPath()) |
| 80 except (OSError, input_api.subprocess.CalledProcessError), e: |
| 81 result = message_type('%s failed.\n%s' % (' '.join(cmd), e)) |
| 82 return result |
| 83 |
| 84 def CheckChangeOnUpload(input_api, output_api): |
| 85 """Special Top level function called by git_cl.""" |
| 86 return CheckChangeJsLintsClean(input_api, output_api) |
| 87 |
| OLD | NEW |