blob: ec55e799cfd7ae7d1287ac8f43eff2a9c1860fc1 [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 Frysingerae409522014-02-01 03:16:11 -05005"""Common errors thrown when repo presubmit checks fail."""
6
Ryan Cui1562fb82011-05-09 11:01:31 -07007import re
8import sys
9
10
11class VerifyException(Exception):
Mike Frysingerae409522014-02-01 03:16:11 -050012 """Basic sanity checks failed."""
Ryan Cui1562fb82011-05-09 11:01:31 -070013
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
Daniel Erat9d203ff2015-02-17 10:12:21 -070021 def __str__(self):
22 return _FormatHookFailure(self)
23
Ryan Cui1562fb82011-05-09 11:01:31 -070024
25_INDENT = ' ' * 4
26_PROJECT_INFO = 'Errors in PROJECT *%s*!'
27
Mike Frysingerae409522014-02-01 03:16:11 -050028
Ryan Cui1562fb82011-05-09 11:01:31 -070029def _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 Frysinger09d6a3d2013-10-08 22:21:03 -040039 print(msg, file=sys.stderr)
Ryan Cui1562fb82011-05-09 11:01:31 -070040
41
Ryan Cui1562fb82011-05-09 11:01:31 -070042def _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)
Mike Frysinger09d6a3d2013-10-08 22:21:03 -040060 print('', file=sys.stderr)
Ryan Cui1562fb82011-05-09 11:01:31 -070061
62
Mike Frysingered1b95a2019-12-12 19:04:51 -050063def PrintErrorsForCommit(color, hook, project, error_list):
Ryan Cui1562fb82011-05-09 11:01:31 -070064 """Prints the hook error to stderr with project and commit context
65
Ryan Cui1562fb82011-05-09 11:01:31 -070066 Args:
Mike Frysingered1b95a2019-12-12 19:04:51 -050067 color: terminal.Color object for colorizing output.
68 hook: Hook function that generated these errors.
Ryan Cui1562fb82011-05-09 11:01:31 -070069 project: project name
Ryan Cui1562fb82011-05-09 11:01:31 -070070 error_list: a list of HookFailure instances
71 """
Mike Frysingered1b95a2019-12-12 19:04:51 -050072 print('[%s] %s: %s' %
73 (color.Color(color.RED, 'FAILED'), project, hook.__name__))
Ryan Cui1562fb82011-05-09 11:01:31 -070074
75 for error in error_list:
Mike Frysingered1b95a2019-12-12 19:04:51 -050076 _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)