blob: 398223a5cbf6cfa5abdcc58f381958fa32a36b8d [file] [log] [blame]
Ryan Cui1562fb82011-05-09 11:01:31 -07001# 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 Frysinger09d6a3d2013-10-08 22:21:03 -04005from __future__ import print_function
6
Ryan Cui1562fb82011-05-09 11:01:31 -07007import re
8import sys
9
10
11class VerifyException(Exception):
12 pass
13
14
15class 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
25def _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 Frysinger09d6a3d2013-10-08 22:21:03 -040035 print(msg, file=sys.stderr)
Ryan Cui1562fb82011-05-09 11:01:31 -070036
37
38def _FormatCommitDesc(desc):
39 """Returns the properly prefixed commit description."""
40 regex = re.compile(r'^', re.M)
41 return regex.sub('>', desc)
42
43
44def _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
53def 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 Frysinger09d6a3d2013-10-08 22:21:03 -040062 print('', file=sys.stderr)
Ryan Cui1562fb82011-05-09 11:01:31 -070063
64
65def 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 Frysinger09d6a3d2013-10-08 22:21:03 -0400103 print('', file=sys.stderr)