]>
Commit | Line | Data |
---|---|---|
3e67e5c9 | 1 | #!/usr/bin/env python3 |
35df7443 ZJS |
2 | # SPDX-License-Identifier: LGPL-2.1+ |
3 | # | |
b2ad12eb MP |
4 | # Simple udev rules syntax checker |
5 | # | |
6 | # (C) 2010 Canonical Ltd. | |
7 | # Author: Martin Pitt <martin.pitt@ubuntu.com> | |
8 | # | |
0228a7e5 KS |
9 | # systemd is free software; you can redistribute it and/or modify it |
10 | # under the terms of the GNU Lesser General Public License as published by | |
11 | # the Free Software Foundation; either version 2.1 of the License, or | |
b2ad12eb | 12 | # (at your option) any later version. |
0228a7e5 KS |
13 | |
14 | # systemd is distributed in the hope that it will be useful, but | |
15 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | # Lesser General Public License for more details. | |
b2ad12eb | 18 | # |
0228a7e5 KS |
19 | # You should have received a copy of the GNU Lesser General Public License |
20 | # along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
b2ad12eb MP |
21 | |
22 | import re | |
23 | import sys | |
e8015e6e MP |
24 | import os |
25 | from glob import glob | |
b2ad12eb | 26 | |
6b97bf22 ZJS |
27 | rules_files = sys.argv[1:] |
28 | if not rules_files: | |
29 | sys.exit('Specify files to test as arguments') | |
b2ad12eb | 30 | |
cda39975 ZJS |
31 | no_args_tests = re.compile(r'(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|RESULT|TEST)\s*(?:=|!)=\s*"([^"]*)"$') |
32 | args_tests = re.compile(r'(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*"([^"]*)"$') | |
33 | no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|PROGRAM|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*"([^"]*)"$') | |
34 | args_assign = re.compile(r'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*"([^"]*)"$') | |
b2ad12eb MP |
35 | |
36 | result = 0 | |
37 | buffer = '' | |
e8015e6e | 38 | for path in rules_files: |
2956395c | 39 | print('# looking at {}'.format(path)) |
b2ad12eb MP |
40 | lineno = 0 |
41 | for line in open(path): | |
42 | lineno += 1 | |
43 | ||
44 | # handle line continuation | |
45 | if line.endswith('\\\n'): | |
46 | buffer += line[:-2] | |
47 | continue | |
48 | else: | |
49 | line = buffer + line | |
50 | buffer = '' | |
51 | ||
52 | # filter out comments and empty lines | |
53 | line = line.strip() | |
54 | if not line or line.startswith('#'): | |
55 | continue | |
56 | ||
57 | for clause in line.split(','): | |
58 | clause = clause.strip() | |
59 | if not (no_args_tests.match(clause) or args_tests.match(clause) or | |
60 | no_args_assign.match(clause) or args_assign.match(clause)): | |
61 | ||
2956395c ZJS |
62 | print('Invalid line {}:{}: {}'.format(path, lineno, line)) |
63 | print(' clause:', clause) | |
64 | print() | |
b2ad12eb MP |
65 | result = 1 |
66 | break | |
67 | ||
68 | sys.exit(result) |