• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Common errors thrown when repo preupload checks fail."""
16
17import os
18import sys
19
20_path = os.path.realpath(__file__ + '/../..')
21if sys.path[0] != _path:
22    sys.path.insert(0, _path)
23del _path
24
25
26class HookResult(object):
27    """A single hook result."""
28
29    def __init__(self, hook, project, commit, error, files=(), fixup_func=None):
30        """Initialize.
31
32        Args:
33          hook: The name of the hook.
34          project: The name of the project.
35          commit: The git commit sha.
36          error: A string representation of the hook's result.  Empty on
37              success.
38          files: The list of files that were involved in the hook execution.
39          fixup_func: A callable that will attempt to automatically fix errors
40              found in the hook's execution.  Returns an non-empty string if
41              this, too, fails.  Can be None if the hook does not support
42              automatically fixing errors.
43        """
44        self.hook = hook
45        self.project = project
46        self.commit = commit
47        self.error = error
48        self.files = files
49        self.fixup_func = fixup_func
50
51    def __bool__(self):
52        return bool(self.error)
53
54    # pylint: disable=nonzero-method
55    def __nonzero__(self):
56        """Python 2/3 glue."""
57        return self.__bool__()
58
59    def is_warning(self):
60        return False
61
62
63class HookCommandResult(HookResult):
64    """A single hook result based on a CompletedProcess."""
65
66    def __init__(self, hook, project, commit, result, files=(),
67                 fixup_func=None):
68        HookResult.__init__(self, hook, project, commit,
69                            result.stderr if result.stderr else result.stdout,
70                            files=files, fixup_func=fixup_func)
71        self.result = result
72
73    def __bool__(self):
74        return self.result.returncode not in (None, 0)
75
76    def is_warning(self):
77        return self.result.returncode == 77
78