]>
git.ipfire.org Git - thirdparty/systemd.git/blob - test/rule-syntax-check.py
2 # SPDX-License-Identifier: LGPL-2.1+
4 # Simple udev rules syntax checker
6 # (C) 2010 Canonical Ltd.
7 # Author: Martin Pitt <martin.pitt@ubuntu.com>
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
12 # (at your option) any later version.
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.
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/>.
27 rules_files
= sys
.argv
[1:]
29 sys
.exit('Specify files to test as arguments')
31 quoted_string_re
= r
'"(?:[^\\"]|\\.)*"'
32 no_args_tests
= re
.compile(r
'(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|PROGRAM|RESULT|TEST)\s*(?:=|!)=\s*' + quoted_string_re
+ '$')
33 args_tests
= re
.compile(r
'(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*' + quoted_string_re
+ '$')
34 no_args_assign
= re
.compile(r
'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re
+ '$')
35 args_assign
= re
.compile(r
'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*' + quoted_string_re
+ '$')
36 # Find comma-separated groups, but allow commas that are inside quoted strings.
37 # Using quoted_string_re + '?' so that strings missing the last double quote
38 # will still match for this part that splits on commas.
39 comma_separated_group_re
= re
.compile(r
'(?:[^,"]|' + quoted_string_re
+ '?)+')
43 for path
in rules_files
:
44 print('# looking at {}'.format(path
))
46 for line
in open(path
):
49 # handle line continuation
50 if line
.endswith('\\\n'):
57 # filter out comments and empty lines
59 if not line
or line
.startswith('#'):
62 # Separator ',' is normally optional but we make it mandatory here as
63 # it generally improves the readability of the rules.
64 for clause_match
in comma_separated_group_re
.finditer(line
):
65 clause
= clause_match
.group().strip()
66 if not (no_args_tests
.match(clause
) or args_tests
.match(clause
) or
67 no_args_assign
.match(clause
) or args_assign
.match(clause
)):
69 print('Invalid line {}:{}: {}'.format(path
, lineno
, line
))
70 print(' clause:', clause
)