Changeset View
Changeset View
Standalone View
Standalone View
kde-modules/httpcheck.py
- This file was added.
1 | #!/usr/bin/env python3 | ||||
---|---|---|---|---|---|
2 | | ||||
3 | # Copyright 2019 Sandro Knauß <sknauss@kde.org> | ||||
4 | # Copyright 2019 Volker Krause <vkrause@kde.org> | ||||
5 | # | ||||
6 | # Redistribution and use in source and binary forms, with or without | ||||
7 | # modification, are permitted provided that the following conditions | ||||
8 | # are met: | ||||
9 | # | ||||
10 | # 1. Redistributions of source code must retain the copyright | ||||
11 | # notice, this list of conditions and the following disclaimer. | ||||
12 | # 2. Redistributions in binary form must reproduce the copyright | ||||
13 | # notice, this list of conditions and the following disclaimer in the | ||||
14 | # documentation and/or other materials provided with the distribution. | ||||
15 | # 3. The name of the author may not be used to endorse or promote products | ||||
16 | # derived from this software without specific prior written permission. | ||||
17 | # | ||||
18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||||
19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||||
20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||||
21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||||
22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||||
23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||||
26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||||
27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
28 | | ||||
29 | import json | ||||
30 | import os | ||||
31 | import re | ||||
32 | import sys | ||||
33 | | ||||
34 | NONMATCHES = [ | ||||
35 | r"HTTPSCHECK_IGNORE: true", # manual ignore a line. | ||||
36 | r"http://www.kde.org/standards/kcfg/", | ||||
37 | r"http://www.kde.org/standards/kxmlgui/", | ||||
38 | r"http://www.freedesktop.org/standards/", | ||||
39 | r"http://www.w3.org/", | ||||
40 | r"xmlns", | ||||
41 | r"rdf:resource", | ||||
42 | r"!DOCTYPE", | ||||
43 | r"http://www.inkscape.org/\) -->", | ||||
44 | r"http://purl.org/", | ||||
45 | r"http://schema.org", | ||||
46 | r"http://xml.org/sax/", | ||||
47 | r"schemas.google.com", | ||||
48 | r"schemas.microsoft.com", | ||||
49 | r"semanticdesktop.org/onto", | ||||
50 | r"http://nepomuk.kde.org/ontologies/", | ||||
51 | r"Generator: DocBook XSL Stylesheets .* <http://docbook.sf.net/>", | ||||
52 | r"http://www.gnu.org/licenses/", # Technically this is an issue, but all license headers have this url in place and hiding the real issues. | ||||
53 | r"http://www.qt-project.org/legal/", # Technically this is an issue, but we refer it a lot in headers, as we copy a lot form Qt | ||||
54 | ] | ||||
55 | | ||||
56 | class HTTPChecker: | ||||
57 | def __init__(self, nonmatches): | ||||
58 | self.nonmatches = nonmatches | ||||
59 | self.reNonmatches = list(map(lambda r:re.compile(r,re.I), nonmatches)) | ||||
60 | self.matcher = re.compile(r'http', re.I) | ||||
61 | | ||||
62 | def checkFile(self, fpath): | ||||
63 | findings = [] | ||||
64 | with open(fpath, 'r') as f: | ||||
65 | if fpath.startswith('./'): | ||||
66 | fpath = fpath[2:] | ||||
67 | linenumber = 0 | ||||
68 | for line in f: | ||||
69 | linenumber += 1 | ||||
70 | m = self.matcher.search(line) | ||||
71 | if m: | ||||
72 | if not any(map(lambda r: r.search(line), self.reNonmatches)): | ||||
73 | print("{}:{}:\t{}".format(fpath,linenumber,line.strip())) | ||||
74 | findings.append((linenumber, line)) | ||||
75 | return findings | ||||
76 | | ||||
77 | def main(): | ||||
78 | nonmatches = NONMATCHES.copy() | ||||
79 | overwrites = {} | ||||
80 | path = "." | ||||
81 | if len(sys.argv) > 1: | ||||
82 | path = sys.argv[1] | ||||
83 | try: | ||||
84 | with open(os.path.join(path, '.httpcheck-overwrite')) as f: | ||||
85 | for line in f: | ||||
86 | nonmatches.append(line) | ||||
87 | except FileNotFoundError: | ||||
88 | pass | ||||
89 | checker = HTTPChecker(nonmatches) | ||||
90 | findings = {} | ||||
91 | for dirpath, dirnames, filenames in os.walk(path): | ||||
92 | parts = dirpath.split("/") | ||||
93 | if any(map(lambda p: p in parts, ['.git', 'tests', 'autotests'])): | ||||
94 | continue | ||||
95 | for fname in filenames: | ||||
96 | try: | ||||
97 | fpath = os.path.join(dirpath, fname) | ||||
98 | issues = checker.checkFile(fpath) | ||||
99 | if issues: | ||||
100 | findings[fpath] = issues | ||||
101 | except UnicodeDecodeError: | ||||
102 | pass | ||||
103 | if findings: | ||||
104 | sys.exit(1) | ||||
105 | else: | ||||
106 | sys.exit(0) | ||||
107 | | ||||
108 | | ||||
109 | | ||||
110 | if __name__ == "__main__": | ||||
111 | main() |