Changeset View
Changeset View
Standalone View
Standalone View
kde-modules/httpcheck.py
- This file was added.
Property | Old Value | New Value |
---|---|---|
File Mode | null | 100755 |
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.gnu.org/copyleft/gpl.html", # Technically this is an issue, but all license headers have this url in place and hiding the real issues. | ||||
54 | 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 | ||||
55 | ] | ||||
56 | | ||||
57 | class HTTPChecker: | ||||
58 | def __init__(self, nonmatches): | ||||
59 | self.nonmatches = nonmatches | ||||
60 | self.reNonmatches = list(map(lambda r:re.compile(r,re.I), nonmatches)) | ||||
61 | self.matcher = re.compile(r'http:', re.I) | ||||
62 | | ||||
63 | def checkFile(self, fpath): | ||||
64 | findings = [] | ||||
65 | with open(fpath, 'r') as f: | ||||
66 | if fpath.startswith('./'): | ||||
67 | fpath = fpath[2:] | ||||
68 | linenumber = 0 | ||||
69 | for line in f: | ||||
70 | linenumber += 1 | ||||
71 | m = self.matcher.search(line) | ||||
72 | if m: | ||||
73 | if not any(map(lambda r: r.search(line), self.reNonmatches)): | ||||
74 | print("{}:{}:\t{}".format(fpath,linenumber,line.strip())) | ||||
75 | findings.append((linenumber, line)) | ||||
76 | return findings | ||||
77 | | ||||
78 | def main(): | ||||
79 | nonmatches = NONMATCHES.copy() | ||||
80 | overwrites = {} | ||||
81 | path = "." | ||||
82 | if len(sys.argv) > 1: | ||||
83 | path = sys.argv[1] | ||||
84 | try: | ||||
85 | with open(os.path.join(path, '.httpcheck-overwrite')) as f: | ||||
86 | for line in f: | ||||
87 | nonmatches.append(line) | ||||
88 | except FileNotFoundError: | ||||
89 | pass | ||||
90 | checker = HTTPChecker(nonmatches) | ||||
91 | findings = {} | ||||
92 | for dirpath, dirnames, filenames in os.walk(path): | ||||
93 | parts = dirpath.split("/") | ||||
94 | if any(map(lambda p: p in parts, ['.git', 'tests', 'autotests'])): | ||||
95 | continue | ||||
96 | for fname in filenames: | ||||
97 | try: | ||||
98 | fpath = os.path.join(dirpath, fname) | ||||
99 | issues = checker.checkFile(fpath) | ||||
100 | if issues: | ||||
101 | findings[fpath] = issues | ||||
102 | except UnicodeDecodeError: | ||||
103 | pass | ||||
104 | if findings: | ||||
105 | sys.exit(1) | ||||
106 | else: | ||||
107 | sys.exit(0) | ||||
108 | | ||||
109 | | ||||
110 | | ||||
111 | if __name__ == "__main__": | ||||
112 | main() |