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