blob: 35ede84ce65c17fc61756c17078fe7685d4ff33e [file] [log] [blame]
Mike Frysinger56e8de02019-07-31 14:40:14 -04001# -*- coding: utf-8 -*-
Ryan Cui1562fb82011-05-09 11:01:31 -07002# 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 Frysingerae409522014-02-01 03:16:11 -05006"""Common errors thrown when repo presubmit checks fail."""
7
Mike Frysinger09d6a3d2013-10-08 22:21:03 -04008from __future__ import print_function
9
Ryan Cui1562fb82011-05-09 11:01:31 -070010import re
11import sys
12
13
14class VerifyException(Exception):
Mike Frysingerae409522014-02-01 03:16:11 -050015 """Basic sanity checks failed."""
Ryan Cui1562fb82011-05-09 11:01:31 -070016
17
18class 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 Erat9d203ff2015-02-17 10:12:21 -070024 def __str__(self):
25 return _FormatHookFailure(self)
26
Ryan Cui1562fb82011-05-09 11:01:31 -070027
28_INDENT = ' ' * 4
29_PROJECT_INFO = 'Errors in PROJECT *%s*!'
30
Mike Frysingerae409522014-02-01 03:16:11 -050031
Ryan Cui1562fb82011-05-09 11:01:31 -070032def _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 Frysinger09d6a3d2013-10-08 22:21:03 -040042 print(msg, file=sys.stderr)
Ryan Cui1562fb82011-05-09 11:01:31 -070043
44
Ryan Cui1562fb82011-05-09 11:01:31 -070045def _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
54def 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 Frysinger09d6a3d2013-10-08 22:21:03 -040063 print('', file=sys.stderr)
Ryan Cui1562fb82011-05-09 11:01:31 -070064
65
Mike Frysingered1b95a2019-12-12 19:04:51 -050066def PrintErrorsForCommit(color, hook, project, error_list):
Ryan Cui1562fb82011-05-09 11:01:31 -070067 """Prints the hook error to stderr with project and commit context
68
Ryan Cui1562fb82011-05-09 11:01:31 -070069 Args:
Mike Frysingered1b95a2019-12-12 19:04:51 -050070 color: terminal.Color object for colorizing output.
71 hook: Hook function that generated these errors.
Ryan Cui1562fb82011-05-09 11:01:31 -070072 project: project name
Ryan Cui1562fb82011-05-09 11:01:31 -070073 error_list: a list of HookFailure instances
74 """
Mike Frysingered1b95a2019-12-12 19:04:51 -050075 print('[%s] %s: %s' %
76 (color.Color(color.RED, 'FAILED'), project, hook.__name__))
Ryan Cui1562fb82011-05-09 11:01:31 -070077
78 for error in error_list:
Mike Frysingered1b95a2019-12-12 19:04:51 -050079 _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)