Debian Codegraph @

examples/copyright/check_parse_and_dump.py

raw
1 #!/usr/bin/python3
2
3 # vim: fileencoding=utf-8
4 #
5 # Copyright (C) 2014 Google, Inc.
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation, either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21 """Check the format and dumpability of debian/copyright filenames on stdin.
22
23 Usage:
24
25 find /tmp/packages -type f -name copyright | ./check_parse_and_dump.py
26
27 The --suppress_warnings and --summary flags can be used to make the program
28 output less verbose.
29 """
30
31 import argparse
32 import codecs
33 import sys
34 from typing import Any
35 import warnings
36
37 from debian import copyright
38
39
40 def parse_args() -> argparse.Namespace:
41 parser = argparse.ArgumentParser()
42 parser.add_argument('--summary', action='store_true',
43 help='Whether to print only a summary')
44 parser.add_argument('--suppress_warnings', action='store_true',
45 help='Whether to suppress copyright warnings')
46 return parser.parse_args()
47
48
49 def main() -> None:
50 args = parse_args()
51
52 if args.suppress_warnings:
53 warnings.filterwarnings('ignore', module='debian.copyright')
54
55 problems: dict[tuple[int, str], list[tuple[str, Any]]] = {}
56
57 total = 0
58 for filename in sys.stdin:
59 total += 1
60 filename = filename.rstrip()
61 with open(filename, encoding='utf-8') as f:
62 try:
63 c = copyright.Copyright(f)
64 except Exception as e:
65 problems.setdefault((1, 'Parse failures'), []).append(
66 (filename, e))
67 continue
68
69 try:
70 c.dump()
71 except Exception as e:
72 problems.setdefault((2, 'Dump failures'), []).append(
73 (filename, e))
74
75 if not c.header.known_format():
76 problems.setdefault((3, 'Unknown format'), []).append(
77 (filename, c.header.format))
78
79 invalid_globs = []
80 globs_with_leading_dot_slash = []
81 globs_with_trailing_comma = []
82 globs_with_double_star = []
83 for p in c.all_files_paragraphs():
84 try:
85 p.files_pattern()
86 except Exception as e:
87 invalid_globs.append(e)
88 for glob in p.files:
89 if glob.startswith('./'):
90 globs_with_leading_dot_slash.append(glob)
91 if glob.endswith(','):
92 globs_with_trailing_comma.append(glob)
93 if '**' in glob:
94 globs_with_double_star.append(glob)
95
96 if invalid_globs:
97 problems.setdefault((4, 'Invalid glob'), []).append(
98 (filename, invalid_globs))
99 if globs_with_leading_dot_slash:
100 problems.setdefault((5, 'Globs with leading ./'), []).append(
101 (filename, globs_with_leading_dot_slash))
102 if globs_with_trailing_comma:
103 problems.setdefault((6, 'Globs with trailing ,'), []).append(
104 (filename, globs_with_trailing_comma))
105 if globs_with_double_star:
106 problems.setdefault((7, 'Globs with **'), []).append(
107 (filename, globs_with_double_star))
108
109 f_out = codecs.getwriter(encoding='utf-8')(sys.stdout)
110 for (_, heading), details in sorted(problems.items()):
111 f_out.write(f"\n{heading}: ({len(details)} / {total})\n")
112 if not args.summary:
113 for filename, problem in details:
114 f_out.write(f" {filename}: {problem}\n")
115
116
117 if __name__ == '__main__':
118 main()