handle %license
[platform/upstream/rpmlint.git] / Filter.py
1 # -*- coding: utf-8 -*-
2 #############################################################################
3 # File          : Filter.py
4 # Package       : rpmlint
5 # Author        : Frederic Lepied
6 # Created on    : Sat Oct 23 15:52:27 1999
7 # Version       : $Id: Filter.py 1871 2011-06-18 09:40:52Z scop $
8 # Purpose       : filter the output of rpmlint to allow exceptions.
9 #############################################################################
10
11 import locale
12 import sys
13 import textwrap
14
15 import Config
16 try:
17     import Testing
18 except ImportError:
19     Testing = None
20
21
22 _rawout = None
23 _diagnostic = list()
24 _badness_score = 0
25 printed_messages = { "I": 0, "W": 0, "E": 0 }
26
27 def __print(s):
28     print(s)
29
30 def printInfo(pkg, reason, *details):
31     _print("I", pkg, reason, details)
32
33 def printWarning(pkg, reason, *details):
34     _print("W", pkg, reason, details)
35
36 def printError(pkg, reason, *details):
37     _print("E", pkg, reason, details)
38
39 def _print(msgtype, pkg, reason, details):
40     global _badness_score
41
42     threshold = badnessThreshold()
43
44     badness = 0
45     if threshold >= 0:
46         badness = Config.badness(reason)
47         # anything with badness is an error
48         if badness:
49             msgtype = 'E'
50         # errors without badness become warnings
51         elif msgtype == 'E':
52             msgtype = 'W'
53
54     ln = ""
55     if pkg.current_linenum is not None:
56         ln = "%s:" % pkg.current_linenum
57     arch = ""
58     if pkg.arch is not None:
59         arch = ".%s" % pkg.arch
60     s = "%s%s:%s %s: %s" % (pkg.name, arch, ln, msgtype, reason)
61     if badness:
62         s = s + " (Badness: %d)" % badness
63     for d in details:
64         s = s + " %s" % d
65     if Testing and Testing.isTest():
66         Testing.addOutput(s)
67     else:
68         if _rawout:
69             print >>_rawout, s.encode(locale.getpreferredencoding(), "replace")
70         if not Config.isFiltered(s):
71             printed_messages[msgtype] += 1
72             _badness_score += badness
73             if threshold >= 0:
74                 _diagnostic.append(s + "\n")
75             else:
76                 __print(s)
77                 if Config.info:
78                     printDescriptions(reason)
79             return True
80
81     return False
82
83 def printDescriptions(reason):
84     try:
85         d = _details[reason]
86         if d and d != '' and d != "\n":
87             __print(textwrap.fill(d, 78))
88             __print("")
89     except KeyError:
90         pass
91
92 def _diag_sortkey(x):
93     xs = x.split()
94     return (xs[2], xs[1])
95
96 def printAllReasons():
97     threshold = badnessThreshold()
98     if threshold < 0:
99         return False
100
101     global _diagnostic
102     _diagnostic.sort(key = _diag_sortkey, reverse = True)
103     last_reason = ''
104     for diag in _diagnostic:
105         if Config.info:
106             reason = diag.split()[2]
107             if reason != last_reason:
108                 if len(last_reason):
109                     printDescriptions(last_reason)
110                 last_reason = reason
111         __print(diag[:-1])
112     if Config.info and len(last_reason):
113         printDescriptions(last_reason)
114     _diagnostic = list()
115     return _badness_score > threshold
116
117
118 _details = {}
119
120 def addDetails(*details):
121     for idx in range(len(details)/2):
122         if not details[idx*2] in _details:
123             _details[details[idx*2]] = details[idx*2+1]
124
125 def badnessScore():
126     return _badness_score
127
128 def badnessThreshold():
129     return Config.getOption("BadnessThreshold", -1)
130
131 def setRawOut(file):
132     global _rawout
133     if _rawout:
134         _rawout.close()
135     _rawout = open(file, "w")
136
137 # Filter.py ends here
138
139 # Local variables:
140 # indent-tabs-mode: nil
141 # py-indent-offset: 4
142 # End:
143 # ex: ts=4 sw=4 et