• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2#                     The LLVM Compiler Infrastructure
3#
4# This file is distributed under the University of Illinois Open Source
5# License. See LICENSE.TXT for details.
6"""
7This module responsible to run the Clang static analyzer against any build
8and generate reports.
9"""
10
11
12def duplicate_check(method):
13    """ Predicate to detect duplicated entries.
14
15    Unique hash method can be use to detect duplicates. Entries are
16    represented as dictionaries, which has no default hash method.
17    This implementation uses a set datatype to store the unique hash values.
18
19    This method returns a method which can detect the duplicate values. """
20
21    def predicate(entry):
22        entry_hash = predicate.unique(entry)
23        if entry_hash not in predicate.state:
24            predicate.state.add(entry_hash)
25            return False
26        return True
27
28    predicate.unique = method
29    predicate.state = set()
30    return predicate
31
32
33def tempdir():
34    """ Return the default temorary directory. """
35
36    from os import getenv
37    return getenv('TMPDIR', getenv('TEMP', getenv('TMP', '/tmp')))
38
39
40def initialize_logging(verbose_level):
41    """ Output content controlled by the verbosity level. """
42
43    import sys
44    import os.path
45    import logging
46    level = logging.WARNING - min(logging.WARNING, (10 * verbose_level))
47
48    if verbose_level <= 3:
49        fmt_string = '{0}: %(levelname)s: %(message)s'
50    else:
51        fmt_string = '{0}: %(levelname)s: %(funcName)s: %(message)s'
52
53    program = os.path.basename(sys.argv[0])
54    logging.basicConfig(format=fmt_string.format(program), level=level)
55
56
57def command_entry_point(function):
58    """ Decorator for command entry points. """
59
60    import functools
61    import logging
62
63    @functools.wraps(function)
64    def wrapper(*args, **kwargs):
65
66        exit_code = 127
67        try:
68            exit_code = function(*args, **kwargs)
69        except KeyboardInterrupt:
70            logging.warning('Keyboard interupt')
71        except Exception:
72            logging.exception('Internal error.')
73            if logging.getLogger().isEnabledFor(logging.DEBUG):
74                logging.error("Please report this bug and attach the output "
75                              "to the bug report")
76            else:
77                logging.error("Please run this command again and turn on "
78                              "verbose mode (add '-vvv' as argument).")
79        finally:
80            return exit_code
81
82    return wrapper
83