blob: 198d0e2d97e4dc18d78f5b140014941bb384e703 [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
5import re
6import sys
7
8
9class VerifyException(Exception):
10 pass
11
12
13class HookFailure(object):
14 """Contains an error message and a list of error details."""
15 def __init__(self, msg, items=None):
16 self.msg = msg
17 self.items = items
18
19
20_INDENT = ' ' * 4
21_PROJECT_INFO = 'Errors in PROJECT *%s*!'
22
23def _PrintWithIndent(msg, indent_level):
24 """Print a block of text with a specified indent level to stderr.
25
26 Args:
27 msg: A string to print (may contain newlines).
28 indent_level: The number of indents to prefix each line with. Each indent
29 is four characters wide.
30 """
31 regex = re.compile(r'^', re.M)
32 msg = regex.sub(_INDENT * indent_level, msg)
33 print >> sys.stderr, msg
34
35
36def _FormatCommitDesc(desc):
37 """Returns the properly prefixed commit description."""
38 regex = re.compile(r'^', re.M)
39 return regex.sub('>', desc)
40
41
42def _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
51def 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)
60 print >> sys.stderr, ''
61
62
63def PrintErrorsForCommit(project, commit, commit_desc, error_list):
64 """Prints the hook error to stderr with project and commit context
65
66 A sample error output for a project would be:
67 ----------------------------------------------------------------------------
68 Errors in PROJECT *chromiumos/repohooks*!
69 COMMIT 10041758:
70 Description:
71 >staged
72 >
73 >TEST=some
74 >Change-Id: I2c4f545a20a659541c02be16aa9dc440c876a604
75 >
76 Errors:
77 * Changelist description needs BUG field (after first line)
78 * Found line ending with white space in:
79 * src/repohooks/pre-upload.py, line 307
80 * Found lines longer than 80 characters (first 5 shown):
81 * src/repohooks/pre-upload.py, line 335, 85 chars
82 ----------------------------------------------------------------------------
83
84 Args:
85 project: project name
86 commit: the commit hash the errors belong to
87 commit_desc: a string containing the commit message
88 error_list: a list of HookFailure instances
89 """
90 _PrintWithIndent(_PROJECT_INFO % project, 0)
91
92 formatted_desc = _FormatCommitDesc(commit_desc)
93 _PrintWithIndent('COMMIT %s:' % commit[:8], 1)
94 _PrintWithIndent('Description:', 2)
95 _PrintWithIndent(formatted_desc, 3)
96 _PrintWithIndent('Errors:', 2)
97
98 for error in error_list:
99 _PrintWithIndent(_FormatHookFailure(error), 3)
100
101 print >> sys.stderr, ''
102