Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS 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 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 5 | """Common errors thrown when repo presubmit checks fail.""" |
| 6 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 7 | import re |
| 8 | import sys |
| 9 | |
| 10 | |
| 11 | class VerifyException(Exception): |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 12 | """Basic sanity checks failed.""" |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 13 | |
| 14 | |
| 15 | class HookFailure(object): |
| 16 | """Contains an error message and a list of error details.""" |
| 17 | def __init__(self, msg, items=None): |
| 18 | self.msg = msg |
| 19 | self.items = items |
| 20 | |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 21 | def __str__(self): |
| 22 | return _FormatHookFailure(self) |
| 23 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 24 | |
| 25 | _INDENT = ' ' * 4 |
| 26 | _PROJECT_INFO = 'Errors in PROJECT *%s*!' |
| 27 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 28 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 29 | def _PrintWithIndent(msg, indent_level): |
| 30 | """Print a block of text with a specified indent level to stderr. |
| 31 | |
| 32 | Args: |
| 33 | msg: A string to print (may contain newlines). |
| 34 | indent_level: The number of indents to prefix each line with. Each indent |
| 35 | is four characters wide. |
| 36 | """ |
| 37 | regex = re.compile(r'^', re.M) |
| 38 | msg = regex.sub(_INDENT * indent_level, msg) |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame] | 39 | print(msg, file=sys.stderr) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 40 | |
| 41 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 42 | def _FormatHookFailure(hook_failure): |
| 43 | """Returns the properly formatted VerifyException as a string.""" |
| 44 | item_prefix = '\n%s* ' % _INDENT |
| 45 | formatted_items = '' |
| 46 | if hook_failure.items: |
| 47 | formatted_items = item_prefix + item_prefix.join(hook_failure.items) |
| 48 | return '* ' + hook_failure.msg + formatted_items |
| 49 | |
| 50 | |
| 51 | def PrintErrorForProject(project, error): |
| 52 | """Prints the project and its error. |
| 53 | |
| 54 | Args: |
| 55 | project: project name |
| 56 | error: An instance of the HookFailure class |
| 57 | """ |
| 58 | _PrintWithIndent(_PROJECT_INFO % project, 0) |
| 59 | _PrintWithIndent(_FormatHookFailure(error), 1) |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame] | 60 | print('', file=sys.stderr) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 61 | |
| 62 | |
Mike Frysinger | ed1b95a | 2019-12-12 19:04:51 -0500 | [diff] [blame] | 63 | def PrintErrorsForCommit(color, hook, project, error_list): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 64 | """Prints the hook error to stderr with project and commit context |
| 65 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 66 | Args: |
Mike Frysinger | ed1b95a | 2019-12-12 19:04:51 -0500 | [diff] [blame] | 67 | color: terminal.Color object for colorizing output. |
| 68 | hook: Hook function that generated these errors. |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 69 | project: project name |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 70 | error_list: a list of HookFailure instances |
| 71 | """ |
Mike Frysinger | ed1b95a | 2019-12-12 19:04:51 -0500 | [diff] [blame] | 72 | print('[%s] %s: %s' % |
| 73 | (color.Color(color.RED, 'FAILED'), project, hook.__name__)) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 74 | |
| 75 | for error in error_list: |
Mike Frysinger | ed1b95a | 2019-12-12 19:04:51 -0500 | [diff] [blame] | 76 | _PrintWithIndent(error.msg.strip(), 1) |
| 77 | if error.items: |
| 78 | for item in error.items: |
| 79 | _PrintWithIndent(item.strip(), 1) |
| 80 | print('', file=sys.stderr) |