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 | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame^] | 5 | from __future__ import print_function |
| 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): |
| 12 | pass |
| 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 | |
| 21 | |
| 22 | _INDENT = ' ' * 4 |
| 23 | _PROJECT_INFO = 'Errors in PROJECT *%s*!' |
| 24 | |
| 25 | def _PrintWithIndent(msg, indent_level): |
| 26 | """Print a block of text with a specified indent level to stderr. |
| 27 | |
| 28 | Args: |
| 29 | msg: A string to print (may contain newlines). |
| 30 | indent_level: The number of indents to prefix each line with. Each indent |
| 31 | is four characters wide. |
| 32 | """ |
| 33 | regex = re.compile(r'^', re.M) |
| 34 | msg = regex.sub(_INDENT * indent_level, msg) |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame^] | 35 | print(msg, file=sys.stderr) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | def _FormatCommitDesc(desc): |
| 39 | """Returns the properly prefixed commit description.""" |
| 40 | regex = re.compile(r'^', re.M) |
| 41 | return regex.sub('>', desc) |
| 42 | |
| 43 | |
| 44 | def _FormatHookFailure(hook_failure): |
| 45 | """Returns the properly formatted VerifyException as a string.""" |
| 46 | item_prefix = '\n%s* ' % _INDENT |
| 47 | formatted_items = '' |
| 48 | if hook_failure.items: |
| 49 | formatted_items = item_prefix + item_prefix.join(hook_failure.items) |
| 50 | return '* ' + hook_failure.msg + formatted_items |
| 51 | |
| 52 | |
| 53 | def PrintErrorForProject(project, error): |
| 54 | """Prints the project and its error. |
| 55 | |
| 56 | Args: |
| 57 | project: project name |
| 58 | error: An instance of the HookFailure class |
| 59 | """ |
| 60 | _PrintWithIndent(_PROJECT_INFO % project, 0) |
| 61 | _PrintWithIndent(_FormatHookFailure(error), 1) |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame^] | 62 | print('', file=sys.stderr) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 63 | |
| 64 | |
| 65 | def PrintErrorsForCommit(project, commit, commit_desc, error_list): |
| 66 | """Prints the hook error to stderr with project and commit context |
| 67 | |
| 68 | A sample error output for a project would be: |
| 69 | ---------------------------------------------------------------------------- |
| 70 | Errors in PROJECT *chromiumos/repohooks*! |
| 71 | COMMIT 10041758: |
| 72 | Description: |
| 73 | >staged |
| 74 | > |
| 75 | >TEST=some |
| 76 | >Change-Id: I2c4f545a20a659541c02be16aa9dc440c876a604 |
| 77 | > |
| 78 | Errors: |
| 79 | * Changelist description needs BUG field (after first line) |
| 80 | * Found line ending with white space in: |
| 81 | * src/repohooks/pre-upload.py, line 307 |
| 82 | * Found lines longer than 80 characters (first 5 shown): |
| 83 | * src/repohooks/pre-upload.py, line 335, 85 chars |
| 84 | ---------------------------------------------------------------------------- |
| 85 | |
| 86 | Args: |
| 87 | project: project name |
| 88 | commit: the commit hash the errors belong to |
| 89 | commit_desc: a string containing the commit message |
| 90 | error_list: a list of HookFailure instances |
| 91 | """ |
| 92 | _PrintWithIndent(_PROJECT_INFO % project, 0) |
| 93 | |
| 94 | formatted_desc = _FormatCommitDesc(commit_desc) |
| 95 | _PrintWithIndent('COMMIT %s:' % commit[:8], 1) |
| 96 | _PrintWithIndent('Description:', 2) |
| 97 | _PrintWithIndent(formatted_desc, 3) |
| 98 | _PrintWithIndent('Errors:', 2) |
| 99 | |
| 100 | for error in error_list: |
| 101 | _PrintWithIndent(_FormatHookFailure(error), 3) |
| 102 | |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame^] | 103 | print('', file=sys.stderr) |